spinlock.c 11.7 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 20 21 22 23 24 25 26 27
#include "debugfs.h"

#ifdef CONFIG_XEN_DEBUG_FS
static struct xen_spinlock_stats
{
	u64 taken;
	u32 taken_slow;
	u32 taken_slow_nested;
	u32 taken_slow_pickup;
	u32 taken_slow_spurious;
28
	u32 taken_slow_irqenable;
J
Jeremy Fitzhardinge 已提交
29 30 31 32 33

	u64 released;
	u32 released_slow;
	u32 released_slow_kicked;

34 35 36 37 38 39 40 41
#define HISTO_BUCKETS	30
	u32 histo_spin_total[HISTO_BUCKETS+1];
	u32 histo_spin_spinning[HISTO_BUCKETS+1];
	u32 histo_spin_blocked[HISTO_BUCKETS+1];

	u64 time_total;
	u64 time_spinning;
	u64 time_blocked;
J
Jeremy Fitzhardinge 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
} spinlock_stats;

static u8 zero_stats;

static unsigned lock_timeout = 1 << 10;
#define TIMEOUT lock_timeout

static inline void check_zero(void)
{
	if (unlikely(zero_stats)) {
		memset(&spinlock_stats, 0, sizeof(spinlock_stats));
		zero_stats = 0;
	}
}

#define ADD_STATS(elem, val)			\
	do { check_zero(); spinlock_stats.elem += (val); } while(0)

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]++;
}

77 78 79 80 81 82 83 84 85
static inline void spin_time_accum_spinning(u64 start)
{
	u32 delta = xen_clocksource_read() - start;

	__spin_time_accum(delta, spinlock_stats.histo_spin_spinning);
	spinlock_stats.time_spinning += delta;
}

static inline void spin_time_accum_total(u64 start)
J
Jeremy Fitzhardinge 已提交
86 87 88
{
	u32 delta = xen_clocksource_read() - start;

89 90
	__spin_time_accum(delta, spinlock_stats.histo_spin_total);
	spinlock_stats.time_total += delta;
J
Jeremy Fitzhardinge 已提交
91 92
}

93
static inline void spin_time_accum_blocked(u64 start)
J
Jeremy Fitzhardinge 已提交
94 95 96
{
	u32 delta = xen_clocksource_read() - start;

97 98
	__spin_time_accum(delta, spinlock_stats.histo_spin_blocked);
	spinlock_stats.time_blocked += delta;
J
Jeremy Fitzhardinge 已提交
99 100 101 102 103 104 105 106 107 108
}
#else  /* !CONFIG_XEN_DEBUG_FS */
#define TIMEOUT			(1 << 10)
#define ADD_STATS(elem, val)	do { (void)(val); } while(0)

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

109 110 111 112
static inline void spin_time_accum_total(u64 start)
{
}
static inline void spin_time_accum_spinning(u64 start)
J
Jeremy Fitzhardinge 已提交
113 114
{
}
115
static inline void spin_time_accum_blocked(u64 start)
J
Jeremy Fitzhardinge 已提交
116 117 118
{
}
#endif  /* CONFIG_XEN_DEBUG_FS */
119

120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
/*
 * 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

137 138
struct xen_spinlock {
	unsigned char lock;		/* 0 -> free; 1 -> locked */
139
	xen_spinners_t spinners;	/* count of waiting cpus */
140 141
};

142
static int xen_spin_is_locked(struct arch_spinlock *lock)
143 144 145 146 147 148
{
	struct xen_spinlock *xl = (struct xen_spinlock *)lock;

	return xl->lock != 0;
}

149
static int xen_spin_is_contended(struct arch_spinlock *lock)
150 151 152 153 154 155 156 157
{
	struct xen_spinlock *xl = (struct xen_spinlock *)lock;

	/* Not strictly true; this is only the count of contended
	   lock-takers entering the slow path. */
	return xl->spinners != 0;
}

158
static int xen_spin_trylock(struct arch_spinlock *lock)
159 160 161 162 163 164 165 166 167 168
{
	struct xen_spinlock *xl = (struct xen_spinlock *)lock;
	u8 old = 1;

	asm("xchgb %b0,%1"
	    : "+q" (old), "+m" (xl->lock) : : "memory");

	return old == 0;
}

