spinlock.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. /*
  96. * Size struct xen_spinlock so it's the same as arch_spinlock_t.
  97. */
  98. #if NR_CPUS < 256
  99. typedef u8 xen_spinners_t;
  100. # define inc_spinners(xl) \
  101. asm(LOCK_PREFIX " incb %0" : "+m" ((xl)->spinners) : : "memory");
  102. # define dec_spinners(xl) \
  103. asm(LOCK_PREFIX " decb %0" : "+m" ((xl)->spinners) : : "memory");
  104. #else
  105. typedef u16 xen_spinners_t;
  106. # define inc_spinners(xl) \
  107. asm(LOCK_PREFIX " incw %0" : "+m" ((xl)->spinners) : : "memory");
  108. # define dec_spinners(xl) \
  109. asm(LOCK_PREFIX " decw %0" : "+m" ((xl)->spinners) : : "memory");
  110. #endif
  111. struct xen_spinlock {
  112. unsigned char lock; /* 0 -> free; 1 -> locked */
  113. xen_spinners_t spinners; /* count of waiting cpus */
  114. };
  115. static int xen_spin_is_locked(struct arch_spinlock *lock)
  116. {
  117. struct xen_spinlock *xl = (struct xen_spinlock *)lock;
  118. return xl->lock != 0;
  119. }
  120. static int xen_spin_is_contended(struct arch_spinlock *lock)
  121. {
  122. struct xen_spinlock *xl = (struct xen_spinlock *)lock;
  123. /* Not strictly true; this is only the count of contended
  124. lock-takers entering the slow path. */
  125. return xl->spinners != 0;
  126. }
  127. static int xen_spin_trylock(struct arch_spinlock *lock)
  128. {
  129. struct xen_spinlock *xl = (struct xen_spinlock *)lock;
  130. u8 old = 1;
  131. asm("xchgb %b0,%1"
  132. : "+q" (old), "+m" (xl->lock) : : "memory");
  133. return old == 0;
  134. }
  135. static DEFINE_PER_CPU(int, lock_kicker_irq) = -1;
  136. static DEFINE_PER_CPU(struct xen_spinlock *, lock_spinners);
  137. /*
  138. * Mark a cpu as interested in a lock. Returns the CPU's previous
  139. * lock of interest, in case we got preempted by an interrupt.
  140. */
  141. static inline struct xen_spinlock *spinning_lock(struct xen_spinlock *xl)
  142. {
  143. struct xen_spinlock *prev;
  144. prev = __this_cpu_read(lock_spinners);
  145. __this_cpu_write(lock_spinners, xl);
  146. wmb(); /* set lock of interest before count */
  147. inc_spinners(xl);
  148. return prev;
  149. }
  150. /*
  151. * Mark a cpu as no longer interested in a lock. Restores previous
  152. * lock of interest (NULL for none).
  153. */
  154. static inline void unspinning_lock(struct xen_spinlock *xl, struct xen_spinlock *prev)
  155. {
  156. dec_spinners(xl);
  157. wmb(); /* decrement count before restoring lock */
  158. __this_cpu_write(lock_spinners, prev);
  159. }
  160. static noinline int xen_spin_lock_slow(struct arch_spinlock *lock, bool irq_enable)
  161. {
  162. struct xen_spinlock *xl = (struct xen_spinlock *)lock;
  163. struct xen_spinlock *prev;
  164. int irq = __this_cpu_read(lock_kicker_irq);
  165. int ret;
  166. u64 start;
  167. /* If kicker interrupts not initialized yet, just spin */
  168. if (irq == -1)
  169. return 0;
  170. start = spin_time_start();
  171. /* announce we're spinning */
  172. prev = spinning_lock(xl);
  173. ADD_STATS(taken_slow, 1);
  174. ADD_STATS(taken_slow_nested, prev != NULL);
  175. do {
  176. unsigned long flags;
  177. /* clear pending */
  178. xen_clear_irq_pending(irq);
  179. /* check again make sure it didn't become free while
  180. we weren't looking */
  181. ret = xen_spin_trylock(lock);
  182. if (ret) {
  183. ADD_STATS(taken_slow_pickup, 1);
  184. /*
  185. * If we interrupted another spinlock while it
  186. * was blocking, make sure it doesn't block
  187. * without rechecking the lock.
  188. */
  189. if (prev != NULL)
  190. xen_set_irq_pending(irq);
  191. goto out;
  192. }
  193. flags = arch_local_save_flags();
  194. if (irq_enable) {
  195. ADD_STATS(taken_slow_irqenable, 1);
  196. raw_local_irq_enable();
  197. }
  198. /*
  199. * Block until irq becomes pending. If we're
  200. * interrupted at this point (after the trylock but
  201. * before entering the block), then the nested lock
  202. * handler guarantees that the irq will be left
  203. * pending if there's any chance the lock became free;
  204. * xen_poll_irq() returns immediately if the irq is
  205. * pending.
  206. */
  207. xen_poll_irq(irq);
  208. raw_local_irq_restore(flags);
  209. ADD_STATS(taken_slow_spurious, !xen_test_irq_pending(irq));
  210. } while (!xen_test_irq_pending(irq)); /* check for spurious wakeups */
  211. kstat_incr_irqs_this_cpu(irq, irq_to_desc(irq));
  212. out:
  213. unspinning_lock(xl, prev);
  214. spin_time_accum_blocked(start);
  215. return ret;
  216. }
  217. static inline void __xen_spin_lock(struct arch_spinlock *lock, bool irq_enable)
  218. {
  219. struct xen_spinlock *xl = (struct xen_spinlock *)lock;
  220. unsigned timeout;
  221. u8 oldval;
  222. u64 start_spin;
  223. ADD_STATS(taken, 1);
  224. start_spin = spin_time_start();
  225. do {
  226. u64 start_spin_fast = spin_time_start();
  227. timeout = TIMEOUT;
  228. asm("1: xchgb %1,%0\n"
  229. " testb %1,%1\n"
  230. " jz 3f\n"
  231. "2: rep;nop\n"
  232. " cmpb $0,%0\n"
  233. " je 1b\n"
  234. " dec %2\n"
  235. " jnz 2b\n"
  236. "3:\n"
  237. : "+m" (xl->lock), "=q" (oldval), "+r" (timeout)
  238. : "1" (1)
  239. : "memory");
  240. spin_time_accum_spinning(start_spin_fast);
  241. } while (unlikely(oldval != 0 &&
  242. (TIMEOUT == ~0 || !xen_spin_lock_slow(lock, irq_enable))));
  243. spin_time_accum_total(start_spin);
  244. }
  245. static void xen_spin_lock(struct arch_spinlock *lock)
  246. {
  247. __xen_spin_lock(lock, false);
  248. }
  249. static void xen_spin_lock_flags(struct arch_spinlock *lock, unsigned long flags)
  250. {
  251. __xen_spin_lock(lock, !raw_irqs_disabled_flags(flags));
  252. }
  253. static noinline void xen_spin_unlock_slow(struct xen_spinlock *xl)
  254. {
  255. int cpu;
  256. ADD_STATS(released_slow, 1);
  257. for_each_online_cpu(cpu) {
  258. /* XXX should mix up next cpu selection */
  259. if (per_cpu(lock_spinners, cpu) == xl) {
  260. ADD_STATS(released_slow_kicked, 1);
  261. xen_send_IPI_one(cpu, XEN_SPIN_UNLOCK_VECTOR);
  262. }
  263. }
  264. }
  265. static void xen_spin_unlock(struct arch_spinlock *lock)
  266. {
  267. struct xen_spinlock *xl = (struct xen_spinlock *)lock;
  268. ADD_STATS(released, 1);
  269. smp_wmb(); /* make sure no writes get moved after unlock */
  270. xl->lock = 0; /* release lock */
  271. /*
  272. * Make sure unlock happens before checking for waiting
  273. * spinners. We need a strong barrier to enforce the
  274. * write-read ordering to different memory locations, as the
  275. * CPU makes no implied guarantees about their ordering.
  276. */
  277. mb();
  278. if (unlikely(xl->spinners))
  279. xen_spin_unlock_slow(xl);
  280. }
  281. static irqreturn_t dummy_handler(int irq, void *dev_id)
  282. {
  283. BUG();
  284. return IRQ_HANDLED;
  285. }
  286. void __cpuinit xen_init_lock_cpu(int cpu)
  287. {
  288. int irq;
  289. const char *name;
  290. name = kasprintf(GFP_KERNEL, "spinlock%d", cpu);
  291. irq = bind_ipi_to_irqhandler(XEN_SPIN_UNLOCK_VECTOR,
  292. cpu,
  293. dummy_handler,
  294. IRQF_DISABLED|IRQF_PERCPU|IRQF_NOBALANCING,
  295. name,
  296. NULL);
  297. if (irq >= 0) {
  298. disable_irq(irq); /* make sure it's never delivered */
  299. per_cpu(lock_kicker_irq, cpu) = irq;
  300. }
  301. printk("cpu %d spinlock event irq %d\n", cpu, irq);
  302. }
  303. void xen_uninit_lock_cpu(int cpu)
  304. {
  305. unbind_from_irqhandler(per_cpu(lock_kicker_irq, cpu), NULL);
  306. }
  307. void __init xen_init_spinlocks(void)
  308. {
  309. BUILD_BUG_ON(sizeof(struct xen_spinlock) > sizeof(arch_spinlock_t));
  310. pv_lock_ops.spin_is_locked = xen_spin_is_locked;
  311. pv_lock_ops.spin_is_contended = xen_spin_is_contended;
  312. pv_lock_ops.spin_lock = xen_spin_lock;
  313. pv_lock_ops.spin_lock_flags = xen_spin_lock_flags;
  314. pv_lock_ops.spin_trylock = xen_spin_trylock;
  315. pv_lock_ops.spin_unlock = xen_spin_unlock;
  316. }
  317. #ifdef CONFIG_XEN_DEBUG_FS
  318. static struct dentry *d_spin_debug;
  319. static int __init xen_spinlock_debugfs(void)
  320. {
  321. struct dentry *d_xen = xen_init_debugfs();
  322. if (d_xen == NULL)
  323. return -ENOMEM;
  324. d_spin_debug = debugfs_create_dir("spinlocks", d_xen);
  325. debugfs_create_u8("zero_stats", 0644, d_spin_debug, &zero_stats);
  326. debugfs_create_u32("timeout", 0644, d_spin_debug, &lock_timeout);
  327. debugfs_create_u64("taken", 0444, d_spin_debug, &spinlock_stats.taken);
  328. debugfs_create_u32("taken_slow", 0444, d_spin_debug,
  329. &spinlock_stats.taken_slow);
  330. debugfs_create_u32("taken_slow_nested", 0444, d_spin_debug,
  331. &spinlock_stats.taken_slow_nested);
  332. debugfs_create_u32("taken_slow_pickup", 0444, d_spin_debug,
  333. &spinlock_stats.taken_slow_pickup);
  334. debugfs_create_u32("taken_slow_spurious", 0444, d_spin_debug,
  335. &spinlock_stats.taken_slow_spurious);
  336. debugfs_create_u32("taken_slow_irqenable", 0444, d_spin_debug,
  337. &spinlock_stats.taken_slow_irqenable);
  338. debugfs_create_u64("released", 0444, d_spin_debug, &spinlock_stats.released);
  339. debugfs_create_u32("released_slow", 0444, d_spin_debug,
  340. &spinlock_stats.released_slow);
  341. debugfs_create_u32("released_slow_kicked", 0444, d_spin_debug,
  342. &spinlock_stats.released_slow_kicked);
  343. debugfs_create_u64("time_spinning", 0444, d_spin_debug,
  344. &spinlock_stats.time_spinning);
  345. debugfs_create_u64("time_blocked", 0444, d_spin_debug,
  346. &spinlock_stats.time_blocked);
  347. debugfs_create_u64("time_total", 0444, d_spin_debug,
  348. &spinlock_stats.time_total);
  349. xen_debugfs_create_u32_array("histo_total", 0444, d_spin_debug,
  350. spinlock_stats.histo_spin_total, HISTO_BUCKETS + 1);
  351. xen_debugfs_create_u32_array("histo_spinning", 0444, d_spin_debug,
  352. spinlock_stats.histo_spin_spinning, HISTO_BUCKETS + 1);
  353. xen_debugfs_create_u32_array("histo_blocked", 0444, d_spin_debug,
  354. spinlock_stats.histo_spin_blocked, HISTO_BUCKETS + 1);
  355. return 0;
  356. }
  357. fs_initcall(xen_spinlock_debugfs);
  358. #endif /* CONFIG_XEN_DEBUG_FS */