qrwlock.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Queued read/write locks
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * (C) Copyright 2013-2014 Hewlett-Packard Development Company, L.P.
  15. *
  16. * Authors: Waiman Long <waiman.long@hp.com>
  17. */
  18. #include <linux/smp.h>
  19. #include <linux/bug.h>
  20. #include <linux/cpumask.h>
  21. #include <linux/percpu.h>
  22. #include <linux/hardirq.h>
  23. #include <asm/qrwlock.h>
  24. /*
  25. * This internal data structure is used for optimizing access to some of
  26. * the subfields within the atomic_t cnts.
  27. */
  28. struct __qrwlock {
  29. union {
  30. atomic_t cnts;
  31. struct {
  32. #ifdef __LITTLE_ENDIAN
  33. u8 wmode; /* Writer mode */
  34. u8 rcnts[3]; /* Reader counts */
  35. #else
  36. u8 rcnts[3]; /* Reader counts */
  37. u8 wmode; /* Writer mode */
  38. #endif
  39. };
  40. };
  41. arch_spinlock_t lock;
  42. };
  43. /**
  44. * rspin_until_writer_unlock - inc reader count & spin until writer is gone
  45. * @lock : Pointer to queue rwlock structure
  46. * @writer: Current queue rwlock writer status byte
  47. *
  48. * In interrupt context or at the head of the queue, the reader will just
  49. * increment the reader count & wait until the writer releases the lock.
  50. */
  51. static __always_inline void
  52. rspin_until_writer_unlock(struct qrwlock *lock, u32 cnts)
  53. {
  54. while ((cnts & _QW_WMASK) == _QW_LOCKED) {
  55. cpu_relax_lowlatency();
  56. cnts = atomic_read_acquire(&lock->cnts);
  57. }
  58. }
  59. /**
  60. * queued_read_lock_slowpath - acquire read lock of a queue rwlock
  61. * @lock: Pointer to queue rwlock structure
  62. * @cnts: Current qrwlock lock value
  63. */
  64. void queued_read_lock_slowpath(struct qrwlock *lock, u32 cnts)
  65. {
  66. /*
  67. * Readers come here when they cannot get the lock without waiting
  68. */
  69. if (unlikely(in_interrupt())) {
  70. /*
  71. * Readers in interrupt context will get the lock immediately
  72. * if the writer is just waiting (not holding the lock yet).
  73. * The rspin_until_writer_unlock() function returns immediately
  74. * in this case. Otherwise, they will spin (with ACQUIRE
  75. * semantics) until the lock is available without waiting in
  76. * the queue.
  77. */
  78. rspin_until_writer_unlock(lock, cnts);
  79. return;
  80. }
  81. atomic_sub(_QR_BIAS, &lock->cnts);
  82. /*
  83. * Put the reader into the wait queue
  84. */
  85. arch_spin_lock(&lock->wait_lock);
  86. /*
  87. * The ACQUIRE semantics of the following spinning code ensure
  88. * that accesses can't leak upwards out of our subsequent critical
  89. * section in the case that the lock is currently held for write.
  90. */
  91. cnts = atomic_add_return_acquire(_QR_BIAS, &lock->cnts) - _QR_BIAS;
  92. rspin_until_writer_unlock(lock, cnts);
  93. /*
  94. * Signal the next one in queue to become queue head
  95. */
  96. arch_spin_unlock(&lock->wait_lock);
  97. }
  98. EXPORT_SYMBOL(queued_read_lock_slowpath);
  99. /**
  100. * queued_write_lock_slowpath - acquire write lock of a queue rwlock
  101. * @lock : Pointer to queue rwlock structure
  102. */
  103. void queued_write_lock_slowpath(struct qrwlock *lock)
  104. {
  105. u32 cnts;
  106. /* Put the writer into the wait queue */
  107. arch_spin_lock(&lock->wait_lock);
  108. /* Try to acquire the lock directly if no reader is present */
  109. if (!atomic_read(&lock->cnts) &&
  110. (atomic_cmpxchg_acquire(&lock->cnts, 0, _QW_LOCKED) == 0))
  111. goto unlock;
  112. /*
  113. * Set the waiting flag to notify readers that a writer is pending,
  114. * or wait for a previous writer to go away.
  115. */
  116. for (;;) {
  117. struct __qrwlock *l = (struct __qrwlock *)lock;
  118. if (!READ_ONCE(l->wmode) &&
  119. (cmpxchg_relaxed(&l->wmode, 0, _QW_WAITING) == 0))
  120. break;
  121. cpu_relax_lowlatency();
  122. }
  123. /* When no more readers, set the locked flag */
  124. for (;;) {
  125. cnts = atomic_read(&lock->cnts);
  126. if ((cnts == _QW_WAITING) &&
  127. (atomic_cmpxchg_acquire(&lock->cnts, _QW_WAITING,
  128. _QW_LOCKED) == _QW_WAITING))
  129. break;
  130. cpu_relax_lowlatency();
  131. }
  132. unlock:
  133. arch_spin_unlock(&lock->wait_lock);
  134. }
  135. EXPORT_SYMBOL(queued_write_lock_slowpath);