GNU Linux-libre 4.4.284-gnu1
[releases.git] / arch / x86 / xen / spinlock.c
1 /*
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>
7 #include <linux/debugfs.h>
8 #include <linux/log2.h>
9 #include <linux/gfp.h>
10 #include <linux/slab.h>
11 #include <linux/atomic.h>
12
13 #include <asm/paravirt.h>
14
15 #include <xen/interface/xen.h>
16 #include <xen/events.h>
17
18 #include "xen-ops.h"
19 #include "debugfs.h"
20
21 static DEFINE_PER_CPU(int, lock_kicker_irq) = -1;
22 static DEFINE_PER_CPU(char *, irq_name);
23 static DEFINE_PER_CPU(atomic_t, xen_qlock_wait_nest);
24 static bool xen_pvspin = true;
25
26 #ifdef CONFIG_QUEUED_SPINLOCKS
27
28 #include <asm/qspinlock.h>
29
30 static void xen_qlock_kick(int cpu)
31 {
32         int irq = per_cpu(lock_kicker_irq, cpu);
33
34         /* Don't kick if the target's kicker interrupt is not initialized. */
35         if (irq == -1)
36                 return;
37
38         xen_send_IPI_one(cpu, XEN_SPIN_UNLOCK_VECTOR);
39 }
40
41 /*
42  * Halt the current CPU & release it back to the host
43  */
44 static void xen_qlock_wait(u8 *byte, u8 val)
45 {
46         int irq = __this_cpu_read(lock_kicker_irq);
47         atomic_t *nest_cnt = this_cpu_ptr(&xen_qlock_wait_nest);
48
49         /* If kicker interrupts not initialized yet, just spin */
50         if (irq == -1 || in_nmi())
51                 return;
52
53         /* Detect reentry. */
54         atomic_inc(nest_cnt);
55
56         /* If irq pending already and no nested call clear it. */
57         if (atomic_read(nest_cnt) == 1 && xen_test_irq_pending(irq)) {
58                 xen_clear_irq_pending(irq);
59         } else if (READ_ONCE(*byte) == val) {
60                 /* Block until irq becomes pending (or a spurious wakeup) */
61                 xen_poll_irq(irq);
62         }
63
64         atomic_dec(nest_cnt);
65 }
66
67 #else /* CONFIG_QUEUED_SPINLOCKS */
68
69 enum xen_contention_stat {
70         TAKEN_SLOW,
71         TAKEN_SLOW_PICKUP,
72         TAKEN_SLOW_SPURIOUS,
73         RELEASED_SLOW,
74         RELEASED_SLOW_KICKED,
75         NR_CONTENTION_STATS
76 };
77
78
79 #ifdef CONFIG_XEN_DEBUG_FS
80 #define HISTO_BUCKETS   30
81 static struct xen_spinlock_stats
82 {
83         u32 contention_stats[NR_CONTENTION_STATS];
84         u32 histo_spin_blocked[HISTO_BUCKETS+1];
85         u64 time_blocked;
86 } spinlock_stats;
87
88 static u8 zero_stats;
89
90 static inline void check_zero(void)
91 {
92         u8 ret;
93         u8 old = READ_ONCE(zero_stats);
94         if (unlikely(old)) {
95                 ret = cmpxchg(&zero_stats, old, 0);
96                 /* This ensures only one fellow resets the stat */
97                 if (ret == old)
98                         memset(&spinlock_stats, 0, sizeof(spinlock_stats));
99         }
100 }
101
102 static inline void add_stats(enum xen_contention_stat var, u32 val)
103 {
104         check_zero();
105         spinlock_stats.contention_stats[var] += val;
106 }
107
108 static inline u64 spin_time_start(void)
109 {
110         return xen_clocksource_read();
111 }
112
113 static void __spin_time_accum(u64 delta, u32 *array)
114 {
115         unsigned index = ilog2(delta);
116
117         check_zero();
118
119         if (index < HISTO_BUCKETS)
120                 array[index]++;
121         else
122                 array[HISTO_BUCKETS]++;
123 }
124
125 static inline void spin_time_accum_blocked(u64 start)
126 {
127         u32 delta = xen_clocksource_read() - start;
128
129         __spin_time_accum(delta, spinlock_stats.histo_spin_blocked);
130         spinlock_stats.time_blocked += delta;
131 }
132 #else  /* !CONFIG_XEN_DEBUG_FS */
133 static inline void add_stats(enum xen_contention_stat var, u32 val)
134 {
135 }
136
137 static inline u64 spin_time_start(void)
138 {
139         return 0;
140 }
141
142 static inline void spin_time_accum_blocked(u64 start)
143 {
144 }
145 #endif  /* CONFIG_XEN_DEBUG_FS */
146
147 struct xen_lock_waiting {
148         struct arch_spinlock *lock;
149         __ticket_t want;
150 };
151
152 static DEFINE_PER_CPU(struct xen_lock_waiting, lock_waiting);
153 static cpumask_t waiting_cpus;
154
155 __visible void xen_lock_spinning(struct arch_spinlock *lock, __ticket_t want)
156 {
157         int irq = __this_cpu_read(lock_kicker_irq);
158         struct xen_lock_waiting *w = this_cpu_ptr(&lock_waiting);
159         int cpu = smp_processor_id();
160         u64 start;
161         __ticket_t head;
162         unsigned long flags;
163
164         /* If kicker interrupts not initialized yet, just spin */
165         if (irq == -1)
166                 return;
167
168         start = spin_time_start();
169
170         /*
171          * Make sure an interrupt handler can't upset things in a
172          * partially setup state.
173          */
174         local_irq_save(flags);
175         /*
176          * We don't really care if we're overwriting some other
177          * (lock,want) pair, as that would mean that we're currently
178          * in an interrupt context, and the outer context had
179          * interrupts enabled.  That has already kicked the VCPU out
180          * of xen_poll_irq(), so it will just return spuriously and
181          * retry with newly setup (lock,want).
182          *
183          * The ordering protocol on this is that the "lock" pointer
184          * may only be set non-NULL if the "want" ticket is correct.
185          * If we're updating "want", we must first clear "lock".
186          */
187         w->lock = NULL;
188         smp_wmb();
189         w->want = want;
190         smp_wmb();
191         w->lock = lock;
192
193         /* This uses set_bit, which atomic and therefore a barrier */
194         cpumask_set_cpu(cpu, &waiting_cpus);
195         add_stats(TAKEN_SLOW, 1);
196
197         /* clear pending */
198         xen_clear_irq_pending(irq);
199
200         /* Only check lock once pending cleared */
201         barrier();
202
203         /*
204          * Mark entry to slowpath before doing the pickup test to make
205          * sure we don't deadlock with an unlocker.
206          */
207         __ticket_enter_slowpath(lock);
208
209         /* make sure enter_slowpath, which is atomic does not cross the read */
210         smp_mb__after_atomic();
211
212         /*
213          * check again make sure it didn't become free while
214          * we weren't looking
215          */
216         head = READ_ONCE(lock->tickets.head);
217         if (__tickets_equal(head, want)) {
218                 add_stats(TAKEN_SLOW_PICKUP, 1);
219                 goto out;
220         }
221
222         /* Allow interrupts while blocked */
223         local_irq_restore(flags);
224
225         /*
226          * If an interrupt happens here, it will leave the wakeup irq
227          * pending, which will cause xen_poll_irq() to return
228          * immediately.
229          */
230
231         /* Block until irq becomes pending (or perhaps a spurious wakeup) */
232         xen_poll_irq(irq);
233         add_stats(TAKEN_SLOW_SPURIOUS, !xen_test_irq_pending(irq));
234
235         local_irq_save(flags);
236
237         kstat_incr_irq_this_cpu(irq);
238 out:
239         cpumask_clear_cpu(cpu, &waiting_cpus);
240         w->lock = NULL;
241
242         local_irq_restore(flags);
243
244         spin_time_accum_blocked(start);
245 }
246 PV_CALLEE_SAVE_REGS_THUNK(xen_lock_spinning);
247
248 static void xen_unlock_kick(struct arch_spinlock *lock, __ticket_t next)
249 {
250         int cpu;
251
252         add_stats(RELEASED_SLOW, 1);
253
254         for_each_cpu(cpu, &waiting_cpus) {
255                 const struct xen_lock_waiting *w = &per_cpu(lock_waiting, cpu);
256
257                 /* Make sure we read lock before want */
258                 if (READ_ONCE(w->lock) == lock &&
259                     READ_ONCE(w->want) == next) {
260                         add_stats(RELEASED_SLOW_KICKED, 1);
261                         xen_send_IPI_one(cpu, XEN_SPIN_UNLOCK_VECTOR);
262                         break;
263                 }
264         }
265 }
266 #endif /* CONFIG_QUEUED_SPINLOCKS */
267
268 static irqreturn_t dummy_handler(int irq, void *dev_id)
269 {
270         BUG();
271         return IRQ_HANDLED;
272 }
273
274 void xen_init_lock_cpu(int cpu)
275 {
276         int irq;
277         char *name;
278
279         if (!xen_pvspin)
280                 return;
281
282         WARN(per_cpu(lock_kicker_irq, cpu) >= 0, "spinlock on CPU%d exists on IRQ%d!\n",
283              cpu, per_cpu(lock_kicker_irq, cpu));
284
285         name = kasprintf(GFP_KERNEL, "spinlock%d", cpu);
286         irq = bind_ipi_to_irqhandler(XEN_SPIN_UNLOCK_VECTOR,
287                                      cpu,
288                                      dummy_handler,
289                                      IRQF_PERCPU|IRQF_NOBALANCING,
290                                      name,
291                                      NULL);
292
293         if (irq >= 0) {
294                 disable_irq(irq); /* make sure it's never delivered */
295                 per_cpu(lock_kicker_irq, cpu) = irq;
296                 per_cpu(irq_name, cpu) = name;
297         }
298
299         printk("cpu %d spinlock event irq %d\n", cpu, irq);
300 }
301
302 void xen_uninit_lock_cpu(int cpu)
303 {
304         int irq;
305
306         if (!xen_pvspin)
307                 return;
308
309         /*
310          * When booting the kernel with 'mitigations=auto,nosmt', the secondary
311          * CPUs are not activated, and lock_kicker_irq is not initialized.
312          */
313         irq = per_cpu(lock_kicker_irq, cpu);
314         if (irq == -1)
315                 return;
316
317         unbind_from_irqhandler(irq, NULL);
318         per_cpu(lock_kicker_irq, cpu) = -1;
319         kfree(per_cpu(irq_name, cpu));
320         per_cpu(irq_name, cpu) = NULL;
321 }
322
323
324 /*
325  * Our init of PV spinlocks is split in two init functions due to us
326  * using paravirt patching and jump labels patching and having to do
327  * all of this before SMP code is invoked.
328  *
329  * The paravirt patching needs to be done _before_ the alternative asm code
330  * is started, otherwise we would not patch the core kernel code.
331  */
332 void __init xen_init_spinlocks(void)
333 {
334
335         if (!xen_pvspin) {
336                 printk(KERN_DEBUG "xen: PV spinlocks disabled\n");
337                 return;
338         }
339         printk(KERN_DEBUG "xen: PV spinlocks enabled\n");
340 #ifdef CONFIG_QUEUED_SPINLOCKS
341         __pv_init_lock_hash();
342         pv_lock_ops.queued_spin_lock_slowpath = __pv_queued_spin_lock_slowpath;
343         pv_lock_ops.queued_spin_unlock = PV_CALLEE_SAVE(__pv_queued_spin_unlock);
344         pv_lock_ops.wait = xen_qlock_wait;
345         pv_lock_ops.kick = xen_qlock_kick;
346 #else
347         pv_lock_ops.lock_spinning = PV_CALLEE_SAVE(xen_lock_spinning);
348         pv_lock_ops.unlock_kick = xen_unlock_kick;
349 #endif
350 }
351
352 /*
353  * While the jump_label init code needs to happend _after_ the jump labels are
354  * enabled and before SMP is started. Hence we use pre-SMP initcall level
355  * init. We cannot do it in xen_init_spinlocks as that is done before
356  * jump labels are activated.
357  */
358 static __init int xen_init_spinlocks_jump(void)
359 {
360         if (!xen_pvspin)
361                 return 0;
362
363         if (!xen_domain())
364                 return 0;
365
366         static_key_slow_inc(&paravirt_ticketlocks_enabled);
367         return 0;
368 }
369 early_initcall(xen_init_spinlocks_jump);
370
371 static __init int xen_parse_nopvspin(char *arg)
372 {
373         xen_pvspin = false;
374         return 0;
375 }
376 early_param("xen_nopvspin", xen_parse_nopvspin);
377
378 #if defined(CONFIG_XEN_DEBUG_FS) && !defined(CONFIG_QUEUED_SPINLOCKS)
379
380 static struct dentry *d_spin_debug;
381
382 static int __init xen_spinlock_debugfs(void)
383 {
384         struct dentry *d_xen = xen_init_debugfs();
385
386         if (d_xen == NULL)
387                 return -ENOMEM;
388
389         if (!xen_pvspin)
390                 return 0;
391
392         d_spin_debug = debugfs_create_dir("spinlocks", d_xen);
393
394         debugfs_create_u8("zero_stats", 0644, d_spin_debug, &zero_stats);
395
396         debugfs_create_u32("taken_slow", 0444, d_spin_debug,
397                            &spinlock_stats.contention_stats[TAKEN_SLOW]);
398         debugfs_create_u32("taken_slow_pickup", 0444, d_spin_debug,
399                            &spinlock_stats.contention_stats[TAKEN_SLOW_PICKUP]);
400         debugfs_create_u32("taken_slow_spurious", 0444, d_spin_debug,
401                            &spinlock_stats.contention_stats[TAKEN_SLOW_SPURIOUS]);
402
403         debugfs_create_u32("released_slow", 0444, d_spin_debug,
404                            &spinlock_stats.contention_stats[RELEASED_SLOW]);
405         debugfs_create_u32("released_slow_kicked", 0444, d_spin_debug,
406                            &spinlock_stats.contention_stats[RELEASED_SLOW_KICKED]);
407
408         debugfs_create_u64("time_blocked", 0444, d_spin_debug,
409                            &spinlock_stats.time_blocked);
410
411         debugfs_create_u32_array("histo_blocked", 0444, d_spin_debug,
412                                 spinlock_stats.histo_spin_blocked, HISTO_BUCKETS + 1);
413
414         return 0;
415 }
416 fs_initcall(xen_spinlock_debugfs);
417
418 #endif  /* CONFIG_XEN_DEBUG_FS */