kref.h 4.2 KB

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