1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Split spinlock implementation out into its own file, so it can be
4 * compiled in a FTRACE-compatible way.
5 */
6 #include <linux/kernel.h>
7 #include <linux/spinlock.h>
8 #include <linux/slab.h>
9 #include <linux/atomic.h>
10
11 #include <asm/paravirt.h>
12 #include <asm/qspinlock.h>
13
14 #include <xen/events.h>
15
16 #include "xen-ops.h"
17
18 static DEFINE_PER_CPU(int, lock_kicker_irq) = -1;
19 static DEFINE_PER_CPU(char *, irq_name);
20 static DEFINE_PER_CPU(atomic_t, xen_qlock_wait_nest);
21
xen_qlock_kick(int cpu)22 static void xen_qlock_kick(int cpu)
23 {
24 int irq = per_cpu(lock_kicker_irq, cpu);
25
26 /* Don't kick if the target's kicker interrupt is not initialized. */
27 if (irq == -1)
28 return;
29
30 xen_send_IPI_one(cpu, XEN_SPIN_UNLOCK_VECTOR);
31 }
32
33 /*
34 * Halt the current CPU & release it back to the host
35 */
xen_qlock_wait(u8 * byte,u8 val)36 static void xen_qlock_wait(u8 *byte, u8 val)
37 {
38 int irq = __this_cpu_read(lock_kicker_irq);
39 atomic_t *nest_cnt = this_cpu_ptr(&xen_qlock_wait_nest);
40
41 /* If kicker interrupts not initialized yet, just spin */
42 if (irq == -1 || in_nmi())
43 return;
44
45 /* Detect reentry. */
46 atomic_inc(nest_cnt);
47
48 /* If irq pending already and no nested call clear it. */
49 if (atomic_read(nest_cnt) == 1 && xen_test_irq_pending(irq)) {
50 xen_clear_irq_pending(irq);
51 } else if (READ_ONCE(*byte) == val) {
52 /* Block until irq becomes pending (or a spurious wakeup) */
53 xen_poll_irq(irq);
54 }
55
56 atomic_dec(nest_cnt);
57 }
58
dummy_handler(int irq,void * dev_id)59 static irqreturn_t dummy_handler(int irq, void *dev_id)
60 {
61 BUG();
62 return IRQ_HANDLED;
63 }
64
xen_init_lock_cpu(int cpu)65 void xen_init_lock_cpu(int cpu)
66 {
67 int irq;
68 char *name;
69
70 if (nopvspin)
71 return;
72
73 WARN(per_cpu(lock_kicker_irq, cpu) >= 0, "spinlock on CPU%d exists on IRQ%d!\n",
74 cpu, per_cpu(lock_kicker_irq, cpu));
75
76 name = kasprintf(GFP_KERNEL, "spinlock%d", cpu);
77 per_cpu(irq_name, cpu) = name;
78 irq = bind_ipi_to_irqhandler(XEN_SPIN_UNLOCK_VECTOR,
79 cpu,
80 dummy_handler,
81 IRQF_PERCPU|IRQF_NOBALANCING,
82 name,
83 NULL);
84
85 if (irq >= 0) {
86 disable_irq(irq); /* make sure it's never delivered */
87 per_cpu(lock_kicker_irq, cpu) = irq;
88 }
89
90 printk("cpu %d spinlock event irq %d\n", cpu, irq);
91 }
92
xen_uninit_lock_cpu(int cpu)93 void xen_uninit_lock_cpu(int cpu)
94 {
95 int irq;
96
97 if (nopvspin)
98 return;
99
100 kfree(per_cpu(irq_name, cpu));
101 per_cpu(irq_name, cpu) = NULL;
102 /*
103 * When booting the kernel with 'mitigations=auto,nosmt', the secondary
104 * CPUs are not activated, and lock_kicker_irq is not initialized.
105 */
106 irq = per_cpu(lock_kicker_irq, cpu);
107 if (irq == -1)
108 return;
109
110 unbind_from_irqhandler(irq, NULL);
111 per_cpu(lock_kicker_irq, cpu) = -1;
112 }
113
114 PV_CALLEE_SAVE_REGS_THUNK(xen_vcpu_stolen);
115
116 /*
117 * Our init of PV spinlocks is split in two init functions due to us
118 * using paravirt patching and jump labels patching and having to do
119 * all of this before SMP code is invoked.
120 *
121 * The paravirt patching needs to be done _before_ the alternative asm code
122 * is started, otherwise we would not patch the core kernel code.
123 */
xen_init_spinlocks(void)124 void __init xen_init_spinlocks(void)
125 {
126 /* Don't need to use pvqspinlock code if there is only 1 vCPU. */
127 if (num_possible_cpus() == 1)
128 nopvspin = true;
129
130 if (nopvspin) {
131 printk(KERN_DEBUG "xen: PV spinlocks disabled\n");
132 static_branch_disable(&virt_spin_lock_key);
133 return;
134 }
135 printk(KERN_DEBUG "xen: PV spinlocks enabled\n");
136
137 __pv_init_lock_hash();
138 pv_ops.lock.queued_spin_lock_slowpath = __pv_queued_spin_lock_slowpath;
139 pv_ops.lock.queued_spin_unlock =
140 PV_CALLEE_SAVE(__pv_queued_spin_unlock);
141 pv_ops.lock.wait = xen_qlock_wait;
142 pv_ops.lock.kick = xen_qlock_kick;
143 pv_ops.lock.vcpu_is_preempted = PV_CALLEE_SAVE(xen_vcpu_stolen);
144 }
145