Commit | Line | Data |
---|---|---|
d5de8841 JF |
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> | |
994025ca JF |
7 | #include <linux/debugfs.h> |
8 | #include <linux/log2.h> | |
5a0e3ad6 | 9 | #include <linux/gfp.h> |
354e7b76 | 10 | #include <linux/slab.h> |
d5de8841 JF |
11 | |
12 | #include <asm/paravirt.h> | |
13 | ||
14 | #include <xen/interface/xen.h> | |
15 | #include <xen/events.h> | |
16 | ||
17 | #include "xen-ops.h" | |
994025ca JF |
18 | #include "debugfs.h" |
19 | ||
80bd58fe JF |
20 | enum 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 | }; | |
994025ca | 28 | |
994025ca | 29 | |
80bd58fe | 30 | #ifdef CONFIG_XEN_DEBUG_FS |
f8eca41f | 31 | #define HISTO_BUCKETS 30 |
80bd58fe JF |
32 | static struct xen_spinlock_stats |
33 | { | |
34 | u32 contention_stats[NR_CONTENTION_STATS]; | |
f8eca41f | 35 | u32 histo_spin_blocked[HISTO_BUCKETS+1]; |
f8eca41f | 36 | u64 time_blocked; |
994025ca JF |
37 | } spinlock_stats; |
38 | ||
39 | static u8 zero_stats; | |
40 | ||
994025ca JF |
41 | static inline void check_zero(void) |
42 | { | |
80bd58fe JF |
43 | u8 ret; |
44 | u8 old = ACCESS_ONCE(zero_stats); | |
45 | 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)); | |
994025ca JF |
50 | } |
51 | } | |
52 | ||
80bd58fe JF |
53 | static inline void add_stats(enum xen_contention_stat var, u32 val) |
54 | { | |
55 | check_zero(); | |
56 | spinlock_stats.contention_stats[var] += val; | |
57 | } | |
994025ca JF |
58 | |
59 | static inline u64 spin_time_start(void) | |
60 | { | |
61 | return xen_clocksource_read(); | |
62 | } | |
63 | ||
64 | static 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 | ||
f8eca41f | 76 | static inline void spin_time_accum_blocked(u64 start) |
994025ca JF |
77 | { |
78 | u32 delta = xen_clocksource_read() - start; | |
79 | ||
f8eca41f JF |
80 | __spin_time_accum(delta, spinlock_stats.histo_spin_blocked); |
81 | spinlock_stats.time_blocked += delta; | |
994025ca JF |
82 | } |
83 | #else /* !CONFIG_XEN_DEBUG_FS */ | |
80bd58fe JF |
84 | static inline void add_stats(enum xen_contention_stat var, u32 val) |
85 | { | |
86 | } | |
994025ca JF |
87 | |
88 | static inline u64 spin_time_start(void) | |
89 | { | |
90 | return 0; | |
91 | } | |
92 | ||
f8eca41f | 93 | static inline void spin_time_accum_blocked(u64 start) |
994025ca JF |
94 | { |
95 | } | |
96 | #endif /* CONFIG_XEN_DEBUG_FS */ | |
d5de8841 | 97 | |
80bd58fe JF |
98 | struct xen_lock_waiting { |
99 | struct arch_spinlock *lock; | |
100 | __ticket_t want; | |
d5de8841 JF |
101 | }; |
102 | ||
545ac138 | 103 | static DEFINE_PER_CPU(int, lock_kicker_irq) = -1; |
354e7b76 | 104 | static DEFINE_PER_CPU(char *, irq_name); |
80bd58fe JF |
105 | static DEFINE_PER_CPU(struct xen_lock_waiting, lock_waiting); |
106 | static cpumask_t waiting_cpus; | |
d5de8841 | 107 | |
c3b7cb1f | 108 | static bool xen_pvspin = true; |
80bd58fe | 109 | static void xen_lock_spinning(struct arch_spinlock *lock, __ticket_t want) |
d5de8841 | 110 | { |
780f36d8 | 111 | int irq = __this_cpu_read(lock_kicker_irq); |
80bd58fe JF |
112 | struct xen_lock_waiting *w = &__get_cpu_var(lock_waiting); |
113 | int cpu = smp_processor_id(); | |
f8eca41f | 114 | u64 start; |
80bd58fe | 115 | unsigned long flags; |
d5de8841 JF |
116 | |
117 | /* If kicker interrupts not initialized yet, just spin */ | |
118 | if (irq == -1) | |
80bd58fe | 119 | return; |
d5de8841 | 120 | |
f8eca41f JF |
121 | start = spin_time_start(); |
122 | ||
80bd58fe JF |
123 | /* |
124 | * Make sure an interrupt handler can't upset things in a | |
125 | * partially setup state. | |
126 | */ | |
127 | local_irq_save(flags); | |
1ed7bf5f JF |
128 | /* |
129 | * We don't really care if we're overwriting some other | |
130 | * (lock,want) pair, as that would mean that we're currently | |
131 | * in an interrupt context, and the outer context had | |
132 | * interrupts enabled. That has already kicked the VCPU out | |
133 | * of xen_poll_irq(), so it will just return spuriously and | |
134 | * retry with newly setup (lock,want). | |
135 | * | |
136 | * The ordering protocol on this is that the "lock" pointer | |
137 | * may only be set non-NULL if the "want" ticket is correct. | |
138 | * If we're updating "want", we must first clear "lock". | |
139 | */ | |
140 | w->lock = NULL; | |
141 | smp_wmb(); | |
80bd58fe JF |
142 | w->want = want; |
143 | smp_wmb(); | |
144 | w->lock = lock; | |
4d576b57 | 145 | |
80bd58fe JF |
146 | /* This uses set_bit, which atomic and therefore a barrier */ |
147 | cpumask_set_cpu(cpu, &waiting_cpus); | |
148 | add_stats(TAKEN_SLOW, 1); | |
4d576b57 | 149 | |
80bd58fe JF |
150 | /* clear pending */ |
151 | xen_clear_irq_pending(irq); | |
4d576b57 | 152 | |
80bd58fe JF |
153 | /* Only check lock once pending cleared */ |
154 | barrier(); | |
d5de8841 | 155 | |
1ed7bf5f JF |
156 | /* |
157 | * Mark entry to slowpath before doing the pickup test to make | |
158 | * sure we don't deadlock with an unlocker. | |
159 | */ | |
96f853ea JF |
160 | __ticket_enter_slowpath(lock); |
161 | ||
1ed7bf5f JF |
162 | /* |
163 | * check again make sure it didn't become free while | |
164 | * we weren't looking | |
165 | */ | |
80bd58fe JF |
166 | if (ACCESS_ONCE(lock->tickets.head) == want) { |
167 | add_stats(TAKEN_SLOW_PICKUP, 1); | |
168 | goto out; | |
169 | } | |
1ed7bf5f JF |
170 | |
171 | /* Allow interrupts while blocked */ | |
172 | local_irq_restore(flags); | |
173 | ||
174 | /* | |
175 | * If an interrupt happens here, it will leave the wakeup irq | |
176 | * pending, which will cause xen_poll_irq() to return | |
177 | * immediately. | |
178 | */ | |
179 | ||
80bd58fe JF |
180 | /* Block until irq becomes pending (or perhaps a spurious wakeup) */ |
181 | xen_poll_irq(irq); | |
182 | add_stats(TAKEN_SLOW_SPURIOUS, !xen_test_irq_pending(irq)); | |
1ed7bf5f JF |
183 | |
184 | local_irq_save(flags); | |
185 | ||
d6c88a50 | 186 | kstat_incr_irqs_this_cpu(irq, irq_to_desc(irq)); |
d5de8841 | 187 | out: |
80bd58fe JF |
188 | cpumask_clear_cpu(cpu, &waiting_cpus); |
189 | w->lock = NULL; | |
1ed7bf5f | 190 | |
80bd58fe | 191 | local_irq_restore(flags); |
1ed7bf5f | 192 | |
f8eca41f | 193 | spin_time_accum_blocked(start); |
d5de8841 | 194 | } |
354714dd | 195 | PV_CALLEE_SAVE_REGS_THUNK(xen_lock_spinning); |
d5de8841 | 196 | |
80bd58fe | 197 | static void xen_unlock_kick(struct arch_spinlock *lock, __ticket_t next) |
d5de8841 JF |
198 | { |
199 | int cpu; | |
200 | ||
80bd58fe JF |
201 | add_stats(RELEASED_SLOW, 1); |
202 | ||
203 | for_each_cpu(cpu, &waiting_cpus) { | |
204 | const struct xen_lock_waiting *w = &per_cpu(lock_waiting, cpu); | |
994025ca | 205 | |
1ed7bf5f JF |
206 | /* Make sure we read lock before want */ |
207 | if (ACCESS_ONCE(w->lock) == lock && | |
208 | ACCESS_ONCE(w->want) == next) { | |
80bd58fe | 209 | add_stats(RELEASED_SLOW_KICKED, 1); |
d5de8841 | 210 | xen_send_IPI_one(cpu, XEN_SPIN_UNLOCK_VECTOR); |
80bd58fe | 211 | break; |
d5de8841 JF |
212 | } |
213 | } | |
214 | } | |
215 | ||
d5de8841 JF |
216 | static irqreturn_t dummy_handler(int irq, void *dev_id) |
217 | { | |
218 | BUG(); | |
219 | return IRQ_HANDLED; | |
220 | } | |
221 | ||
148f9bb8 | 222 | void xen_init_lock_cpu(int cpu) |
d5de8841 JF |
223 | { |
224 | int irq; | |
354e7b76 | 225 | char *name; |
d5de8841 | 226 | |
3310bbed KRW |
227 | if (!xen_pvspin) |
228 | return; | |
229 | ||
cb91f8f4 | 230 | WARN(per_cpu(lock_kicker_irq, cpu) >= 0, "spinlock on CPU%d exists on IRQ%d!\n", |
cb9c6f15 KRW |
231 | cpu, per_cpu(lock_kicker_irq, cpu)); |
232 | ||
d5de8841 JF |
233 | name = kasprintf(GFP_KERNEL, "spinlock%d", cpu); |
234 | irq = bind_ipi_to_irqhandler(XEN_SPIN_UNLOCK_VECTOR, | |
235 | cpu, | |
236 | dummy_handler, | |
9d71cee6 | 237 | IRQF_PERCPU|IRQF_NOBALANCING, |
d5de8841 JF |
238 | name, |
239 | NULL); | |
240 | ||
241 | if (irq >= 0) { | |
242 | disable_irq(irq); /* make sure it's never delivered */ | |
243 | per_cpu(lock_kicker_irq, cpu) = irq; | |
354e7b76 | 244 | per_cpu(irq_name, cpu) = name; |
d5de8841 JF |
245 | } |
246 | ||
247 | printk("cpu %d spinlock event irq %d\n", cpu, irq); | |
248 | } | |
249 | ||
d68d82af AN |
250 | void xen_uninit_lock_cpu(int cpu) |
251 | { | |
3310bbed KRW |
252 | if (!xen_pvspin) |
253 | return; | |
254 | ||
d68d82af | 255 | unbind_from_irqhandler(per_cpu(lock_kicker_irq, cpu), NULL); |
cb9c6f15 | 256 | per_cpu(lock_kicker_irq, cpu) = -1; |
354e7b76 KRW |
257 | kfree(per_cpu(irq_name, cpu)); |
258 | per_cpu(irq_name, cpu) = NULL; | |
d68d82af AN |
259 | } |
260 | ||
b8fa70b5 | 261 | |
a945928e KRW |
262 | /* |
263 | * Our init of PV spinlocks is split in two init functions due to us | |
264 | * using paravirt patching and jump labels patching and having to do | |
265 | * all of this before SMP code is invoked. | |
266 | * | |
267 | * The paravirt patching needs to be done _before_ the alternative asm code | |
268 | * is started, otherwise we would not patch the core kernel code. | |
269 | */ | |
d5de8841 JF |
270 | void __init xen_init_spinlocks(void) |
271 | { | |
70dd4998 | 272 | |
b8fa70b5 JF |
273 | if (!xen_pvspin) { |
274 | printk(KERN_DEBUG "xen: PV spinlocks disabled\n"); | |
275 | return; | |
276 | } | |
277 | ||
354714dd | 278 | pv_lock_ops.lock_spinning = PV_CALLEE_SAVE(xen_lock_spinning); |
80bd58fe | 279 | pv_lock_ops.unlock_kick = xen_unlock_kick; |
d5de8841 | 280 | } |
994025ca | 281 | |
a945928e KRW |
282 | /* |
283 | * While the jump_label init code needs to happend _after_ the jump labels are | |
284 | * enabled and before SMP is started. Hence we use pre-SMP initcall level | |
285 | * init. We cannot do it in xen_init_spinlocks as that is done before | |
286 | * jump labels are activated. | |
287 | */ | |
288 | static __init int xen_init_spinlocks_jump(void) | |
289 | { | |
290 | if (!xen_pvspin) | |
291 | return 0; | |
292 | ||
293 | static_key_slow_inc(¶virt_ticketlocks_enabled); | |
294 | return 0; | |
295 | } | |
296 | early_initcall(xen_init_spinlocks_jump); | |
297 | ||
b8fa70b5 JF |
298 | static __init int xen_parse_nopvspin(char *arg) |
299 | { | |
300 | xen_pvspin = false; | |
301 | return 0; | |
302 | } | |
303 | early_param("xen_nopvspin", xen_parse_nopvspin); | |
304 | ||
994025ca JF |
305 | #ifdef CONFIG_XEN_DEBUG_FS |
306 | ||
307 | static struct dentry *d_spin_debug; | |
308 | ||
309 | static int __init xen_spinlock_debugfs(void) | |
310 | { | |
311 | struct dentry *d_xen = xen_init_debugfs(); | |
312 | ||
313 | if (d_xen == NULL) | |
314 | return -ENOMEM; | |
315 | ||
3310bbed KRW |
316 | if (!xen_pvspin) |
317 | return 0; | |
318 | ||
994025ca JF |
319 | d_spin_debug = debugfs_create_dir("spinlocks", d_xen); |
320 | ||
321 | debugfs_create_u8("zero_stats", 0644, d_spin_debug, &zero_stats); | |
322 | ||
994025ca | 323 | debugfs_create_u32("taken_slow", 0444, d_spin_debug, |
80bd58fe | 324 | &spinlock_stats.contention_stats[TAKEN_SLOW]); |
994025ca | 325 | debugfs_create_u32("taken_slow_pickup", 0444, d_spin_debug, |
80bd58fe | 326 | &spinlock_stats.contention_stats[TAKEN_SLOW_PICKUP]); |
994025ca | 327 | debugfs_create_u32("taken_slow_spurious", 0444, d_spin_debug, |
80bd58fe | 328 | &spinlock_stats.contention_stats[TAKEN_SLOW_SPURIOUS]); |
994025ca | 329 | |
994025ca | 330 | debugfs_create_u32("released_slow", 0444, d_spin_debug, |
80bd58fe | 331 | &spinlock_stats.contention_stats[RELEASED_SLOW]); |
994025ca | 332 | debugfs_create_u32("released_slow_kicked", 0444, d_spin_debug, |
80bd58fe | 333 | &spinlock_stats.contention_stats[RELEASED_SLOW_KICKED]); |
994025ca | 334 | |
f8eca41f JF |
335 | debugfs_create_u64("time_blocked", 0444, d_spin_debug, |
336 | &spinlock_stats.time_blocked); | |
994025ca | 337 | |
9fe2a701 SV |
338 | debugfs_create_u32_array("histo_blocked", 0444, d_spin_debug, |
339 | spinlock_stats.histo_spin_blocked, HISTO_BUCKETS + 1); | |
994025ca JF |
340 | |
341 | return 0; | |
342 | } | |
343 | fs_initcall(xen_spinlock_debugfs); | |
344 | ||
345 | #endif /* CONFIG_XEN_DEBUG_FS */ |