spinlock.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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 <asm/paravirt.h>
  10. #include <xen/interface/xen.h>
  11. #include <xen/events.h>
  12. #include "xen-ops.h"
  13. #include "debugfs.h"
  14. #ifdef CONFIG_XEN_DEBUG_FS
  15. static struct xen_spinlock_stats
  16. {
  17. u64 taken;
  18. u32 taken_slow;
  19. u32 taken_slow_nested;
  20. u32 taken_slow_pickup;
  21. u32 taken_slow_spurious;
  22. u32 taken_slow_irqenable;
  23. u64 released;
  24. u32 released_slow;
  25. u32 released_slow_kicked;
  26. #define HISTO_BUCKETS 20
  27. u32 histo_spin_fast[HISTO_BUCKETS+1];
  28. u32 histo_spin[HISTO_BUCKETS+1];
  29. u64 spinning_time;
  30. u64 total_time;
  31. } spinlock_stats;
  32. static u8 zero_stats;
  33. static unsigned lock_timeout = 1 << 10;
  34. #define TIMEOUT lock_timeout
  35. static inline void check_zero(void)
  36. {
  37. if (unlikely(zero_stats)) {
  38. memset(&spinlock_stats, 0, sizeof(spinlock_stats));
  39. zero_stats = 0;
  40. }
  41. }
  42. #define ADD_STATS(elem, val) \
  43. do { check_zero(); spinlock_stats.elem += (val); } while(0)
  44. static inline u64 spin_time_start(void)
  45. {
  46. return xen_clocksource_read();
  47. }
  48. static void __spin_time_accum(u64 delta, u32 *array)
  49. {
  50. unsigned index = ilog2(delta);
  51. check_zero();
  52. if (index < HISTO_BUCKETS)
  53. array[index]++;
  54. else
  55. array[HISTO_BUCKETS]++;
  56. }
  57. static inline void spin_time_accum_fast(u64 start)
  58. {
  59. u32 delta = xen_clocksource_read() - start;
  60. __spin_time_accum(delta, spinlock_stats.histo_spin_fast);
  61. spinlock_stats.spinning_time += delta;
  62. }
  63. static inline void spin_time_accum(u64 start)
  64. {
  65. u32 delta = xen_clocksource_read() - start;
  66. __spin_time_accum(delta, spinlock_stats.histo_spin);
  67. spinlock_stats.total_time += delta;
  68. }
  69. #else /* !CONFIG_XEN_DEBUG_FS */
  70. #define TIMEOUT (1 << 10)
  71. #define ADD_STATS(elem, val) do { (void)(val); } while(0)
  72. static inline u64 spin_time_start(void)
  73. {
  74. return 0;
  75. }
  76. static inline void spin_time_accum_fast(u64 start)
  77. {
  78. }
  79. static inline void spin_time_accum(u64 start)
  80. {
  81. }
  82. #endif /* CONFIG_XEN_DEBUG_FS */
  83. struct xen_spinlock {
  84. unsigned char lock; /* 0 -> free; 1 -> locked */
  85. unsigned short spinners; /* count of waiting cpus */
  86. };
  87. static int xen_spin_is_locked(struct raw_spinlock *lock)
  88. {
  89. struct xen_spinlock *xl = (struct xen_spinlock *)lock;
  90. return xl->lock != 0;
  91. }
  92. static int xen_spin_is_contended(struct raw_spinlock *lock)
  93. {
  94. struct xen_spinlock *xl = (struct xen_spinlock *)lock;
  95. /* Not strictly true; this is only the count of contended
  96. lock-takers entering the slow path. */
  97. return xl->spinners != 0;
  98. }
  99. static int xen_spin_trylock(struct raw_spinlock *lock)
  100. {
  101. struct xen_spinlock *xl = (struct xen_spinlock *)lock;
  102. u8 old = 1;
  103. asm("xchgb %b0,%1"
  104. : "+q" (old), "+m" (xl->lock) : : "memory");
  105. return old == 0;
  106. }
  107. static DEFINE_PER_CPU(int, lock_kicker_irq) = -1;
  108. static DEFINE_PER_CPU(struct xen_spinlock *, lock_spinners);
  109. /*
  110. * Mark a cpu as interested in a lock. Returns the CPU's previous
  111. * lock of interest, in case we got preempted by an interrupt.
  112. */
  113. static inline struct xen_spinlock *spinning_lock(struct xen_spinlock *xl)
  114. {
  115. struct xen_spinlock *prev;
  116. prev = __get_cpu_var(lock_spinners);
  117. __get_cpu_var(lock_spinners) = xl;
  118. wmb(); /* set lock of interest before count */
  119. asm(LOCK_PREFIX " incw %0"
  120. : "+m" (xl->spinners) : : "memory");
  121. return prev;
  122. }
  123. /*
  124. * Mark a cpu as no longer interested in a lock. Restores previous
  125. * lock of interest (NULL for none).
  126. */
  127. static inline void unspinning_lock(struct xen_spinlock *xl, struct xen_spinlock *prev)
  128. {
  129. asm(LOCK_PREFIX " decw %0"
  130. : "+m" (xl->spinners) : : "memory");
  131. wmb(); /* decrement count before restoring lock */
  132. __get_cpu_var(lock_spinners) = prev;
  133. }
  134. static noinline int xen_spin_lock_slow(struct raw_spinlock *lock, bool irq_enable)
  135. {
  136. struct xen_spinlock *xl = (struct xen_spinlock *)lock;
  137. struct xen_spinlock *prev;
  138. int irq = __get_cpu_var(lock_kicker_irq);
  139. int ret;
  140. unsigned long flags;
  141. /* If kicker interrupts not initialized yet, just spin */
  142. if (irq == -1)
  143. return 0;
  144. /* announce we're spinning */
  145. prev = spinning_lock(xl);
  146. flags = __raw_local_save_flags();
  147. if (irq_enable) {
  148. ADD_STATS(taken_slow_irqenable, 1);
  149. raw_local_irq_enable();
  150. }
  151. ADD_STATS(taken_slow, 1);
  152. ADD_STATS(taken_slow_nested, prev != NULL);
  153. do {
  154. /* clear pending */
  155. xen_clear_irq_pending(irq);
  156. /* check again make sure it didn't become free while
  157. we weren't looking */
  158. ret = xen_spin_trylock(lock);
  159. if (ret) {
  160. ADD_STATS(taken_slow_pickup, 1);
  161. /*
  162. * If we interrupted another spinlock while it
  163. * was blocking, make sure it doesn't block
  164. * without rechecking the lock.
  165. */
  166. if (prev != NULL)
  167. xen_set_irq_pending(irq);
  168. goto out;
  169. }
  170. /*
  171. * Block until irq becomes pending. If we're
  172. * interrupted at this point (after the trylock but
  173. * before entering the block), then the nested lock
  174. * handler guarantees that the irq will be left
  175. * pending if there's any chance the lock became free;
  176. * xen_poll_irq() returns immediately if the irq is
  177. * pending.
  178. */
  179. xen_poll_irq(irq);
  180. ADD_STATS(taken_slow_spurious, !xen_test_irq_pending(irq));
  181. } while (!xen_test_irq_pending(irq)); /* check for spurious wakeups */
  182. kstat_this_cpu.irqs[irq]++;
  183. out:
  184. raw_local_irq_restore(flags);
  185. unspinning_lock(xl, prev);
  186. return ret;
  187. }
  188. static inline void __xen_spin_lock(struct raw_spinlock *lock, bool irq_enable)
  189. {
  190. struct xen_spinlock *xl = (struct xen_spinlock *)lock;
  191. unsigned timeout;
  192. u8 oldval;
  193. u64 start_spin;
  194. ADD_STATS(taken, 1);
  195. start_spin = spin_time_start();
  196. do {
  197. u64 start_spin_fast = spin_time_start();
  198. timeout = TIMEOUT;
  199. asm("1: xchgb %1,%0\n"
  200. " testb %1,%1\n"
  201. " jz 3f\n"
  202. "2: rep;nop\n"
  203. " cmpb $0,%0\n"
  204. " je 1b\n"
  205. " dec %2\n"
  206. " jnz 2b\n"
  207. "3:\n"
  208. : "+m" (xl->lock), "=q" (oldval), "+r" (timeout)
  209. : "1" (1)
  210. : "memory");
  211. spin_time_accum_fast(start_spin_fast);
  212. } while (unlikely(oldval != 0 &&
  213. (TIMEOUT == ~0 || !xen_spin_lock_slow(lock, irq_enable))));
  214. spin_time_accum(start_spin);
  215. }
  216. static void xen_spin_lock(struct raw_spinlock *lock)
  217. {
  218. __xen_spin_lock(lock, false);
  219. }
  220. static void xen_spin_lock_flags(struct raw_spinlock *lock, unsigned long flags)
  221. {
  222. __xen_spin_lock(lock, !raw_irqs_disabled_flags(flags));
  223. }
  224. static noinline void xen_spin_unlock_slow(struct xen_spinlock *xl)
  225. {
  226. int cpu;
  227. ADD_STATS(released_slow, 1);
  228. for_each_online_cpu(cpu) {
  229. /* XXX should mix up next cpu selection */
  230. if (per_cpu(lock_spinners, cpu) == xl) {
  231. ADD_STATS(released_slow_kicked, 1);
  232. xen_send_IPI_one(cpu, XEN_SPIN_UNLOCK_VECTOR);
  233. break;
  234. }
  235. }
  236. }
  237. static void xen_spin_unlock(struct raw_spinlock *lock)
  238. {
  239. struct xen_spinlock *xl = (struct xen_spinlock *)lock;
  240. ADD_STATS(released, 1);
  241. smp_wmb(); /* make sure no writes get moved after unlock */
  242. xl->lock = 0; /* release lock */
  243. /* make sure unlock happens before kick */
  244. barrier();
  245. if (unlikely(xl->spinners))
  246. xen_spin_unlock_slow(xl);
  247. }
  248. static irqreturn_t dummy_handler(int irq, void *dev_id)
  249. {
  250. BUG();
  251. return IRQ_HANDLED;
  252. }
  253. void __cpuinit xen_init_lock_cpu(int cpu)
  254. {
  255. int irq;
  256. const char *name;
  257. name = kasprintf(GFP_KERNEL, "spinlock%d", cpu);
  258. irq = bind_ipi_to_irqhandler(XEN_SPIN_UNLOCK_VECTOR,
  259. cpu,
  260. dummy_handler,
  261. IRQF_DISABLED|IRQF_PERCPU|IRQF_NOBALANCING,
  262. name,
  263. NULL);
  264. if (irq >= 0) {
  265. disable_irq(irq); /* make sure it's never delivered */
  266. per_cpu(lock_kicker_irq, cpu) = irq;
  267. }
  268. printk("cpu %d spinlock event irq %d\n", cpu, irq);
  269. }
  270. void __init xen_init_spinlocks(void)
  271. {
  272. pv_lock_ops.spin_is_locked = xen_spin_is_locked;
  273. pv_lock_ops.spin_is_contended = xen_spin_is_contended;
  274. pv_lock_ops.spin_lock = xen_spin_lock;
  275. pv_lock_ops.spin_lock_flags = xen_spin_lock_flags;
  276. pv_lock_ops.spin_trylock = xen_spin_trylock;
  277. pv_lock_ops.spin_unlock = xen_spin_unlock;
  278. }
  279. #ifdef CONFIG_XEN_DEBUG_FS
  280. static struct dentry *d_spin_debug;
  281. static int __init xen_spinlock_debugfs(void)
  282. {
  283. struct dentry *d_xen = xen_init_debugfs();
  284. if (d_xen == NULL)
  285. return -ENOMEM;
  286. d_spin_debug = debugfs_create_dir("spinlocks", d_xen);
  287. debugfs_create_u8("zero_stats", 0644, d_spin_debug, &zero_stats);
  288. debugfs_create_u32("timeout", 0644, d_spin_debug, &lock_timeout);
  289. debugfs_create_u64("taken", 0444, d_spin_debug, &spinlock_stats.taken);
  290. debugfs_create_u32("taken_slow", 0444, d_spin_debug,
  291. &spinlock_stats.taken_slow);
  292. debugfs_create_u32("taken_slow_nested", 0444, d_spin_debug,
  293. &spinlock_stats.taken_slow_nested);
  294. debugfs_create_u32("taken_slow_pickup", 0444, d_spin_debug,
  295. &spinlock_stats.taken_slow_pickup);
  296. debugfs_create_u32("taken_slow_spurious", 0444, d_spin_debug,
  297. &spinlock_stats.taken_slow_spurious);
  298. debugfs_create_u32("taken_slow_irqenable", 0444, d_spin_debug,
  299. &spinlock_stats.taken_slow_irqenable);
  300. debugfs_create_u64("released", 0444, d_spin_debug, &spinlock_stats.released);
  301. debugfs_create_u32("released_slow", 0444, d_spin_debug,
  302. &spinlock_stats.released_slow);
  303. debugfs_create_u32("released_slow_kicked", 0444, d_spin_debug,
  304. &spinlock_stats.released_slow_kicked);
  305. debugfs_create_u64("time_spinning", 0444, d_spin_debug,
  306. &spinlock_stats.spinning_time);
  307. debugfs_create_u64("time_total", 0444, d_spin_debug,
  308. &spinlock_stats.total_time);
  309. xen_debugfs_create_u32_array("histo_total", 0444, d_spin_debug,
  310. spinlock_stats.histo_spin, HISTO_BUCKETS + 1);
  311. xen_debugfs_create_u32_array("histo_spinning", 0444, d_spin_debug,
  312. spinlock_stats.histo_spin_fast, HISTO_BUCKETS + 1);
  313. return 0;
  314. }
  315. fs_initcall(xen_spinlock_debugfs);
  316. #endif /* CONFIG_XEN_DEBUG_FS */