spinlock.c 12 KB

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