2 * Split spinlock implementation out into its own file, so it can be
3 * compiled in a FTRACE-compatible way.
5 #include <linux/kernel_stat.h>
6 #include <linux/spinlock.h>
7 #include <linux/debugfs.h>
8 #include <linux/log2.h>
11 #include <asm/paravirt.h>
13 #include <xen/interface/xen.h>
14 #include <xen/events.h>
19 #ifdef CONFIG_XEN_DEBUG_FS
20 static struct xen_spinlock_stats
24 u32 taken_slow_nested
;
25 u32 taken_slow_pickup
;
26 u32 taken_slow_spurious
;
27 u32 taken_slow_irqenable
;
31 u32 released_slow_kicked
;
33 #define HISTO_BUCKETS 30
34 u32 histo_spin_total
[HISTO_BUCKETS
+1];
35 u32 histo_spin_spinning
[HISTO_BUCKETS
+1];
36 u32 histo_spin_blocked
[HISTO_BUCKETS
+1];
45 static unsigned lock_timeout
= 1 << 10;
46 #define TIMEOUT lock_timeout
48 static inline void check_zero(void)
50 if (unlikely(zero_stats
)) {
51 memset(&spinlock_stats
, 0, sizeof(spinlock_stats
));
56 #define ADD_STATS(elem, val) \
57 do { check_zero(); spinlock_stats.elem += (val); } while(0)
59 static inline u64
spin_time_start(void)
61 return xen_clocksource_read();
64 static void __spin_time_accum(u64 delta
, u32
*array
)
66 unsigned index
= ilog2(delta
);
70 if (index
< HISTO_BUCKETS
)
73 array
[HISTO_BUCKETS
]++;
76 static inline void spin_time_accum_spinning(u64 start
)
78 u32 delta
= xen_clocksource_read() - start
;
80 __spin_time_accum(delta
, spinlock_stats
.histo_spin_spinning
);
81 spinlock_stats
.time_spinning
+= delta
;
84 static inline void spin_time_accum_total(u64 start
)
86 u32 delta
= xen_clocksource_read() - start
;
88 __spin_time_accum(delta
, spinlock_stats
.histo_spin_total
);
89 spinlock_stats
.time_total
+= delta
;
92 static inline void spin_time_accum_blocked(u64 start
)
94 u32 delta
= xen_clocksource_read() - start
;
96 __spin_time_accum(delta
, spinlock_stats
.histo_spin_blocked
);
97 spinlock_stats
.time_blocked
+= delta
;
99 #else /* !CONFIG_XEN_DEBUG_FS */
100 #define TIMEOUT (1 << 10)
101 #define ADD_STATS(elem, val) do { (void)(val); } while(0)
103 static inline u64
spin_time_start(void)
108 static inline void spin_time_accum_total(u64 start
)
111 static inline void spin_time_accum_spinning(u64 start
)
114 static inline void spin_time_accum_blocked(u64 start
)
117 #endif /* CONFIG_XEN_DEBUG_FS */
120 * Size struct xen_spinlock so it's the same as arch_spinlock_t.
123 typedef u8 xen_spinners_t
;
124 # define inc_spinners(xl) \
125 asm(LOCK_PREFIX " incb %0" : "+m" ((xl)->spinners) : : "memory");
126 # define dec_spinners(xl) \
127 asm(LOCK_PREFIX " decb %0" : "+m" ((xl)->spinners) : : "memory");
129 typedef u16 xen_spinners_t
;
130 # define inc_spinners(xl) \
131 asm(LOCK_PREFIX " incw %0" : "+m" ((xl)->spinners) : : "memory");
132 # define dec_spinners(xl) \
133 asm(LOCK_PREFIX " decw %0" : "+m" ((xl)->spinners) : : "memory");
136 struct xen_spinlock
{
137 unsigned char lock
; /* 0 -> free; 1 -> locked */
138 xen_spinners_t spinners
; /* count of waiting cpus */
141 static int xen_spin_is_locked(struct arch_spinlock
*lock
)
143 struct xen_spinlock
*xl
= (struct xen_spinlock
*)lock
;
145 return xl
->lock
!= 0;
148 static int xen_spin_is_contended(struct arch_spinlock
*lock
)
150 struct xen_spinlock
*xl
= (struct xen_spinlock
*)lock
;
152 /* Not strictly true; this is only the count of contended
153 lock-takers entering the slow path. */
154 return xl
->spinners
!= 0;
157 static int xen_spin_trylock(struct arch_spinlock
*lock
)
159 struct xen_spinlock
*xl
= (struct xen_spinlock
*)lock
;
163 : "+q" (old
), "+m" (xl
->lock
) : : "memory");
168 static DEFINE_PER_CPU(int, lock_kicker_irq
) = -1;
169 static DEFINE_PER_CPU(struct xen_spinlock
*, lock_spinners
);
172 * Mark a cpu as interested in a lock. Returns the CPU's previous
173 * lock of interest, in case we got preempted by an interrupt.
175 static inline struct xen_spinlock
*spinning_lock(struct xen_spinlock
*xl
)
177 struct xen_spinlock
*prev
;
179 prev
= __this_cpu_read(lock_spinners
);
180 __this_cpu_write(lock_spinners
, xl
);
182 wmb(); /* set lock of interest before count */
190 * Mark a cpu as no longer interested in a lock. Restores previous
191 * lock of interest (NULL for none).
193 static inline void unspinning_lock(struct xen_spinlock
*xl
, struct xen_spinlock
*prev
)
196 wmb(); /* decrement count before restoring lock */
197 __this_cpu_write(lock_spinners
, prev
);
200 static noinline
int xen_spin_lock_slow(struct arch_spinlock
*lock
, bool irq_enable
)
202 struct xen_spinlock
*xl
= (struct xen_spinlock
*)lock
;
203 struct xen_spinlock
*prev
;
204 int irq
= __this_cpu_read(lock_kicker_irq
);
208 /* If kicker interrupts not initialized yet, just spin */
212 start
= spin_time_start();
214 /* announce we're spinning */
215 prev
= spinning_lock(xl
);
217 ADD_STATS(taken_slow
, 1);
218 ADD_STATS(taken_slow_nested
, prev
!= NULL
);
224 xen_clear_irq_pending(irq
);
226 /* check again make sure it didn't become free while
227 we weren't looking */
228 ret
= xen_spin_trylock(lock
);
230 ADD_STATS(taken_slow_pickup
, 1);
233 * If we interrupted another spinlock while it
234 * was blocking, make sure it doesn't block
235 * without rechecking the lock.
238 xen_set_irq_pending(irq
);
242 flags
= arch_local_save_flags();
244 ADD_STATS(taken_slow_irqenable
, 1);
245 raw_local_irq_enable();
249 * Block until irq becomes pending. If we're
250 * interrupted at this point (after the trylock but
251 * before entering the block), then the nested lock
252 * handler guarantees that the irq will be left
253 * pending if there's any chance the lock became free;
254 * xen_poll_irq() returns immediately if the irq is
259 raw_local_irq_restore(flags
);
261 ADD_STATS(taken_slow_spurious
, !xen_test_irq_pending(irq
));
262 } while (!xen_test_irq_pending(irq
)); /* check for spurious wakeups */
264 kstat_incr_irqs_this_cpu(irq
, irq_to_desc(irq
));
267 unspinning_lock(xl
, prev
);
268 spin_time_accum_blocked(start
);
273 static inline void __xen_spin_lock(struct arch_spinlock
*lock
, bool irq_enable
)
275 struct xen_spinlock
*xl
= (struct xen_spinlock
*)lock
;
282 start_spin
= spin_time_start();
285 u64 start_spin_fast
= spin_time_start();
289 asm("1: xchgb %1,%0\n"
298 : "+m" (xl
->lock
), "=q" (oldval
), "+r" (timeout
)
302 spin_time_accum_spinning(start_spin_fast
);
304 } while (unlikely(oldval
!= 0 &&
305 (TIMEOUT
== ~0 || !xen_spin_lock_slow(lock
, irq_enable
))));
307 spin_time_accum_total(start_spin
);
310 static void xen_spin_lock(struct arch_spinlock
*lock
)
312 __xen_spin_lock(lock
, false);
315 static void xen_spin_lock_flags(struct arch_spinlock
*lock
, unsigned long flags
)
317 __xen_spin_lock(lock
, !raw_irqs_disabled_flags(flags
));
320 static noinline
void xen_spin_unlock_slow(struct xen_spinlock
*xl
)
324 ADD_STATS(released_slow
, 1);
326 for_each_online_cpu(cpu
) {
327 /* XXX should mix up next cpu selection */
328 if (per_cpu(lock_spinners
, cpu
) == xl
) {
329 ADD_STATS(released_slow_kicked
, 1);
330 xen_send_IPI_one(cpu
, XEN_SPIN_UNLOCK_VECTOR
);
336 static void xen_spin_unlock(struct arch_spinlock
*lock
)
338 struct xen_spinlock
*xl
= (struct xen_spinlock
*)lock
;
340 ADD_STATS(released
, 1);
342 smp_wmb(); /* make sure no writes get moved after unlock */
343 xl
->lock
= 0; /* release lock */
346 * Make sure unlock happens before checking for waiting
347 * spinners. We need a strong barrier to enforce the
348 * write-read ordering to different memory locations, as the
349 * CPU makes no implied guarantees about their ordering.
353 if (unlikely(xl
->spinners
))
354 xen_spin_unlock_slow(xl
);
357 static irqreturn_t
dummy_handler(int irq
, void *dev_id
)
363 void __cpuinit
xen_init_lock_cpu(int cpu
)
368 name
= kasprintf(GFP_KERNEL
, "spinlock%d", cpu
);
369 irq
= bind_ipi_to_irqhandler(XEN_SPIN_UNLOCK_VECTOR
,
372 IRQF_DISABLED
|IRQF_PERCPU
|IRQF_NOBALANCING
,
377 disable_irq(irq
); /* make sure it's never delivered */
378 per_cpu(lock_kicker_irq
, cpu
) = irq
;
381 printk("cpu %d spinlock event irq %d\n", cpu
, irq
);
384 void xen_uninit_lock_cpu(int cpu
)
386 unbind_from_irqhandler(per_cpu(lock_kicker_irq
, cpu
), NULL
);
389 void __init
xen_init_spinlocks(void)
391 BUILD_BUG_ON(sizeof(struct xen_spinlock
) > sizeof(arch_spinlock_t
));
393 pv_lock_ops
.spin_is_locked
= xen_spin_is_locked
;
394 pv_lock_ops
.spin_is_contended
= xen_spin_is_contended
;
395 pv_lock_ops
.spin_lock
= xen_spin_lock
;
396 pv_lock_ops
.spin_lock_flags
= xen_spin_lock_flags
;
397 pv_lock_ops
.spin_trylock
= xen_spin_trylock
;
398 pv_lock_ops
.spin_unlock
= xen_spin_unlock
;
401 #ifdef CONFIG_XEN_DEBUG_FS
403 static struct dentry
*d_spin_debug
;
405 static int __init
xen_spinlock_debugfs(void)
407 struct dentry
*d_xen
= xen_init_debugfs();
412 d_spin_debug
= debugfs_create_dir("spinlocks", d_xen
);
414 debugfs_create_u8("zero_stats", 0644, d_spin_debug
, &zero_stats
);
416 debugfs_create_u32("timeout", 0644, d_spin_debug
, &lock_timeout
);
418 debugfs_create_u64("taken", 0444, d_spin_debug
, &spinlock_stats
.taken
);
419 debugfs_create_u32("taken_slow", 0444, d_spin_debug
,
420 &spinlock_stats
.taken_slow
);
421 debugfs_create_u32("taken_slow_nested", 0444, d_spin_debug
,
422 &spinlock_stats
.taken_slow_nested
);
423 debugfs_create_u32("taken_slow_pickup", 0444, d_spin_debug
,
424 &spinlock_stats
.taken_slow_pickup
);
425 debugfs_create_u32("taken_slow_spurious", 0444, d_spin_debug
,
426 &spinlock_stats
.taken_slow_spurious
);
427 debugfs_create_u32("taken_slow_irqenable", 0444, d_spin_debug
,
428 &spinlock_stats
.taken_slow_irqenable
);
430 debugfs_create_u64("released", 0444, d_spin_debug
, &spinlock_stats
.released
);
431 debugfs_create_u32("released_slow", 0444, d_spin_debug
,
432 &spinlock_stats
.released_slow
);
433 debugfs_create_u32("released_slow_kicked", 0444, d_spin_debug
,
434 &spinlock_stats
.released_slow_kicked
);
436 debugfs_create_u64("time_spinning", 0444, d_spin_debug
,
437 &spinlock_stats
.time_spinning
);
438 debugfs_create_u64("time_blocked", 0444, d_spin_debug
,
439 &spinlock_stats
.time_blocked
);
440 debugfs_create_u64("time_total", 0444, d_spin_debug
,
441 &spinlock_stats
.time_total
);
443 xen_debugfs_create_u32_array("histo_total", 0444, d_spin_debug
,
444 spinlock_stats
.histo_spin_total
, HISTO_BUCKETS
+ 1);
445 xen_debugfs_create_u32_array("histo_spinning", 0444, d_spin_debug
,
446 spinlock_stats
.histo_spin_spinning
, HISTO_BUCKETS
+ 1);
447 xen_debugfs_create_u32_array("histo_blocked", 0444, d_spin_debug
,
448 spinlock_stats
.histo_spin_blocked
, HISTO_BUCKETS
+ 1);
452 fs_initcall(xen_spinlock_debugfs
);
454 #endif /* CONFIG_XEN_DEBUG_FS */