lockref.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/export.h>
  3. #include <linux/lockref.h>
  4. #if USE_CMPXCHG_LOCKREF
  5. /*
  6. * Note that the "cmpxchg()" reloads the "old" value for the
  7. * failure case.
  8. */
  9. #define CMPXCHG_LOOP(CODE, SUCCESS) do { \
  10. int retry = 100; \
  11. struct lockref old; \
  12. BUILD_BUG_ON(sizeof(old) != 8); \
  13. old.lock_count = READ_ONCE(lockref->lock_count); \
  14. while (likely(arch_spin_value_unlocked(old.lock.rlock.raw_lock))) { \
  15. struct lockref new = old; \
  16. CODE \
  17. if (likely(try_cmpxchg64_relaxed(&lockref->lock_count, \
  18. &old.lock_count, \
  19. new.lock_count))) { \
  20. SUCCESS; \
  21. } \
  22. if (!--retry) \
  23. break; \
  24. } \
  25. } while (0)
  26. #else
  27. #define CMPXCHG_LOOP(CODE, SUCCESS) do { } while (0)
  28. #endif
  29. /**
  30. * lockref_get - Increments reference count unconditionally
  31. * @lockref: pointer to lockref structure
  32. *
  33. * This operation is only valid if you already hold a reference
  34. * to the object, so you know the count cannot be zero.
  35. */
  36. void lockref_get(struct lockref *lockref)
  37. {
  38. CMPXCHG_LOOP(
  39. new.count++;
  40. ,
  41. return;
  42. );
  43. spin_lock(&lockref->lock);
  44. lockref->count++;
  45. spin_unlock(&lockref->lock);
  46. }
  47. EXPORT_SYMBOL(lockref_get);
  48. /**
  49. * lockref_get_not_zero - Increments count unless the count is 0 or dead
  50. * @lockref: pointer to lockref structure
  51. * Return: 1 if count updated successfully or 0 if count was zero
  52. */
  53. bool lockref_get_not_zero(struct lockref *lockref)
  54. {
  55. bool retval = false;
  56. CMPXCHG_LOOP(
  57. new.count++;
  58. if (old.count <= 0)
  59. return false;
  60. ,
  61. return true;
  62. );
  63. spin_lock(&lockref->lock);
  64. if (lockref->count > 0) {
  65. lockref->count++;
  66. retval = true;
  67. }
  68. spin_unlock(&lockref->lock);
  69. return retval;
  70. }
  71. EXPORT_SYMBOL(lockref_get_not_zero);
  72. /**
  73. * lockref_put_return - Decrement reference count if possible
  74. * @lockref: pointer to lockref structure
  75. *
  76. * Decrement the reference count and return the new value.
  77. * If the lockref was dead or locked, return -1.
  78. */
  79. int lockref_put_return(struct lockref *lockref)
  80. {
  81. CMPXCHG_LOOP(
  82. new.count--;
  83. if (old.count <= 0)
  84. return -1;
  85. ,
  86. return new.count;
  87. );
  88. return -1;
  89. }
  90. EXPORT_SYMBOL(lockref_put_return);
  91. /**
  92. * lockref_put_or_lock - decrements count unless count <= 1 before decrement
  93. * @lockref: pointer to lockref structure
  94. * Return: 1 if count updated successfully or 0 if count <= 1 and lock taken
  95. */
  96. bool lockref_put_or_lock(struct lockref *lockref)
  97. {
  98. CMPXCHG_LOOP(
  99. new.count--;
  100. if (old.count <= 1)
  101. break;
  102. ,
  103. return true;
  104. );
  105. spin_lock(&lockref->lock);
  106. if (lockref->count <= 1)
  107. return false;
  108. lockref->count--;
  109. spin_unlock(&lockref->lock);
  110. return true;
  111. }
  112. EXPORT_SYMBOL(lockref_put_or_lock);
  113. /**
  114. * lockref_mark_dead - mark lockref dead
  115. * @lockref: pointer to lockref structure
  116. */
  117. void lockref_mark_dead(struct lockref *lockref)
  118. {
  119. assert_spin_locked(&lockref->lock);
  120. lockref->count = -128;
  121. }
  122. EXPORT_SYMBOL(lockref_mark_dead);
  123. /**
  124. * lockref_get_not_dead - Increments count unless the ref is dead
  125. * @lockref: pointer to lockref structure
  126. * Return: 1 if count updated successfully or 0 if lockref was dead
  127. */
  128. bool lockref_get_not_dead(struct lockref *lockref)
  129. {
  130. bool retval = false;
  131. CMPXCHG_LOOP(
  132. new.count++;
  133. if (old.count < 0)
  134. return false;
  135. ,
  136. return true;
  137. );
  138. spin_lock(&lockref->lock);
  139. if (lockref->count >= 0) {
  140. lockref->count++;
  141. retval = true;
  142. }
  143. spin_unlock(&lockref->lock);
  144. return retval;
  145. }
  146. EXPORT_SYMBOL(lockref_get_not_dead);