169
static DEFINE_PER_CPU(char *, irq_name);
170 171 172
static DEFINE_PER_CPU(int, lock_kicker_irq) = -1;
static DEFINE_PER_CPU(struct xen_spinlock *, lock_spinners);

173 174 175 176 177
/*
 * Mark a cpu as interested in a lock.  Returns the CPU's previous
 * lock of interest, in case we got preempted by an interrupt.
 */
static inline struct xen_spinlock *spinning_lock(struct xen_spinlock *xl)
178
{
179 180
	struct xen_spinlock *prev;

C
Christoph Lameter 已提交
181 182
	prev = __this_cpu_read(lock_spinners);
	__this_cpu_write(lock_spinners, xl);
183

184
	wmb();			/* set lock of interest before count */
185

186
	inc_spinners(xl);
187 188

	return prev;
189 190
}

191 192 193 194 195
/*
 * Mark a cpu as no longer interested in a lock.  Restores previous
 * lock of interest (NULL for none).
 */
static inline void unspinning_lock(struct xen_spinlock *xl, struct xen_spinlock *prev)
196
{
197
	dec_spinners(xl);
198
	wmb();			/* decrement count before restoring lock */
C
Christoph Lameter 已提交
199
	__this_cpu_write(lock_spinners, prev);
200 201
}

202
static noinline int xen_spin_lock_slow(struct arch_spinlock *lock, bool irq_enable)
203 204
{
	struct xen_spinlock *xl = (struct xen_spinlock *)lock;
205
	struct xen_spinlock *prev;
C
Christoph Lameter 已提交
206
	int irq = __this_cpu_read(lock_kicker_irq);
207
	int ret;
208
	u64 start;
209 210 211 212 213

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

214 215
	start = spin_time_start();

216
	/* announce we're spinning */
217
	prev = spinning_lock(xl);
218

J
Jeremy Fitzhardinge 已提交
219 220 221
	ADD_STATS(taken_slow, 1);
	ADD_STATS(taken_slow_nested, prev != NULL);

222
	do {
223 224
		unsigned long flags;

225 226 227 228 229 230 231
		/* clear pending */
		xen_clear_irq_pending(irq);

		/* check again make sure it didn't become free while
		   we weren't looking  */
		ret = xen_spin_trylock(lock);
		if (ret) {
J
Jeremy Fitzhardinge 已提交
232 233
			ADD_STATS(taken_slow_pickup, 1);

234 235 236 237 238 239 240 241 242
			/*
			 * If we interrupted another spinlock while it
			 * was blocking, make sure it doesn't block
			 * without rechecking the lock.
			 */
			if (prev != NULL)
				xen_set_irq_pending(irq);
			goto out;
		}
243

D
David Howells 已提交
244
		flags = arch_local_save_flags();
245 246 247 248 249
		if (irq_enable) {
			ADD_STATS(taken_slow_irqenable, 1);
			raw_local_irq_enable();
		}

250 251 252 253 254 255 256 257 258 259
		/*
		 * Block until irq becomes pending.  If we're
		 * interrupted at this point (after the trylock but
		 * before entering the block), then the nested lock
		 * handler guarantees that the irq will be left
		 * pending if there's any chance the lock became free;
		 * xen_poll_irq() returns immediately if the irq is
		 * pending.
		 */
		xen_poll_irq(irq);
260 261 262

		raw_local_irq_restore(flags);

J
Jeremy Fitzhardinge 已提交
263
		ADD_STATS(taken_slow_spurious, !xen_test_irq_pending(irq));
264
	} while (!xen_test_irq_pending(irq)); /* check for spurious wakeups */
265

T
Thomas Gleixner 已提交
266
	kstat_incr_irqs_this_cpu(irq, irq_to_desc(irq));
267 268

out:
269
	unspinning_lock(xl, prev);
270 271
	spin_time_accum_blocked(start);

272 273 274
	return ret;
}

