blob: 043c73dfd2c98388c4adcdb9cb001ce0de0b15dc [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -07002/*
3 * Split spinlock implementation out into its own file, so it can be
4 * compiled in a FTRACE-compatible way.
5 */
Juergen Grossf2a5fef2018-11-19 14:59:45 +01006#include <linux/kernel.h>
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -07007#include <linux/spinlock.h>
Konrad Rzeszutek Wilk354e7b72013-06-05 10:44:47 -04008#include <linux/slab.h>
Juergen Grossd3132b32018-11-08 08:35:06 +01009#include <linux/atomic.h>
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -070010
11#include <asm/paravirt.h>
Juergen Grosse6fd28e2017-09-06 19:36:25 +020012#include <asm/qspinlock.h>
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -070013
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -070014#include <xen/events.h>
15
16#include "xen-ops.h"
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -070017
David Vrabele95e6f12015-04-24 14:56:40 -040018static DEFINE_PER_CPU(int, lock_kicker_irq) = -1;
19static DEFINE_PER_CPU(char *, irq_name);
Juergen Grossd3132b32018-11-08 08:35:06 +010020static DEFINE_PER_CPU(atomic_t, xen_qlock_wait_nest);
David Vrabele95e6f12015-04-24 14:56:40 -040021static bool xen_pvspin = true;
22
David Vrabele95e6f12015-04-24 14:56:40 -040023static void xen_qlock_kick(int cpu)
24{
Ross Lagerwall707e59b2016-04-22 13:05:31 +010025 int irq = per_cpu(lock_kicker_irq, cpu);
26
27 /* Don't kick if the target's kicker interrupt is not initialized. */
28 if (irq == -1)
29 return;
30
David Vrabele95e6f12015-04-24 14:56:40 -040031 xen_send_IPI_one(cpu, XEN_SPIN_UNLOCK_VECTOR);
32}
33
34/*
35 * Halt the current CPU & release it back to the host
36 */
37static void xen_qlock_wait(u8 *byte, u8 val)
38{
39 int irq = __this_cpu_read(lock_kicker_irq);
Juergen Grossd3132b32018-11-08 08:35:06 +010040 atomic_t *nest_cnt = this_cpu_ptr(&xen_qlock_wait_nest);
David Vrabele95e6f12015-04-24 14:56:40 -040041
42 /* If kicker interrupts not initialized yet, just spin */
Juergen Grossa8565312018-10-01 07:57:42 +020043 if (irq == -1 || in_nmi())
David Vrabele95e6f12015-04-24 14:56:40 -040044 return;
45
Juergen Grossd3132b32018-11-08 08:35:06 +010046 /* Detect reentry. */
47 atomic_inc(nest_cnt);
Juergen Grossa8565312018-10-01 07:57:42 +020048
Juergen Grossd3132b32018-11-08 08:35:06 +010049 /* If irq pending already and no nested call clear it. */
50 if (atomic_read(nest_cnt) == 1 && xen_test_irq_pending(irq)) {
Juergen Gross2ac2a7d2018-10-01 07:57:42 +020051 xen_clear_irq_pending(irq);
Juergen Grossa8565312018-10-01 07:57:42 +020052 } else if (READ_ONCE(*byte) == val) {
53 /* Block until irq becomes pending (or a spurious wakeup) */
54 xen_poll_irq(irq);
Juergen Gross2ac2a7d2018-10-01 07:57:42 +020055 }
David Vrabele95e6f12015-04-24 14:56:40 -040056
Juergen Grossd3132b32018-11-08 08:35:06 +010057 atomic_dec(nest_cnt);
David Vrabele95e6f12015-04-24 14:56:40 -040058}
59
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -070060static irqreturn_t dummy_handler(int irq, void *dev_id)
61{
62 BUG();
63 return IRQ_HANDLED;
64}
65
Paul Gortmaker148f9bb2013-06-18 18:23:59 -040066void xen_init_lock_cpu(int cpu)
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -070067{
68 int irq;
Konrad Rzeszutek Wilk354e7b72013-06-05 10:44:47 -040069 char *name;
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -070070
Zhenzhong Duan090d54b2019-06-26 16:57:09 +080071 if (!xen_pvspin)
Konrad Rzeszutek Wilk3310bbe2013-08-26 14:28:06 -040072 return;
73
Konrad Rzeszutek Wilkcb91f8f2013-05-06 08:33:15 -040074 WARN(per_cpu(lock_kicker_irq, cpu) >= 0, "spinlock on CPU%d exists on IRQ%d!\n",
Konrad Rzeszutek Wilkcb9c6f12013-04-16 14:33:20 -040075 cpu, per_cpu(lock_kicker_irq, cpu));
76
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -070077 name = kasprintf(GFP_KERNEL, "spinlock%d", cpu);
78 irq = bind_ipi_to_irqhandler(XEN_SPIN_UNLOCK_VECTOR,
79 cpu,
80 dummy_handler,
Michael Opdenacker9d71cee2013-09-07 08:46:49 +020081 IRQF_PERCPU|IRQF_NOBALANCING,
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -070082 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;
Konrad Rzeszutek Wilk354e7b72013-06-05 10:44:47 -040088 per_cpu(irq_name, cpu) = name;
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -070089 }
90
91 printk("cpu %d spinlock event irq %d\n", cpu, irq);
92}
93
Alex Nixond68d82a2008-08-22 11:52:15 +010094void xen_uninit_lock_cpu(int cpu)
95{
Brian Masney65cae182020-11-06 20:11:19 -050096 int irq;
97
Konrad Rzeszutek Wilk3310bbe2013-08-26 14:28:06 -040098 if (!xen_pvspin)
99 return;
100
Brian Masney65cae182020-11-06 20:11:19 -0500101 /*
102 * When booting the kernel with 'mitigations=auto,nosmt', the secondary
103 * CPUs are not activated, and lock_kicker_irq is not initialized.
104 */
105 irq = per_cpu(lock_kicker_irq, cpu);
106 if (irq == -1)
107 return;
108
109 unbind_from_irqhandler(irq, NULL);
Konrad Rzeszutek Wilkcb9c6f12013-04-16 14:33:20 -0400110 per_cpu(lock_kicker_irq, cpu) = -1;
Konrad Rzeszutek Wilk354e7b72013-06-05 10:44:47 -0400111 kfree(per_cpu(irq_name, cpu));
112 per_cpu(irq_name, cpu) = NULL;
Alex Nixond68d82a2008-08-22 11:52:15 +0100113}
114
Peter Zijlstra3cded412016-11-15 16:47:06 +0100115PV_CALLEE_SAVE_REGS_THUNK(xen_vcpu_stolen);
116
Konrad Rzeszutek Wilka9459282013-09-12 22:29:44 -0400117/*
118 * Our init of PV spinlocks is split in two init functions due to us
119 * using paravirt patching and jump labels patching and having to do
120 * all of this before SMP code is invoked.
121 *
122 * The paravirt patching needs to be done _before_ the alternative asm code
123 * is started, otherwise we would not patch the core kernel code.
124 */
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700125void __init xen_init_spinlocks(void)
126{
Waiman Long47b428d2018-07-19 17:39:57 -0400127 /* Don't need to use pvqspinlock code if there is only 1 vCPU. */
Zhenzhong Duan9a3c05e2019-10-23 19:16:23 +0800128 if (num_possible_cpus() == 1 || nopvspin)
Waiman Long47b428d2018-07-19 17:39:57 -0400129 xen_pvspin = false;
130
Jeremy Fitzhardingeb8fa70b2013-08-09 19:51:54 +0530131 if (!xen_pvspin) {
132 printk(KERN_DEBUG "xen: PV spinlocks disabled\n");
Zhenzhong Duan090d54b2019-06-26 16:57:09 +0800133 static_branch_disable(&virt_spin_lock_key);
Jeremy Fitzhardingeb8fa70b2013-08-09 19:51:54 +0530134 return;
135 }
Konrad Rzeszutek Wilke0fc17a2014-04-04 14:48:04 -0400136 printk(KERN_DEBUG "xen: PV spinlocks enabled\n");
Peter Zijlstracfd89832016-05-18 20:43:02 +0200137
David Vrabele95e6f12015-04-24 14:56:40 -0400138 __pv_init_lock_hash();
Juergen Gross5c835112018-08-28 09:40:19 +0200139 pv_ops.lock.queued_spin_lock_slowpath = __pv_queued_spin_lock_slowpath;
140 pv_ops.lock.queued_spin_unlock =
141 PV_CALLEE_SAVE(__pv_queued_spin_unlock);
142 pv_ops.lock.wait = xen_qlock_wait;
143 pv_ops.lock.kick = xen_qlock_kick;
144 pv_ops.lock.vcpu_is_preempted = PV_CALLEE_SAVE(xen_vcpu_stolen);
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700145}
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700146
Jeremy Fitzhardingeb8fa70b2013-08-09 19:51:54 +0530147static __init int xen_parse_nopvspin(char *arg)
148{
Zhenzhong Duan9a3c05e2019-10-23 19:16:23 +0800149 pr_notice("\"xen_nopvspin\" is deprecated, please use \"nopvspin\" instead\n");
Jeremy Fitzhardingeb8fa70b2013-08-09 19:51:54 +0530150 xen_pvspin = false;
151 return 0;
152}
153early_param("xen_nopvspin", xen_parse_nopvspin);
154