qrwlock.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Queue read/write lock
  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/mutex.h>
  24. #include <asm/qrwlock.h>
  25. /**
  26. * rspin_until_writer_unlock - inc reader count & spin until writer is gone
  27. * @lock : Pointer to queue rwlock structure
  28. * @writer: Current queue rwlock writer status byte
  29. *
  30. * In interrupt context or at the head of the queue, the reader will just
  31. * increment the reader count & wait until the writer releases the lock.
  32. */
  33. static __always_inline void
  34. rspin_until_writer_unlock(struct qrwlock *lock, u32 cnts)
  35. {
  36. while ((cnts & _QW_WMASK) == _QW_LOCKED) {
  37. arch_mutex_cpu_relax();
  38. cnts = smp_load_acquire((u32 *)&lock->cnts);
  39. }
  40. }
  41. /**
  42. * queue_read_lock_slowpath - acquire read lock of a queue rwlock
  43. * @lock: Pointer to queue rwlock structure
  44. */
  45. void queue_read_lock_slowpath(struct qrwlock *lock)
  46. {
  47. u32 cnts;
  48. /*
  49. * Readers come here when they cannot get the lock without waiting
  50. */
  51. if (unlikely(in_interrupt())) {
  52. /*
  53. * Readers in interrupt context will spin until the lock is
  54. * available without waiting in the queue.
  55. */
  56. cnts = smp_load_acquire((u32 *)&lock->cnts);
  57. rspin_until_writer_unlock(lock, cnts);
  58. return;
  59. }
  60. atomic_sub(_QR_BIAS, &lock->cnts);
  61. /*
  62. * Put the reader into the wait queue
  63. */
  64. arch_spin_lock(&lock->lock);
  65. /*
  66. * At the head of the wait queue now, wait until the writer state
  67. * goes to 0 and then try to increment the reader count and get
  68. * the lock. It is possible that an incoming writer may steal the
  69. * lock in the interim, so it is necessary to check the writer byte
  70. * to make sure that the write lock isn't taken.
  71. */
  72. while (atomic_read(&lock->cnts) & _QW_WMASK)
  73. arch_mutex_cpu_relax();
  74. cnts = atomic_add_return(_QR_BIAS, &lock->cnts) - _QR_BIAS;
  75. rspin_until_writer_unlock(lock, cnts);
  76. /*
  77. * Signal the next one in queue to become queue head
  78. */
  79. arch_spin_unlock(&lock->lock);
  80. }
  81. EXPORT_SYMBOL(queue_read_lock_slowpath);
  82. /**
  83. * queue_write_lock_slowpath - acquire write lock of a queue rwlock
  84. * @lock : Pointer to queue rwlock structure
  85. */
  86. void queue_write_lock_slowpath(struct qrwlock *lock)
  87. {
  88. u32 cnts;
  89. /* Put the writer into the wait queue */
  90. arch_spin_lock(&lock->lock);
  91. /* Try to acquire the lock directly if no reader is present */
  92. if (!atomic_read(&lock->cnts) &&
  93. (atomic_cmpxchg(&lock->cnts, 0, _QW_LOCKED) == 0))
  94. goto unlock;
  95. /*
  96. * Set the waiting flag to notify readers that a writer is pending,
  97. * or wait for a previous writer to go away.
  98. */
  99. for (;;) {
  100. cnts = atomic_read(&lock->cnts);
  101. if (!(cnts & _QW_WMASK) &&
  102. (atomic_cmpxchg(&lock->cnts, cnts,
  103. cnts | _QW_WAITING) == cnts))
  104. break;
  105. arch_mutex_cpu_relax();
  106. }
  107. /* When no more readers, set the locked flag */
  108. for (;;) {
  109. cnts = atomic_read(&lock->cnts);
  110. if ((cnts == _QW_WAITING) &&
  111. (atomic_cmpxchg(&lock->cnts, _QW_WAITING,
  112. _QW_LOCKED) == _QW_WAITING))
  113. break;
  114. arch_mutex_cpu_relax();
  115. }
  116. unlock:
  117. arch_spin_unlock(&lock->lock);
  118. }
  119. EXPORT_SYMBOL(queue_write_lock_slowpath);