275
static inline void __xen_spin_lock(struct arch_spinlock *lock, bool irq_enable)
276 277
{
	struct xen_spinlock *xl = (struct xen_spinlock *)lock;
J
Jeremy Fitzhardinge 已提交
278
	unsigned timeout;
279
	u8 oldval;
J
Jeremy Fitzhardinge 已提交
280 281 282 283 284
	u64 start_spin;

	ADD_STATS(taken, 1);

	start_spin = spin_time_start();
285 286

	do {
J
Jeremy Fitzhardinge 已提交
287 288 289
		u64 start_spin_fast = spin_time_start();

		timeout = TIMEOUT;
290 291 292 293 294 295 296 297 298 299 300 301 302 303

		asm("1: xchgb %1,%0\n"
		    "   testb %1,%1\n"
		    "   jz 3f\n"
		    "2: rep;nop\n"
		    "   cmpb $0,%0\n"
		    "   je 1b\n"
		    "   dec %2\n"
		    "   jnz 2b\n"
		    "3:\n"
		    : "+m" (xl->lock), "=q" (oldval), "+r" (timeout)
		    : "1" (1)
		    : "memory");

304
		spin_time_accum_spinning(start_spin_fast);
305 306 307

	} while (unlikely(oldval != 0 &&
			  (TIMEOUT == ~0 || !xen_spin_lock_slow(lock, irq_enable))));
J
Jeremy Fitzhardinge 已提交
308

309
	spin_time_accum_total(start_spin);
310 311
}

312
static void xen_spin_lock(struct arch_spinlock *lock)
313 314 315 316
{
	__xen_spin_lock(lock, false);
}

317
static void xen_spin_lock_flags(struct arch_spinlock *lock, unsigned long flags)
318 319 320 321
{
	__xen_spin_lock(lock, !raw_irqs_disabled_flags(flags));
}

322 323 324 325
static noinline void xen_spin_unlock_slow(struct xen_spinlock *xl)
{
	int cpu;

J
Jeremy Fitzhardinge 已提交
326 327
	ADD_STATS(released_slow, 1);

328 329 330
	for_each_online_cpu(cpu) {
		/* XXX should mix up next cpu selection */
		if (per_cpu(lock_spinners, cpu) == xl) {
J
Jeremy Fitzhardinge 已提交
331
			ADD_STATS(released_slow_kicked, 1);
332 333 334 335 336
			xen_send_IPI_one(cpu, XEN_SPIN_UNLOCK_VECTOR);
		}
	}
}

337
static void xen_spin_unlock(struct arch_spinlock *lock)
338 339 340
{
	struct xen_spinlock *xl = (struct xen_spinlock *)lock;

J
Jeremy Fitzhardinge 已提交
341 342
	ADD_STATS(released, 1);

343 344 345
	smp_wmb();		/* make sure no writes get moved after unlock */
	xl->lock = 0;		/* release lock */

346 347 348 349 350 351 352
	/*
	 * Make sure unlock happens before checking for waiting
	 * spinners.  We need a strong barrier to enforce the
	 * write-read ordering to different memory locations, as the
	 * CPU makes no implied guarantees about their ordering.
	 */
	mb();
353 354 355 356 357 358 359 360 361 362 363 364 365 366

	if (unlikely(xl->spinners))
		xen_spin_unlock_slow(xl);
}

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

void __cpuinit xen_init_lock_cpu(int cpu)
{
	int irq;
367
	char *name;
368

369
	WARN(per_cpu(lock_kicker_irq, cpu) >= 0, "spinlock on CPU%d exists on IRQ%d!\n",
370 371
	     cpu, per_cpu(lock_kicker_irq, cpu));

372 373 374 375 376 377 378
	/*
	 * See git commit f10cd522c5fbfec9ae3cc01967868c9c2401ed23
	 * (xen: disable PV spinlocks on HVM)
	 */
	if (xen_hvm_domain())
		return;

379 380 381 382 383 384 385 386 387 388 389
	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;
390
		per_cpu(irq_name, cpu) = name;
391 392 393 394 395
	}

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

