kref.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * kref.h - library routines for handling generic reference counted objects
  3. *
  4. * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
  5. * Copyright (C) 2004 IBM Corp.
  6. *
  7. * based on kobject.h which was:
  8. * Copyright (C) 2002-2003 Patrick Mochel <mochel@osdl.org>
  9. * Copyright (C) 2002-2003 Open Source Development Labs
  10. *
  11. * This file is released under the GPLv2.
  12. *
  13. */
  14. #ifndef _KREF_H_
  15. #define _KREF_H_
  16. #include <linux/bug.h>
  17. #include <linux/atomic.h>
  18. #include <linux/kernel.h>
  19. #include <linux/mutex.h>
  20. #include <linux/spinlock.h>
  21. struct kref {
  22. atomic_t refcount;
  23. };
  24. /**
  25. * kref_init - initialize object.
  26. * @kref: object in question.
  27. */
  28. static inline void kref_init(struct kref *kref)
  29. {
  30. atomic_set(&kref->refcount, 1);
  31. }
  32. /**
  33. * kref_get - increment refcount for object.
  34. * @kref: object.
  35. */
  36. static inline void kref_get(struct kref *kref)
  37. {
  38. /* If refcount was 0 before incrementing then we have a race
  39. * condition when this kref is freeing by some other thread right now.
  40. * In this case one should use kref_get_unless_zero()
  41. */
  42. WARN_ON_ONCE(atomic_inc_return(&kref->refcount) < 2);
  43. }
  44. /**
  45. * kref_sub - subtract a number of refcounts for object.
  46. * @kref: object.
  47. * @count: Number of recounts to subtract.
  48. * @release: pointer to the function that will clean up the object when the
  49. * last reference to the object is released.
  50. * This pointer is required, and it is not acceptable to pass kfree
  51. * in as this function. If the caller does pass kfree to this
  52. * function, you will be publicly mocked mercilessly by the kref
  53. * maintainer, and anyone else who happens to notice it. You have
  54. * been warned.
  55. *
  56. * Subtract @count from the refcount, and if 0, call release().
  57. * Return 1 if the object was removed, otherwise return 0. Beware, if this
  58. * function returns 0, you still can not count on the kref from remaining in
  59. * memory. Only use the return value if you want to see if the kref is now
  60. * gone, not present.
  61. */
  62. static inline int kref_sub(struct kref *kref, unsigned int count,
  63. void (*release)(struct kref *kref))
  64. {
  65. WARN_ON(release == NULL);
  66. if (atomic_sub_and_test((int) count, &kref->refcount)) {
  67. release(kref);
  68. return 1;
  69. }
  70. return 0;
  71. }
  72. /**
  73. * kref_put - decrement refcount for object.
  74. * @kref: object.
  75. * @release: pointer to the function that will clean up the object when the
  76. * last reference to the object is released.
  77. * This pointer is required, and it is not acceptable to pass kfree
  78. * in as this function. If the caller does pass kfree to this
  79. * function, you will be publicly mocked mercilessly by the kref
  80. * maintainer, and anyone else who happens to notice it. You have
  81. * been warned.
  82. *
  83. * Decrement the refcount, and if 0, call release().
  84. * Return 1 if the object was removed, otherwise return 0. Beware, if this
  85. * function returns 0, you still can not count on the kref from remaining in
  86. * memory. Only use the return value if you want to see if the kref is now
  87. * gone, not present.
  88. */
  89. static inline int kref_put(struct kref *kref, void (*release)(struct kref *kref))
  90. {
  91. return kref_sub(kref, 1, release);
  92. }
  93. /**
  94. * kref_put_spinlock_irqsave - decrement refcount for object.
  95. * @kref: object.
  96. * @release: pointer to the function that will clean up the object when the
  97. * last reference to the object is released.
  98. * This pointer is required, and it is not acceptable to pass kfree
  99. * in as this function.
  100. * @lock: lock to take in release case
  101. *
  102. * Behaves identical to kref_put with one exception. If the reference count
  103. * drops to zero, the lock will be taken atomically wrt dropping the reference
  104. * count. The release function has to call spin_unlock() without _irqrestore.
  105. */
  106. static inline int kref_put_spinlock_irqsave(struct kref *kref,
  107. void (*release)(struct kref *kref),
  108. spinlock_t *lock)
  109. {
  110. unsigned long flags;
  111. WARN_ON(release == NULL);
  112. if (atomic_add_unless(&kref->refcount, -1, 1))
  113. return 0;
  114. spin_lock_irqsave(lock, flags);
  115. if (atomic_dec_and_test(&kref->refcount)) {
  116. release(kref);
  117. local_irq_restore(flags);
  118. return 1;
  119. }
  120. spin_unlock_irqrestore(lock, flags);
  121. return 0;
  122. }
  123. static inline int kref_put_mutex(struct kref *kref,
  124. void (*release)(struct kref *kref),
  125. struct mutex *lock)
  126. {
  127. WARN_ON(release == NULL);
  128. if (unlikely(!atomic_add_unless(&kref->refcount, -1, 1))) {
  129. mutex_lock(lock);
  130. if (unlikely(!atomic_dec_and_test(&kref->refcount))) {
  131. mutex_unlock(lock);
  132. return 0;
  133. }
  134. release(kref);
  135. return 1;
  136. }
  137. return 0;
  138. }
  139. /**
  140. * kref_get_unless_zero - Increment refcount for object unless it is zero.
  141. * @kref: object.
  142. *
  143. * Return non-zero if the increment succeeded. Otherwise return 0.
  144. *
  145. * This function is intended to simplify locking around refcounting for
  146. * objects that can be looked up from a lookup structure, and which are
  147. * removed from that lookup structure in the object destructor.
  148. * Operations on such objects require at least a read lock around
  149. * lookup + kref_get, and a write lock around kref_put + remove from lookup
  150. * structure. Furthermore, RCU implementations become extremely tricky.
  151. * With a lookup followed by a kref_get_unless_zero *with return value check*
  152. * locking in the kref_put path can be deferred to the actual removal from
  153. * the lookup structure and RCU lookups become trivial.
  154. */
  155. static inline int __must_check kref_get_unless_zero(struct kref *kref)
  156. {
  157. return atomic_add_unless(&kref->refcount, 1, 0);
  158. }
  159. #endif /* _KREF_H_ */