spinlock.c 10 KB

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