qrwlock.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 = smp_load_acquire((u32 *)&lock->cnts);
  57. }
  58. }
  59. /**
  60. * queue_read_lock_slowpath - acquire read lock of a queue rwlock
  61. * @lock: Pointer to queue rwlock structure
  62. */
  63. void queue_read_lock_slowpath(struct qrwlock *lock)
  64. {
  65. u32 cnts;
  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 spin until the lock is
  72. * available without waiting in the queue.
  73. */
  74. cnts = smp_load_acquire((u32 *)&lock->cnts);
  75. rspin_until_writer_unlock(lock, cnts);
  76. return;
  77. }
  78. atomic_sub(_QR_BIAS, &lock->cnts);
  79. /*
  80. * Put the reader into the wait queue
  81. */
  82. arch_spin_lock(&lock->lock);
  83. /*
  84. * At the head of the wait queue now, wait until the writer state
  85. * goes to 0 and then try to increment the reader count and get
  86. * the lock. It is possible that an incoming writer may steal the
  87. * lock in the interim, so it is necessary to check the writer byte
  88. * to make sure that the write lock isn't taken.
  89. */
  90. while (atomic_read(&lock->cnts) & _QW_WMASK)
  91. cpu_relax_lowlatency();
  92. cnts = atomic_add_return(_QR_BIAS, &lock->cnts) - _QR_BIAS;
  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->lock);
  98. }
  99. EXPORT_SYMBOL(queue_read_lock_slowpath);
  100. /**
  101. * queue_write_lock_slowpath - acquire write lock of a queue rwlock
  102. * @lock : Pointer to queue rwlock structure
  103. */
  104. void queue_write_lock_slowpath(struct qrwlock *lock)
  105. {
  106. u32 cnts;
  107. /* Put the writer into the wait queue */
  108. arch_spin_lock(&lock->lock);
  109. /* Try to acquire the lock directly if no reader is present */
  110. if (!atomic_read(&lock->cnts) &&
  111. (atomic_cmpxchg(&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(&l->wmode, 0, _QW_WAITING) == 0))
  121. break;
  122. cpu_relax_lowlatency();
  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(&lock->cnts, _QW_WAITING,
  129. _QW_LOCKED) == _QW_WAITING))
  130. break;
  131. cpu_relax_lowlatency();
  132. }
  133. unlock:
  134. arch_spin_unlock(&lock->lock);
  135. }
  136. EXPORT_SYMBOL(queue_write_lock_slowpath);