kref.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. struct kref {
  21. atomic_t refcount;
  22. };
  23. #define KREF_INIT(n) { .refcount = ATOMIC_INIT(n), }
  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. static inline int kref_read(const struct kref *kref)
  33. {
  34. return atomic_read(&kref->refcount);
  35. }
  36. /**
  37. * kref_get - increment refcount for object.
  38. * @kref: object.
  39. */
  40. static inline void kref_get(struct kref *kref)
  41. {
  42. /* If refcount was 0 before incrementing then we have a race
  43. * condition when this kref is freeing by some other thread right now.
  44. * In this case one should use kref_get_unless_zero()
  45. */
  46. WARN_ON_ONCE(atomic_inc_return(&kref->refcount) < 2);
  47. }
  48. /**
  49. * kref_sub - subtract a number of refcounts for object.
  50. * @kref: object.
  51. * @count: Number of recounts to subtract.
  52. * @release: pointer to the function that will clean up the object when the
  53. * last reference to the object is released.
  54. * This pointer is required, and it is not acceptable to pass kfree
  55. * in as this function. If the caller does pass kfree to this
  56. * function, you will be publicly mocked mercilessly by the kref
  57. * maintainer, and anyone else who happens to notice it. You have
  58. * been warned.
  59. *
  60. * Subtract @count from the refcount, and if 0, call release().
  61. * Return 1 if the object was removed, otherwise return 0. Beware, if this
  62. * function returns 0, you still can not count on the kref from remaining in
  63. * memory. Only use the return value if you want to see if the kref is now
  64. * gone, not present.
  65. */
  66. static inline int kref_sub(struct kref *kref, unsigned int count,
  67. void (*release)(struct kref *kref))
  68. {
  69. WARN_ON(release == NULL);
  70. if (atomic_sub_and_test((int) count, &kref->refcount)) {
  71. release(kref);
  72. return 1;
  73. }
  74. return 0;
  75. }
  76. /**
  77. * kref_put - decrement refcount for object.
  78. * @kref: object.
  79. * @release: pointer to the function that will clean up the object when the
  80. * last reference to the object is released.
  81. * This pointer is required, and it is not acceptable to pass kfree
  82. * in as this function. If the caller does pass kfree to this
  83. * function, you will be publicly mocked mercilessly by the kref
  84. * maintainer, and anyone else who happens to notice it. You have
  85. * been warned.
  86. *
  87. * Decrement the refcount, and if 0, call release().
  88. * Return 1 if the object was removed, otherwise return 0. Beware, if this
  89. * function returns 0, you still can not count on the kref from remaining in
  90. * memory. Only use the return value if you want to see if the kref is now
  91. * gone, not present.
  92. */
  93. static inline int kref_put(struct kref *kref, void (*release)(struct kref *kref))
  94. {
  95. return kref_sub(kref, 1, release);
  96. }
  97. static inline int kref_put_mutex(struct kref *kref,
  98. void (*release)(struct kref *kref),
  99. struct mutex *lock)
  100. {
  101. WARN_ON(release == NULL);
  102. if (unlikely(!atomic_add_unless(&kref->refcount, -1, 1))) {
  103. mutex_lock(lock);
  104. if (unlikely(!atomic_dec_and_test(&kref->refcount))) {
  105. mutex_unlock(lock);
  106. return 0;
  107. }
  108. release(kref);
  109. return 1;
  110. }
  111. return 0;
  112. }
  113. /**
  114. * kref_get_unless_zero - Increment refcount for object unless it is zero.
  115. * @kref: object.
  116. *
  117. * Return non-zero if the increment succeeded. Otherwise return 0.
  118. *
  119. * This function is intended to simplify locking around refcounting for
  120. * objects that can be looked up from a lookup structure, and which are
  121. * removed from that lookup structure in the object destructor.
  122. * Operations on such objects require at least a read lock around
  123. * lookup + kref_get, and a write lock around kref_put + remove from lookup
  124. * structure. Furthermore, RCU implementations become extremely tricky.
  125. * With a lookup followed by a kref_get_unless_zero *with return value check*
  126. * locking in the kref_put path can be deferred to the actual removal from
  127. * the lookup structure and RCU lookups become trivial.
  128. */
  129. static inline int __must_check kref_get_unless_zero(struct kref *kref)
  130. {
  131. return atomic_add_unless(&kref->refcount, 1, 0);
  132. }
  133. #endif /* _KREF_H_ */