spinlock.c 3.9 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
static DEFINE_PER_CPU(int, lock_kicker_irq) = -1;
static DEFINE_PER_CPU(char *, irq_name);
static bool xen_pvspin = true;

#include <asm/qspinlock.h>

static void xen_qlock_kick(int cpu)
{
28 29 30 31 32 33
	int irq = per_cpu(lock_kicker_irq, cpu);

	/* Don't kick if the target's kicker interrupt is not initialized. */
	if (irq == -1)
		return;

34 35 36 37 38 39 40 41 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
	xen_send_IPI_one(cpu, XEN_SPIN_UNLOCK_VECTOR);
}

/*
 * Halt the current CPU & release it back to the host
 */
static void xen_qlock_wait(u8 *byte, u8 val)
{
	int irq = __this_cpu_read(lock_kicker_irq);

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

	/* clear pending */
	xen_clear_irq_pending(irq);
	barrier();

	/*
	 * We check the byte value after clearing pending IRQ to make sure
	 * that we won't miss a wakeup event because of the clearing.
	 *
	 * The sync_clear_bit() call in xen_clear_irq_pending() is atomic.
	 * So it is effectively a memory barrier for x86.
	 */
	if (READ_ONCE(*byte) != val)
		return;

	/*
	 * If an interrupt happens here, it will leave the wakeup irq
	 * pending, which will cause xen_poll_irq() to return
	 * immediately.
	 */

	/* Block until irq becomes pending (or perhaps a spurious wakeup) */
	xen_poll_irq(irq);
}

72 73 74 75 76 77
static irqreturn_t dummy_handler(int irq, void *dev_id)
{
	BUG();
	return IRQ_HANDLED;
}

78
void xen_init_lock_cpu(int cpu)
79 80
{
	int irq;
81
	char *name;
82

83 84 85
	if (!xen_pvspin)
		return;

86
	WARN(per_cpu(lock_kicker_irq, cpu) >= 0, "spinlock on CPU%d exists on IRQ%d!\n",
87 88
	     cpu, per_cpu(lock_kicker_irq, cpu));

89 90 91 92
	name = kasprintf(GFP_KERNEL, "spinlock%d", cpu);
	irq = bind_ipi_to_irqhandler(XEN_SPIN_UNLOCK_VECTOR,
				     cpu,
				     dummy_handler,
93
				     IRQF_PERCPU|IRQF_NOBALANCING,
94 95 96 97 98 99
				     name,
				     NULL);

	if (irq >= 0) {
		disable_irq(irq); /* make sure it's never delivered */
		per_cpu(lock_kicker_irq, cpu) = irq;
100
		per_cpu(irq_name, cpu) = name;
101 102 103 104 105
	}

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

A
Alex Nixon 已提交
106 107
void xen_uninit_lock_cpu(int cpu)
{
108 109 110
	if (!xen_pvspin)
		return;

A
Alex Nixon 已提交
111
	unbind_from_irqhandler(per_cpu(lock_kicker_irq, cpu), NULL);
112
	per_cpu(lock_kicker_irq, cpu) = -1;
113 114
	kfree(per_cpu(irq_name, cpu));
	per_cpu(irq_name, cpu) = NULL;
A
Alex Nixon 已提交
115 116
}

117

118 119 120 121 122 123 124 125
/*
 * Our init of PV spinlocks is split in two init functions due to us
 * using paravirt patching and jump labels patching and having to do
 * all of this before SMP code is invoked.
 *
 * The paravirt patching needs to be done _before_ the alternative asm code
 * is started, otherwise we would not patch the core kernel code.
 */
126 127
void __init xen_init_spinlocks(void)
{
128

129 130 131 132
	if (!xen_pvspin) {
		printk(KERN_DEBUG "xen: PV spinlocks disabled\n");
		return;
	}
133
	printk(KERN_DEBUG "xen: PV spinlocks enabled\n");
134

135 136 137 138 139
	__pv_init_lock_hash();
	pv_lock_ops.queued_spin_lock_slowpath = __pv_queued_spin_lock_slowpath;
	pv_lock_ops.queued_spin_unlock = PV_CALLEE_SAVE(__pv_queued_spin_unlock);
	pv_lock_ops.wait = xen_qlock_wait;
	pv_lock_ops.kick = xen_qlock_kick;
140
}
J
Jeremy Fitzhardinge 已提交
141

142 143 144 145 146 147 148 149 150 151 152
/*
 * While the jump_label init code needs to happend _after_ the jump labels are
 * enabled and before SMP is started. Hence we use pre-SMP initcall level
 * init. We cannot do it in xen_init_spinlocks as that is done before
 * jump labels are activated.
 */
static __init int xen_init_spinlocks_jump(void)
{
	if (!xen_pvspin)
		return 0;

153 154 155
	if (!xen_domain())
		return 0;

156 157 158 159 160
	static_key_slow_inc(&paravirt_ticketlocks_enabled);
	return 0;
}
early_initcall(xen_init_spinlocks_jump);

161 162 163 164 165 166 167
static __init int xen_parse_nopvspin(char *arg)
{
	xen_pvspin = false;
	return 0;
}
early_param("xen_nopvspin", xen_parse_nopvspin);