Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Jeremy Fitzhardinge | d5de884 | 2008-07-23 13:28:58 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Split spinlock implementation out into its own file, so it can be |
| 4 | * compiled in a FTRACE-compatible way. |
| 5 | */ |
Juergen Gross | f2a5fef | 2018-11-19 14:59:45 +0100 | [diff] [blame] | 6 | #include <linux/kernel.h> |
Jeremy Fitzhardinge | d5de884 | 2008-07-23 13:28:58 -0700 | [diff] [blame] | 7 | #include <linux/spinlock.h> |
Konrad Rzeszutek Wilk | 354e7b7 | 2013-06-05 10:44:47 -0400 | [diff] [blame] | 8 | #include <linux/slab.h> |
Juergen Gross | d3132b3 | 2018-11-08 08:35:06 +0100 | [diff] [blame] | 9 | #include <linux/atomic.h> |
Jeremy Fitzhardinge | d5de884 | 2008-07-23 13:28:58 -0700 | [diff] [blame] | 10 | |
| 11 | #include <asm/paravirt.h> |
Juergen Gross | e6fd28e | 2017-09-06 19:36:25 +0200 | [diff] [blame] | 12 | #include <asm/qspinlock.h> |
Jeremy Fitzhardinge | d5de884 | 2008-07-23 13:28:58 -0700 | [diff] [blame] | 13 | |
Jeremy Fitzhardinge | d5de884 | 2008-07-23 13:28:58 -0700 | [diff] [blame] | 14 | #include <xen/events.h> |
| 15 | |
| 16 | #include "xen-ops.h" |
Jeremy Fitzhardinge | 994025c | 2008-08-20 17:02:19 -0700 | [diff] [blame] | 17 | |
David Vrabel | e95e6f1 | 2015-04-24 14:56:40 -0400 | [diff] [blame] | 18 | static DEFINE_PER_CPU(int, lock_kicker_irq) = -1; |
| 19 | static DEFINE_PER_CPU(char *, irq_name); |
Juergen Gross | d3132b3 | 2018-11-08 08:35:06 +0100 | [diff] [blame] | 20 | static DEFINE_PER_CPU(atomic_t, xen_qlock_wait_nest); |
David Vrabel | e95e6f1 | 2015-04-24 14:56:40 -0400 | [diff] [blame] | 21 | static bool xen_pvspin = true; |
| 22 | |
David Vrabel | e95e6f1 | 2015-04-24 14:56:40 -0400 | [diff] [blame] | 23 | static void xen_qlock_kick(int cpu) |
| 24 | { |
Ross Lagerwall | 707e59b | 2016-04-22 13:05:31 +0100 | [diff] [blame] | 25 | 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 Vrabel | e95e6f1 | 2015-04-24 14:56:40 -0400 | [diff] [blame] | 31 | xen_send_IPI_one(cpu, XEN_SPIN_UNLOCK_VECTOR); |
| 32 | } |
| 33 | |
| 34 | /* |
| 35 | * Halt the current CPU & release it back to the host |
| 36 | */ |
| 37 | static void xen_qlock_wait(u8 *byte, u8 val) |
| 38 | { |
| 39 | int irq = __this_cpu_read(lock_kicker_irq); |
Juergen Gross | d3132b3 | 2018-11-08 08:35:06 +0100 | [diff] [blame] | 40 | atomic_t *nest_cnt = this_cpu_ptr(&xen_qlock_wait_nest); |
David Vrabel | e95e6f1 | 2015-04-24 14:56:40 -0400 | [diff] [blame] | 41 | |
| 42 | /* If kicker interrupts not initialized yet, just spin */ |
Juergen Gross | a856531 | 2018-10-01 07:57:42 +0200 | [diff] [blame] | 43 | if (irq == -1 || in_nmi()) |
David Vrabel | e95e6f1 | 2015-04-24 14:56:40 -0400 | [diff] [blame] | 44 | return; |
| 45 | |
Juergen Gross | d3132b3 | 2018-11-08 08:35:06 +0100 | [diff] [blame] | 46 | /* Detect reentry. */ |
| 47 | atomic_inc(nest_cnt); |
Juergen Gross | a856531 | 2018-10-01 07:57:42 +0200 | [diff] [blame] | 48 | |
Juergen Gross | d3132b3 | 2018-11-08 08:35:06 +0100 | [diff] [blame] | 49 | /* If irq pending already and no nested call clear it. */ |
| 50 | if (atomic_read(nest_cnt) == 1 && xen_test_irq_pending(irq)) { |
Juergen Gross | 2ac2a7d | 2018-10-01 07:57:42 +0200 | [diff] [blame] | 51 | xen_clear_irq_pending(irq); |
Juergen Gross | a856531 | 2018-10-01 07:57:42 +0200 | [diff] [blame] | 52 | } else if (READ_ONCE(*byte) == val) { |
| 53 | /* Block until irq becomes pending (or a spurious wakeup) */ |
| 54 | xen_poll_irq(irq); |
Juergen Gross | 2ac2a7d | 2018-10-01 07:57:42 +0200 | [diff] [blame] | 55 | } |
David Vrabel | e95e6f1 | 2015-04-24 14:56:40 -0400 | [diff] [blame] | 56 | |
Juergen Gross | d3132b3 | 2018-11-08 08:35:06 +0100 | [diff] [blame] | 57 | atomic_dec(nest_cnt); |
David Vrabel | e95e6f1 | 2015-04-24 14:56:40 -0400 | [diff] [blame] | 58 | } |
| 59 | |
Jeremy Fitzhardinge | d5de884 | 2008-07-23 13:28:58 -0700 | [diff] [blame] | 60 | static irqreturn_t dummy_handler(int irq, void *dev_id) |
| 61 | { |
| 62 | BUG(); |
| 63 | return IRQ_HANDLED; |
| 64 | } |
| 65 | |
Paul Gortmaker | 148f9bb | 2013-06-18 18:23:59 -0400 | [diff] [blame] | 66 | void xen_init_lock_cpu(int cpu) |
Jeremy Fitzhardinge | d5de884 | 2008-07-23 13:28:58 -0700 | [diff] [blame] | 67 | { |
| 68 | int irq; |
Konrad Rzeszutek Wilk | 354e7b7 | 2013-06-05 10:44:47 -0400 | [diff] [blame] | 69 | char *name; |
Jeremy Fitzhardinge | d5de884 | 2008-07-23 13:28:58 -0700 | [diff] [blame] | 70 | |
Zhenzhong Duan | 090d54b | 2019-06-26 16:57:09 +0800 | [diff] [blame] | 71 | if (!xen_pvspin) |
Konrad Rzeszutek Wilk | 3310bbe | 2013-08-26 14:28:06 -0400 | [diff] [blame] | 72 | return; |
| 73 | |
Konrad Rzeszutek Wilk | cb91f8f | 2013-05-06 08:33:15 -0400 | [diff] [blame] | 74 | WARN(per_cpu(lock_kicker_irq, cpu) >= 0, "spinlock on CPU%d exists on IRQ%d!\n", |
Konrad Rzeszutek Wilk | cb9c6f1 | 2013-04-16 14:33:20 -0400 | [diff] [blame] | 75 | cpu, per_cpu(lock_kicker_irq, cpu)); |
| 76 | |
Jeremy Fitzhardinge | d5de884 | 2008-07-23 13:28:58 -0700 | [diff] [blame] | 77 | name = kasprintf(GFP_KERNEL, "spinlock%d", cpu); |
| 78 | irq = bind_ipi_to_irqhandler(XEN_SPIN_UNLOCK_VECTOR, |
| 79 | cpu, |
| 80 | dummy_handler, |
Michael Opdenacker | 9d71cee | 2013-09-07 08:46:49 +0200 | [diff] [blame] | 81 | IRQF_PERCPU|IRQF_NOBALANCING, |
Jeremy Fitzhardinge | d5de884 | 2008-07-23 13:28:58 -0700 | [diff] [blame] | 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; |
Konrad Rzeszutek Wilk | 354e7b7 | 2013-06-05 10:44:47 -0400 | [diff] [blame] | 88 | per_cpu(irq_name, cpu) = name; |
Jeremy Fitzhardinge | d5de884 | 2008-07-23 13:28:58 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | printk("cpu %d spinlock event irq %d\n", cpu, irq); |
| 92 | } |
| 93 | |
Alex Nixon | d68d82a | 2008-08-22 11:52:15 +0100 | [diff] [blame] | 94 | void xen_uninit_lock_cpu(int cpu) |
| 95 | { |
Brian Masney | 65cae18 | 2020-11-06 20:11:19 -0500 | [diff] [blame] | 96 | int irq; |
| 97 | |
Konrad Rzeszutek Wilk | 3310bbe | 2013-08-26 14:28:06 -0400 | [diff] [blame] | 98 | if (!xen_pvspin) |
| 99 | return; |
| 100 | |
Brian Masney | 65cae18 | 2020-11-06 20:11:19 -0500 | [diff] [blame] | 101 | /* |
| 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 Wilk | cb9c6f1 | 2013-04-16 14:33:20 -0400 | [diff] [blame] | 110 | per_cpu(lock_kicker_irq, cpu) = -1; |
Konrad Rzeszutek Wilk | 354e7b7 | 2013-06-05 10:44:47 -0400 | [diff] [blame] | 111 | kfree(per_cpu(irq_name, cpu)); |
| 112 | per_cpu(irq_name, cpu) = NULL; |
Alex Nixon | d68d82a | 2008-08-22 11:52:15 +0100 | [diff] [blame] | 113 | } |
| 114 | |
Peter Zijlstra | 3cded41 | 2016-11-15 16:47:06 +0100 | [diff] [blame] | 115 | PV_CALLEE_SAVE_REGS_THUNK(xen_vcpu_stolen); |
| 116 | |
Konrad Rzeszutek Wilk | a945928 | 2013-09-12 22:29:44 -0400 | [diff] [blame] | 117 | /* |
| 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 Fitzhardinge | d5de884 | 2008-07-23 13:28:58 -0700 | [diff] [blame] | 125 | void __init xen_init_spinlocks(void) |
| 126 | { |
Waiman Long | 47b428d | 2018-07-19 17:39:57 -0400 | [diff] [blame] | 127 | /* Don't need to use pvqspinlock code if there is only 1 vCPU. */ |
Zhenzhong Duan | 9a3c05e | 2019-10-23 19:16:23 +0800 | [diff] [blame] | 128 | if (num_possible_cpus() == 1 || nopvspin) |
Waiman Long | 47b428d | 2018-07-19 17:39:57 -0400 | [diff] [blame] | 129 | xen_pvspin = false; |
| 130 | |
Jeremy Fitzhardinge | b8fa70b | 2013-08-09 19:51:54 +0530 | [diff] [blame] | 131 | if (!xen_pvspin) { |
| 132 | printk(KERN_DEBUG "xen: PV spinlocks disabled\n"); |
Zhenzhong Duan | 090d54b | 2019-06-26 16:57:09 +0800 | [diff] [blame] | 133 | static_branch_disable(&virt_spin_lock_key); |
Jeremy Fitzhardinge | b8fa70b | 2013-08-09 19:51:54 +0530 | [diff] [blame] | 134 | return; |
| 135 | } |
Konrad Rzeszutek Wilk | e0fc17a | 2014-04-04 14:48:04 -0400 | [diff] [blame] | 136 | printk(KERN_DEBUG "xen: PV spinlocks enabled\n"); |
Peter Zijlstra | cfd8983 | 2016-05-18 20:43:02 +0200 | [diff] [blame] | 137 | |
David Vrabel | e95e6f1 | 2015-04-24 14:56:40 -0400 | [diff] [blame] | 138 | __pv_init_lock_hash(); |
Juergen Gross | 5c83511 | 2018-08-28 09:40:19 +0200 | [diff] [blame] | 139 | 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 Fitzhardinge | d5de884 | 2008-07-23 13:28:58 -0700 | [diff] [blame] | 145 | } |
Jeremy Fitzhardinge | 994025c | 2008-08-20 17:02:19 -0700 | [diff] [blame] | 146 | |
Jeremy Fitzhardinge | b8fa70b | 2013-08-09 19:51:54 +0530 | [diff] [blame] | 147 | static __init int xen_parse_nopvspin(char *arg) |
| 148 | { |
Zhenzhong Duan | 9a3c05e | 2019-10-23 19:16:23 +0800 | [diff] [blame] | 149 | pr_notice("\"xen_nopvspin\" is deprecated, please use \"nopvspin\" instead\n"); |
Jeremy Fitzhardinge | b8fa70b | 2013-08-09 19:51:54 +0530 | [diff] [blame] | 150 | xen_pvspin = false; |
| 151 | return 0; |
| 152 | } |
| 153 | early_param("xen_nopvspin", xen_parse_nopvspin); |
| 154 | |