qspinlock.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_X86_QSPINLOCK_H
  3. #define _ASM_X86_QSPINLOCK_H
  4. #include <linux/jump_label.h>
  5. #include <asm/cpufeature.h>
  6. #include <asm-generic/qspinlock_types.h>
  7. #include <asm/paravirt.h>
  8. #include <asm/rmwcc.h>
  9. #define _Q_PENDING_LOOPS (1 << 9)
  10. #define queued_fetch_set_pending_acquire queued_fetch_set_pending_acquire
  11. static __always_inline u32 queued_fetch_set_pending_acquire(struct qspinlock *lock)
  12. {
  13. u32 val;
  14. /*
  15. * We can't use GEN_BINARY_RMWcc() inside an if() stmt because asm goto
  16. * and CONFIG_PROFILE_ALL_BRANCHES=y results in a label inside a
  17. * statement expression, which GCC doesn't like.
  18. */
  19. val = GEN_BINARY_RMWcc(LOCK_PREFIX "btsl", lock->val.counter, c,
  20. "I", _Q_PENDING_OFFSET) * _Q_PENDING_VAL;
  21. val |= atomic_read(&lock->val) & ~_Q_PENDING_MASK;
  22. return val;
  23. }
  24. #ifdef CONFIG_PARAVIRT_SPINLOCKS
  25. extern void native_queued_spin_lock_slowpath(struct qspinlock *lock, u32 val);
  26. extern void __pv_init_lock_hash(void);
  27. extern void __pv_queued_spin_lock_slowpath(struct qspinlock *lock, u32 val);
  28. extern void __raw_callee_save___pv_queued_spin_unlock(struct qspinlock *lock);
  29. extern bool nopvspin;
  30. #define queued_spin_unlock queued_spin_unlock
  31. /**
  32. * queued_spin_unlock - release a queued spinlock
  33. * @lock : Pointer to queued spinlock structure
  34. *
  35. * A smp_store_release() on the least-significant byte.
  36. */
  37. static inline void native_queued_spin_unlock(struct qspinlock *lock)
  38. {
  39. smp_store_release(&lock->locked, 0);
  40. }
  41. static inline void queued_spin_lock_slowpath(struct qspinlock *lock, u32 val)
  42. {
  43. pv_queued_spin_lock_slowpath(lock, val);
  44. }
  45. static inline void queued_spin_unlock(struct qspinlock *lock)
  46. {
  47. kcsan_release();
  48. pv_queued_spin_unlock(lock);
  49. }
  50. #define vcpu_is_preempted vcpu_is_preempted
  51. static inline bool vcpu_is_preempted(long cpu)
  52. {
  53. return pv_vcpu_is_preempted(cpu);
  54. }
  55. #endif
  56. #ifdef CONFIG_PARAVIRT
  57. /*
  58. * virt_spin_lock_key - enables (by default) the virt_spin_lock() hijack.
  59. *
  60. * Native (and PV wanting native due to vCPU pinning) should disable this key.
  61. * It is done in this backwards fashion to only have a single direction change,
  62. * which removes ordering between native_pv_spin_init() and HV setup.
  63. */
  64. DECLARE_STATIC_KEY_TRUE(virt_spin_lock_key);
  65. void native_pv_lock_init(void) __init;
  66. /*
  67. * Shortcut for the queued_spin_lock_slowpath() function that allows
  68. * virt to hijack it.
  69. *
  70. * Returns:
  71. * true - lock has been negotiated, all done;
  72. * false - queued_spin_lock_slowpath() will do its thing.
  73. */
  74. #define virt_spin_lock virt_spin_lock
  75. static inline bool virt_spin_lock(struct qspinlock *lock)
  76. {
  77. if (!static_branch_likely(&virt_spin_lock_key))
  78. return false;
  79. /*
  80. * On hypervisors without PARAVIRT_SPINLOCKS support we fall
  81. * back to a Test-and-Set spinlock, because fair locks have
  82. * horrible lock 'holder' preemption issues.
  83. */
  84. do {
  85. while (atomic_read(&lock->val) != 0)
  86. cpu_relax();
  87. } while (atomic_cmpxchg(&lock->val, 0, _Q_LOCKED_VAL) != 0);
  88. return true;
  89. }
  90. #else
  91. static inline void native_pv_lock_init(void)
  92. {
  93. }
  94. #endif /* CONFIG_PARAVIRT */
  95. #include <asm-generic/qspinlock.h>
  96. #endif /* _ASM_X86_QSPINLOCK_H */