spinlock.c 12 KB

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