kref.h 4.3 KB

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