A
Alex Nixon 已提交
396 397
void xen_uninit_lock_cpu(int cpu)
{
398 399 400 401 402 403 404
	/*
	 * See git commit f10cd522c5fbfec9ae3cc01967868c9c2401ed23
	 * (xen: disable PV spinlocks on HVM)
	 */
	if (xen_hvm_domain())
		return;

A
Alex Nixon 已提交
405
	unbind_from_irqhandler(per_cpu(lock_kicker_irq, cpu), NULL);
406
	per_cpu(lock_kicker_irq, cpu) = -1;
407 408
	kfree(per_cpu(irq_name, cpu));
	per_cpu(irq_name, cpu) = NULL;
A
Alex Nixon 已提交
409 410
}

411 412
void __init xen_init_spinlocks(void)
{
413 414 415 416 417 418 419
	/*
	 * See git commit f10cd522c5fbfec9ae3cc01967868c9c2401ed23
	 * (xen: disable PV spinlocks on HVM)
	 */
	if (xen_hvm_domain())
		return;

420 421
	BUILD_BUG_ON(sizeof(struct xen_spinlock) > sizeof(arch_spinlock_t));

422 423 424
	pv_lock_ops.spin_is_locked = xen_spin_is_locked;
	pv_lock_ops.spin_is_contended = xen_spin_is_contended;
	pv_lock_ops.spin_lock = xen_spin_lock;
425
	pv_lock_ops.spin_lock_flags = xen_spin_lock_flags;
426 427 428
	pv_lock_ops.spin_trylock = xen_spin_trylock;
	pv_lock_ops.spin_unlock = xen_spin_unlock;
}
J
Jeremy Fitzhardinge 已提交
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455

#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("timeout", 0644, d_spin_debug, &lock_timeout);

	debugfs_create_u64("taken", 0444, d_spin_debug, &spinlock_stats.taken);
	debugfs_create_u32("taken_slow", 0444, d_spin_debug,
			   &spinlock_stats.taken_slow);
	debugfs_create_u32("taken_slow_nested", 0444, d_spin_debug,
			   &spinlock_stats.taken_slow_nested);
	debugfs_create_u32("taken_slow_pickup", 0444, d_spin_debug,
			   &spinlock_stats.taken_slow_pickup);
	debugfs_create_u32("taken_slow_spurious", 0444, d_spin_debug,
			   &spinlock_stats.taken_slow_spurious);
456 457
	debugfs_create_u32("taken_slow_irqenable", 0444, d_spin_debug,
			   &spinlock_stats.taken_slow_irqenable);
J
Jeremy Fitzhardinge 已提交
458 459 460 461 462 463 464 465

	debugfs_create_u64("released", 0444, d_spin_debug, &spinlock_stats.released);
	debugfs_create_u32("released_slow", 0444, d_spin_debug,
			   &spinlock_stats.released_slow);
	debugfs_create_u32("released_slow_kicked", 0444, d_spin_debug,
			   &spinlock_stats.released_slow_kicked);

	debugfs_create_u64("time_spinning", 0444, d_spin_debug,
466 467 468
			   &spinlock_stats.time_spinning);
	debugfs_create_u64("time_blocked", 0444, d_spin_debug,
			   &spinlock_stats.time_blocked);
J
Jeremy Fitzhardinge 已提交
469
	debugfs_create_u64("time_total", 0444, d_spin_debug,
470
			   &spinlock_stats.time_total);
J
Jeremy Fitzhardinge 已提交
471

472 473 474 475 476 477
	debugfs_create_u32_array("histo_total", 0444, d_spin_debug,
				spinlock_stats.histo_spin_total, HISTO_BUCKETS + 1);
	debugfs_create_u32_array("histo_spinning", 0444, d_spin_debug,
				spinlock_stats.histo_spin_spinning, HISTO_BUCKETS + 1);
	debugfs_create_u32_array("histo_blocked", 0444, d_spin_debug,
				spinlock_stats.histo_spin_blocked, HISTO_BUCKETS + 1);
J
Jeremy Fitzhardinge 已提交
478 479 480 481 482 483

	return 0;
}
fs_initcall(xen_spinlock_debugfs);

#endif	/* CONFIG_XEN_DEBUG_FS */