kref.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. WARN_ON(!atomic_read(&kref->refcount));
  39. atomic_inc(&kref->refcount);
  40. }
  41. /**
  42. * kref_sub - subtract a number of refcounts for object.
  43. * @kref: object.
  44. * @count: Number of recounts to subtract.
  45. * @release: pointer to the function that will clean up the object when the
  46. * last reference to the object is released.
  47. * This pointer is required, and it is not acceptable to pass kfree
  48. * in as this function. If the caller does pass kfree to this
  49. * function, you will be publicly mocked mercilessly by the kref
  50. * maintainer, and anyone else who happens to notice it. You have
  51. * been warned.
  52. *
  53. * Subtract @count from the refcount, and if 0, call release().
  54. * Return 1 if the object was removed, otherwise return 0. Beware, if this
  55. * function returns 0, you still can not count on the kref from remaining in
  56. * memory. Only use the return value if you want to see if the kref is now
  57. * gone, not present.
  58. */
  59. static inline int kref_sub(struct kref *kref, unsigned int count,
  60. void (*release)(struct kref *kref))
  61. {
  62. WARN_ON(release == NULL);
  63. if (atomic_sub_and_test((int) count, &kref->refcount)) {
  64. release(kref);
  65. return 1;
  66. }
  67. return 0;
  68. }
  69. /**
  70. * kref_put - decrement refcount for object.
  71. * @kref: object.
  72. * @release: pointer to the function that will clean up the object when the
  73. * last reference to the object is released.
  74. * This pointer is required, and it is not acceptable to pass kfree
  75. * in as this function. If the caller does pass kfree to this
  76. * function, you will be publicly mocked mercilessly by the kref
  77. * maintainer, and anyone else who happens to notice it. You have
  78. * been warned.
  79. *
  80. * Decrement the refcount, and if 0, call release().
  81. * Return 1 if the object was removed, otherwise return 0. Beware, if this
  82. * function returns 0, you still can not count on the kref from remaining in
  83. * memory. Only use the return value if you want to see if the kref is now
  84. * gone, not present.
  85. */
  86. static inline int kref_put(struct kref *kref, void (*release)(struct kref *kref))
  87. {
  88. return kref_sub(kref, 1, release);
  89. }
  90. /**
  91. * kref_put_spinlock_irqsave - decrement refcount for object.
  92. * @kref: object.
  93. * @release: pointer to the function that will clean up the object when the
  94. * last reference to the object is released.
  95. * This pointer is required, and it is not acceptable to pass kfree
  96. * in as this function.
  97. * @lock: lock to take in release case
  98. *
  99. * Behaves identical to kref_put with one exception. If the reference count
  100. * drops to zero, the lock will be taken atomically wrt dropping the reference
  101. * count. The release function has to call spin_unlock() without _irqrestore.
  102. */
  103. static inline int kref_put_spinlock_irqsave(struct kref *kref,
  104. void (*release)(struct kref *kref),
  105. spinlock_t *lock)
  106. {
  107. unsigned long flags;
  108. WARN_ON(release == NULL);
  109. if (atomic_add_unless(&kref->refcount, -1, 1))
  110. return 0;
  111. spin_lock_irqsave(lock, flags);
  112. if (atomic_dec_and_test(&kref->refcount)) {
  113. release(kref);
  114. local_irq_restore(flags);
  115. return 1;
  116. }
  117. spin_unlock_irqrestore(lock, flags);
  118. return 0;
  119. }
  120. static inline int kref_put_mutex(struct kref *kref,
  121. void (*release)(struct kref *kref),
  122. struct mutex *lock)
  123. {
  124. WARN_ON(release == NULL);
  125. if (unlikely(!atomic_add_unless(&kref->refcount, -1, 1))) {
  126. mutex_lock(lock);
  127. if (unlikely(!atomic_dec_and_test(&kref->refcount))) {
  128. mutex_unlock(lock);
  129. return 0;
  130. }
  131. release(kref);
  132. return 1;
  133. }
  134. return 0;
  135. }
  136. /**
  137. * kref_get_unless_zero - Increment refcount for object unless it is zero.
  138. * @kref: object.
  139. *
  140. * Return non-zero if the increment succeeded. Otherwise return 0.
  141. *
  142. * This function is intended to simplify locking around refcounting for
  143. * objects that can be looked up from a lookup structure, and which are
  144. * removed from that lookup structure in the object destructor.
  145. * Operations on such objects require at least a read lock around
  146. * lookup + kref_get, and a write lock around kref_put + remove from lookup
  147. * structure. Furthermore, RCU implementations become extremely tricky.
  148. * With a lookup followed by a kref_get_unless_zero *with return value check*
  149. * locking in the kref_put path can be deferred to the actual removal from
  150. * the lookup structure and RCU lookups become trivial.
  151. */
  152. static inline int __must_check kref_get_unless_zero(struct kref *kref)
  153. {
  154. return atomic_add_unless(&kref->refcount, 1, 0);
  155. }
  156. #endif /* _KREF_H_ */