qrwlock.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 <linux/spinlock.h>
  24. #include <asm/qrwlock.h>
  25. /*
  26. * This internal data structure is used for optimizing access to some of
  27. * the subfields within the atomic_t cnts.
  28. */
  29. struct __qrwlock {
  30. union {
  31. atomic_t cnts;
  32. struct {
  33. #ifdef __LITTLE_ENDIAN
  34. u8 wmode; /* Writer mode */
  35. u8 rcnts[3]; /* Reader counts */
  36. #else
  37. u8 rcnts[3]; /* Reader counts */
  38. u8 wmode; /* Writer mode */
  39. #endif
  40. };
  41. };
  42. arch_spinlock_t lock;
  43. };
  44. /**
  45. * rspin_until_writer_unlock - inc reader count & spin until writer is gone
  46. * @lock : Pointer to queue rwlock structure
  47. * @writer: Current queue rwlock writer status byte
  48. *
  49. * In interrupt context or at the head of the queue, the reader will just
  50. * increment the reader count & wait until the writer releases the lock.
  51. */
  52. static __always_inline void
  53. rspin_until_writer_unlock(struct qrwlock *lock, u32 cnts)
  54. {
  55. while ((cnts & _QW_WMASK) == _QW_LOCKED) {
  56. cpu_relax();
  57. cnts = atomic_read_acquire(&lock->cnts);
  58. }
  59. }
  60. /**
  61. * queued_read_lock_slowpath - acquire read lock of a queue rwlock
  62. * @lock: Pointer to queue rwlock structure
  63. * @cnts: Current qrwlock lock value
  64. */
  65. void queued_read_lock_slowpath(struct qrwlock *lock, u32 cnts)
  66. {
  67. /*
  68. * Readers come here when they cannot get the lock without waiting
  69. */
  70. if (unlikely(in_interrupt())) {
  71. /*
  72. * Readers in interrupt context will get the lock immediately
  73. * if the writer is just waiting (not holding the lock yet).
  74. * The rspin_until_writer_unlock() function returns immediately
  75. * in this case. Otherwise, they will spin (with ACQUIRE
  76. * semantics) until the lock is available without waiting in
  77. * the queue.
  78. */
  79. rspin_until_writer_unlock(lock, cnts);
  80. return;
  81. }
  82. atomic_sub(_QR_BIAS, &lock->cnts);
  83. /*
  84. * Put the reader into the wait queue
  85. */
  86. arch_spin_lock(&lock->wait_lock);
  87. /*
  88. * The ACQUIRE semantics of the following spinning code ensure
  89. * that accesses can't leak upwards out of our subsequent critical
  90. * section in the case that the lock is currently held for write.
  91. */
  92. cnts = atomic_fetch_add_acquire(_QR_BIAS, &lock->cnts);
  93. rspin_until_writer_unlock(lock, cnts);
  94. /*
  95. * Signal the next one in queue to become queue head
  96. */
  97. arch_spin_unlock(&lock->wait_lock);
  98. }
  99. EXPORT_SYMBOL(queued_read_lock_slowpath);
  100. /**
  101. * queued_write_lock_slowpath - acquire write lock of a queue rwlock
  102. * @lock : Pointer to queue rwlock structure
  103. */
  104. void queued_write_lock_slowpath(struct qrwlock *lock)
  105. {
  106. u32 cnts;
  107. /* Put the writer into the wait queue */
  108. arch_spin_lock(&lock->wait_lock);
  109. /* Try to acquire the lock directly if no reader is present */
  110. if (!atomic_read(&lock->cnts) &&
  111. (atomic_cmpxchg_acquire(&lock->cnts, 0, _QW_LOCKED) == 0))
  112. goto unlock;
  113. /*
  114. * Set the waiting flag to notify readers that a writer is pending,
  115. * or wait for a previous writer to go away.
  116. */
  117. for (;;) {
  118. struct __qrwlock *l = (struct __qrwlock *)lock;
  119. if (!READ_ONCE(l->wmode) &&
  120. (cmpxchg_relaxed(&l->wmode, 0, _QW_WAITING) == 0))
  121. break;
  122. cpu_relax();
  123. }
  124. /* When no more readers, set the locked flag */
  125. for (;;) {
  126. cnts = atomic_read(&lock->cnts);
  127. if ((cnts == _QW_WAITING) &&
  128. (atomic_cmpxchg_acquire(&lock->cnts, _QW_WAITING,
  129. _QW_LOCKED) == _QW_WAITING))
  130. break;
  131. cpu_relax();
  132. }
  133. unlock:
  134. arch_spin_unlock(&lock->wait_lock);
  135. }
  136. EXPORT_SYMBOL(queued_write_lock_slowpath);