blob: 956374c1edbc31e4c1eb50c3fb29cb8828ad44b5 [file] [log] [blame]
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -07001/*
2 * Split spinlock implementation out into its own file, so it can be
3 * compiled in a FTRACE-compatible way.
4 */
5#include <linux/kernel_stat.h>
6#include <linux/spinlock.h>
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -07007#include <linux/debugfs.h>
8#include <linux/log2.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09009#include <linux/gfp.h>
Konrad Rzeszutek Wilk354e7b72013-06-05 10:44:47 -040010#include <linux/slab.h>
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -070011
12#include <asm/paravirt.h>
13
14#include <xen/interface/xen.h>
15#include <xen/events.h>
16
17#include "xen-ops.h"
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -070018#include "debugfs.h"
19
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +053020enum xen_contention_stat {
21 TAKEN_SLOW,
22 TAKEN_SLOW_PICKUP,
23 TAKEN_SLOW_SPURIOUS,
24 RELEASED_SLOW,
25 RELEASED_SLOW_KICKED,
26 NR_CONTENTION_STATS
27};
28
29
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -070030#ifdef CONFIG_XEN_DEBUG_FS
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +053031#define HISTO_BUCKETS 30
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -070032static struct xen_spinlock_stats
33{
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +053034 u32 contention_stats[NR_CONTENTION_STATS];
Jeremy Fitzhardingef8eca412008-08-20 17:02:21 -070035 u32 histo_spin_blocked[HISTO_BUCKETS+1];
Jeremy Fitzhardingef8eca412008-08-20 17:02:21 -070036 u64 time_blocked;
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -070037} spinlock_stats;
38
39static u8 zero_stats;
40
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -070041static inline void check_zero(void)
42{
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +053043 u8 ret;
Raghavendra K Td6abfdb2015-02-06 16:44:11 +053044 u8 old = READ_ONCE(zero_stats);
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +053045 if (unlikely(old)) {
46 ret = cmpxchg(&zero_stats, old, 0);
47 /* This ensures only one fellow resets the stat */
48 if (ret == old)
49 memset(&spinlock_stats, 0, sizeof(spinlock_stats));
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -070050 }
51}
52
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +053053static inline void add_stats(enum xen_contention_stat var, u32 val)
54{
55 check_zero();
56 spinlock_stats.contention_stats[var] += val;
57}
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -070058
59static inline u64 spin_time_start(void)
60{
61 return xen_clocksource_read();
62}
63
64static void __spin_time_accum(u64 delta, u32 *array)
65{
66 unsigned index = ilog2(delta);
67
68 check_zero();
69
70 if (index < HISTO_BUCKETS)
71 array[index]++;
72 else
73 array[HISTO_BUCKETS]++;
74}
75
Jeremy Fitzhardingef8eca412008-08-20 17:02:21 -070076static inline void spin_time_accum_blocked(u64 start)
77{
78 u32 delta = xen_clocksource_read() - start;
79
80 __spin_time_accum(delta, spinlock_stats.histo_spin_blocked);
81 spinlock_stats.time_blocked += delta;
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -070082}
83#else /* !CONFIG_XEN_DEBUG_FS */
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +053084static inline void add_stats(enum xen_contention_stat var, u32 val)
85{
86}
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -070087
88static inline u64 spin_time_start(void)
89{
90 return 0;
91}
92
Jeremy Fitzhardingef8eca412008-08-20 17:02:21 -070093static inline void spin_time_accum_blocked(u64 start)
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -070094{
95}
96#endif /* CONFIG_XEN_DEBUG_FS */
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -070097
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +053098struct xen_lock_waiting {
99 struct arch_spinlock *lock;
100 __ticket_t want;
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700101};
102
Jeremy Fitzhardinge545ac132013-08-09 19:51:49 +0530103static DEFINE_PER_CPU(int, lock_kicker_irq) = -1;
Konrad Rzeszutek Wilk354e7b72013-06-05 10:44:47 -0400104static DEFINE_PER_CPU(char *, irq_name);
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +0530105static DEFINE_PER_CPU(struct xen_lock_waiting, lock_waiting);
106static cpumask_t waiting_cpus;
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700107
Konrad Rzeszutek Wilkc3b7cb12013-09-09 13:08:49 -0400108static bool xen_pvspin = true;
Andi Kleendd41f812013-10-22 09:07:58 -0700109__visible void xen_lock_spinning(struct arch_spinlock *lock, __ticket_t want)
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700110{
Christoph Lameter780f36d2010-12-06 11:16:29 -0600111 int irq = __this_cpu_read(lock_kicker_irq);
Christoph Lameter89cbc762014-08-17 12:30:40 -0500112 struct xen_lock_waiting *w = this_cpu_ptr(&lock_waiting);
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +0530113 int cpu = smp_processor_id();
Jeremy Fitzhardingef8eca412008-08-20 17:02:21 -0700114 u64 start;
Raghavendra K Td6abfdb2015-02-06 16:44:11 +0530115 __ticket_t head;
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +0530116 unsigned long flags;
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700117
118 /* If kicker interrupts not initialized yet, just spin */
119 if (irq == -1)
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +0530120 return;
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700121
Jeremy Fitzhardingef8eca412008-08-20 17:02:21 -0700122 start = spin_time_start();
123
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +0530124 /*
125 * Make sure an interrupt handler can't upset things in a
126 * partially setup state.
127 */
128 local_irq_save(flags);
Jeremy Fitzhardinge1ed7bf52013-08-09 19:51:59 +0530129 /*
130 * We don't really care if we're overwriting some other
131 * (lock,want) pair, as that would mean that we're currently
132 * in an interrupt context, and the outer context had
133 * interrupts enabled. That has already kicked the VCPU out
134 * of xen_poll_irq(), so it will just return spuriously and
135 * retry with newly setup (lock,want).
136 *
137 * The ordering protocol on this is that the "lock" pointer
138 * may only be set non-NULL if the "want" ticket is correct.
139 * If we're updating "want", we must first clear "lock".
140 */
141 w->lock = NULL;
142 smp_wmb();
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +0530143 w->want = want;
144 smp_wmb();
145 w->lock = lock;
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700146
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +0530147 /* This uses set_bit, which atomic and therefore a barrier */
148 cpumask_set_cpu(cpu, &waiting_cpus);
149 add_stats(TAKEN_SLOW, 1);
Jeremy Fitzhardinge4d576b52009-09-09 12:33:51 -0700150
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +0530151 /* clear pending */
152 xen_clear_irq_pending(irq);
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700153
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +0530154 /* Only check lock once pending cleared */
155 barrier();
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700156
Jeremy Fitzhardinge1ed7bf52013-08-09 19:51:59 +0530157 /*
158 * Mark entry to slowpath before doing the pickup test to make
159 * sure we don't deadlock with an unlocker.
160 */
Jeremy Fitzhardinge96f853e2013-08-09 19:51:58 +0530161 __ticket_enter_slowpath(lock);
162
Raghavendra K Td6abfdb2015-02-06 16:44:11 +0530163 /* make sure enter_slowpath, which is atomic does not cross the read */
164 smp_mb__after_atomic();
165
Jeremy Fitzhardinge1ed7bf52013-08-09 19:51:59 +0530166 /*
167 * check again make sure it didn't become free while
168 * we weren't looking
169 */
Raghavendra K Td6abfdb2015-02-06 16:44:11 +0530170 head = READ_ONCE(lock->tickets.head);
171 if (__tickets_equal(head, want)) {
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +0530172 add_stats(TAKEN_SLOW_PICKUP, 1);
173 goto out;
174 }
Jeremy Fitzhardinge1ed7bf52013-08-09 19:51:59 +0530175
176 /* Allow interrupts while blocked */
177 local_irq_restore(flags);
178
179 /*
180 * If an interrupt happens here, it will leave the wakeup irq
181 * pending, which will cause xen_poll_irq() to return
182 * immediately.
183 */
184
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +0530185 /* Block until irq becomes pending (or perhaps a spurious wakeup) */
186 xen_poll_irq(irq);
187 add_stats(TAKEN_SLOW_SPURIOUS, !xen_test_irq_pending(irq));
Jeremy Fitzhardinge1ed7bf52013-08-09 19:51:59 +0530188
189 local_irq_save(flags);
190
Thomas Gleixner770144e2014-02-23 21:40:16 +0000191 kstat_incr_irq_this_cpu(irq);
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700192out:
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +0530193 cpumask_clear_cpu(cpu, &waiting_cpus);
194 w->lock = NULL;
Jeremy Fitzhardinge1ed7bf52013-08-09 19:51:59 +0530195
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +0530196 local_irq_restore(flags);
Jeremy Fitzhardinge1ed7bf52013-08-09 19:51:59 +0530197
Jeremy Fitzhardingef8eca412008-08-20 17:02:21 -0700198 spin_time_accum_blocked(start);
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700199}
Jeremy Fitzhardinge354714d2013-08-09 19:51:55 +0530200PV_CALLEE_SAVE_REGS_THUNK(xen_lock_spinning);
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700201
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +0530202static void xen_unlock_kick(struct arch_spinlock *lock, __ticket_t next)
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700203{
204 int cpu;
205
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +0530206 add_stats(RELEASED_SLOW, 1);
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700207
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +0530208 for_each_cpu(cpu, &waiting_cpus) {
209 const struct xen_lock_waiting *w = &per_cpu(lock_waiting, cpu);
210
Jeremy Fitzhardinge1ed7bf52013-08-09 19:51:59 +0530211 /* Make sure we read lock before want */
Raghavendra K Td6abfdb2015-02-06 16:44:11 +0530212 if (READ_ONCE(w->lock) == lock &&
213 READ_ONCE(w->want) == next) {
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +0530214 add_stats(RELEASED_SLOW_KICKED, 1);
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700215 xen_send_IPI_one(cpu, XEN_SPIN_UNLOCK_VECTOR);
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +0530216 break;
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700217 }
218 }
219}
220
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700221static irqreturn_t dummy_handler(int irq, void *dev_id)
222{
223 BUG();
224 return IRQ_HANDLED;
225}
226
Paul Gortmaker148f9bb2013-06-18 18:23:59 -0400227void xen_init_lock_cpu(int cpu)
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700228{
229 int irq;
Konrad Rzeszutek Wilk354e7b72013-06-05 10:44:47 -0400230 char *name;
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700231
Konrad Rzeszutek Wilk3310bbe2013-08-26 14:28:06 -0400232 if (!xen_pvspin)
233 return;
234
Konrad Rzeszutek Wilkcb91f8f2013-05-06 08:33:15 -0400235 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 -0400236 cpu, per_cpu(lock_kicker_irq, cpu));
237
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700238 name = kasprintf(GFP_KERNEL, "spinlock%d", cpu);
239 irq = bind_ipi_to_irqhandler(XEN_SPIN_UNLOCK_VECTOR,
240 cpu,
241 dummy_handler,
Michael Opdenacker9d71cee2013-09-07 08:46:49 +0200242 IRQF_PERCPU|IRQF_NOBALANCING,
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700243 name,
244 NULL);
245
246 if (irq >= 0) {
247 disable_irq(irq); /* make sure it's never delivered */
248 per_cpu(lock_kicker_irq, cpu) = irq;
Konrad Rzeszutek Wilk354e7b72013-06-05 10:44:47 -0400249 per_cpu(irq_name, cpu) = name;
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700250 }
251
252 printk("cpu %d spinlock event irq %d\n", cpu, irq);
253}
254
Alex Nixond68d82a2008-08-22 11:52:15 +0100255void xen_uninit_lock_cpu(int cpu)
256{
Konrad Rzeszutek Wilk3310bbe2013-08-26 14:28:06 -0400257 if (!xen_pvspin)
258 return;
259
Alex Nixond68d82a2008-08-22 11:52:15 +0100260 unbind_from_irqhandler(per_cpu(lock_kicker_irq, cpu), NULL);
Konrad Rzeszutek Wilkcb9c6f12013-04-16 14:33:20 -0400261 per_cpu(lock_kicker_irq, cpu) = -1;
Konrad Rzeszutek Wilk354e7b72013-06-05 10:44:47 -0400262 kfree(per_cpu(irq_name, cpu));
263 per_cpu(irq_name, cpu) = NULL;
Alex Nixond68d82a2008-08-22 11:52:15 +0100264}
265
Jeremy Fitzhardingeb8fa70b2013-08-09 19:51:54 +0530266
Konrad Rzeszutek Wilka9459282013-09-12 22:29:44 -0400267/*
268 * Our init of PV spinlocks is split in two init functions due to us
269 * using paravirt patching and jump labels patching and having to do
270 * all of this before SMP code is invoked.
271 *
272 * The paravirt patching needs to be done _before_ the alternative asm code
273 * is started, otherwise we would not patch the core kernel code.
274 */
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700275void __init xen_init_spinlocks(void)
276{
Konrad Rzeszutek Wilk70dd4992013-04-16 14:34:45 -0400277
Jeremy Fitzhardingeb8fa70b2013-08-09 19:51:54 +0530278 if (!xen_pvspin) {
279 printk(KERN_DEBUG "xen: PV spinlocks disabled\n");
280 return;
281 }
Konrad Rzeszutek Wilke0fc17a2014-04-04 14:48:04 -0400282 printk(KERN_DEBUG "xen: PV spinlocks enabled\n");
Jeremy Fitzhardinge354714d2013-08-09 19:51:55 +0530283 pv_lock_ops.lock_spinning = PV_CALLEE_SAVE(xen_lock_spinning);
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +0530284 pv_lock_ops.unlock_kick = xen_unlock_kick;
Jeremy Fitzhardinged5de8842008-07-23 13:28:58 -0700285}
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700286
Konrad Rzeszutek Wilka9459282013-09-12 22:29:44 -0400287/*
288 * While the jump_label init code needs to happend _after_ the jump labels are
289 * enabled and before SMP is started. Hence we use pre-SMP initcall level
290 * init. We cannot do it in xen_init_spinlocks as that is done before
291 * jump labels are activated.
292 */
293static __init int xen_init_spinlocks_jump(void)
294{
295 if (!xen_pvspin)
296 return 0;
297
Konrad Rzeszutek Wilke0fc17a2014-04-04 14:48:04 -0400298 if (!xen_domain())
299 return 0;
300
Konrad Rzeszutek Wilka9459282013-09-12 22:29:44 -0400301 static_key_slow_inc(&paravirt_ticketlocks_enabled);
302 return 0;
303}
304early_initcall(xen_init_spinlocks_jump);
305
Jeremy Fitzhardingeb8fa70b2013-08-09 19:51:54 +0530306static __init int xen_parse_nopvspin(char *arg)
307{
308 xen_pvspin = false;
309 return 0;
310}
311early_param("xen_nopvspin", xen_parse_nopvspin);
312
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700313#ifdef CONFIG_XEN_DEBUG_FS
314
315static struct dentry *d_spin_debug;
316
317static int __init xen_spinlock_debugfs(void)
318{
319 struct dentry *d_xen = xen_init_debugfs();
320
321 if (d_xen == NULL)
322 return -ENOMEM;
323
Konrad Rzeszutek Wilk3310bbe2013-08-26 14:28:06 -0400324 if (!xen_pvspin)
325 return 0;
326
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700327 d_spin_debug = debugfs_create_dir("spinlocks", d_xen);
328
329 debugfs_create_u8("zero_stats", 0644, d_spin_debug, &zero_stats);
330
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700331 debugfs_create_u32("taken_slow", 0444, d_spin_debug,
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +0530332 &spinlock_stats.contention_stats[TAKEN_SLOW]);
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700333 debugfs_create_u32("taken_slow_pickup", 0444, d_spin_debug,
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +0530334 &spinlock_stats.contention_stats[TAKEN_SLOW_PICKUP]);
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700335 debugfs_create_u32("taken_slow_spurious", 0444, d_spin_debug,
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +0530336 &spinlock_stats.contention_stats[TAKEN_SLOW_SPURIOUS]);
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700337
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700338 debugfs_create_u32("released_slow", 0444, d_spin_debug,
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +0530339 &spinlock_stats.contention_stats[RELEASED_SLOW]);
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700340 debugfs_create_u32("released_slow_kicked", 0444, d_spin_debug,
Jeremy Fitzhardinge80bd58f2013-08-09 19:51:53 +0530341 &spinlock_stats.contention_stats[RELEASED_SLOW_KICKED]);
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700342
Jeremy Fitzhardingef8eca412008-08-20 17:02:21 -0700343 debugfs_create_u64("time_blocked", 0444, d_spin_debug,
344 &spinlock_stats.time_blocked);
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700345
Srivatsa Vaddagiri9fe2a702012-03-23 13:36:28 +0530346 debugfs_create_u32_array("histo_blocked", 0444, d_spin_debug,
347 spinlock_stats.histo_spin_blocked, HISTO_BUCKETS + 1);
Jeremy Fitzhardinge994025c2008-08-20 17:02:19 -0700348
349 return 0;
350}
351fs_initcall(xen_spinlock_debugfs);
352
353#endif /* CONFIG_XEN_DEBUG_FS */