spinlock.c 11 KB

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