kref.txt 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. krefs allow you to add reference counters to your objects. If you
  2. have objects that are used in multiple places and passed around, and
  3. you don't have refcounts, your code is almost certainly broken. If
  4. you want refcounts, krefs are the way to go.
  5. To use a kref, add one to your data structures like:
  6. struct my_data
  7. {
  8. .
  9. .
  10. struct kref refcount;
  11. .
  12. .
  13. };
  14. The kref can occur anywhere within the data structure.
  15. You must initialize the kref after you allocate it. To do this, call
  16. kref_init as so:
  17. struct my_data *data;
  18. data = kmalloc(sizeof(*data), GFP_KERNEL);
  19. if (!data)
  20. return -ENOMEM;
  21. kref_init(&data->refcount);
  22. This sets the refcount in the kref to 1.
  23. Once you have an initialized kref, you must follow the following
  24. rules:
  25. 1) If you make a non-temporary copy of a pointer, especially if
  26. it can be passed to another thread of execution, you must
  27. increment the refcount with kref_get() before passing it off:
  28. kref_get(&data->refcount);
  29. If you already have a valid pointer to a kref-ed structure (the
  30. refcount cannot go to zero) you may do this without a lock.
  31. 2) When you are done with a pointer, you must call kref_put():
  32. kref_put(&data->refcount, data_release);
  33. If this is the last reference to the pointer, the release
  34. routine will be called. If the code never tries to get
  35. a valid pointer to a kref-ed structure without already
  36. holding a valid pointer, it is safe to do this without
  37. a lock.
  38. 3) If the code attempts to gain a reference to a kref-ed structure
  39. without already holding a valid pointer, it must serialize access
  40. where a kref_put() cannot occur during the kref_get(), and the
  41. structure must remain valid during the kref_get().
  42. For example, if you allocate some data and then pass it to another
  43. thread to process:
  44. void data_release(struct kref *ref)
  45. {
  46. struct my_data *data = container_of(ref, struct my_data, refcount);
  47. kfree(data);
  48. }
  49. void more_data_handling(void *cb_data)
  50. {
  51. struct my_data *data = cb_data;
  52. .
  53. . do stuff with data here
  54. .
  55. kref_put(&data->refcount, data_release);
  56. }
  57. int my_data_handler(void)
  58. {
  59. int rv = 0;
  60. struct my_data *data;
  61. struct task_struct *task;
  62. data = kmalloc(sizeof(*data), GFP_KERNEL);
  63. if (!data)
  64. return -ENOMEM;
  65. kref_init(&data->refcount);
  66. kref_get(&data->refcount);
  67. task = kthread_run(more_data_handling, data, "more_data_handling");
  68. if (task == ERR_PTR(-ENOMEM)) {
  69. rv = -ENOMEM;
  70. goto out;
  71. }
  72. .
  73. . do stuff with data here
  74. .
  75. out:
  76. kref_put(&data->refcount, data_release);
  77. return rv;
  78. }
  79. This way, it doesn't matter what order the two threads handle the
  80. data, the kref_put() handles knowing when the data is not referenced
  81. any more and releasing it. The kref_get() does not require a lock,
  82. since we already have a valid pointer that we own a refcount for. The
  83. put needs no lock because nothing tries to get the data without
  84. already holding a pointer.
  85. Note that the "before" in rule 1 is very important. You should never
  86. do something like:
  87. task = kthread_run(more_data_handling, data, "more_data_handling");
  88. if (task == ERR_PTR(-ENOMEM)) {
  89. rv = -ENOMEM;
  90. goto out;
  91. } else
  92. /* BAD BAD BAD - get is after the handoff */
  93. kref_get(&data->refcount);
  94. Don't assume you know what you are doing and use the above construct.
  95. First of all, you may not know what you are doing. Second, you may
  96. know what you are doing (there are some situations where locking is
  97. involved where the above may be legal) but someone else who doesn't
  98. know what they are doing may change the code or copy the code. It's
  99. bad style. Don't do it.
  100. There are some situations where you can optimize the gets and puts.
  101. For instance, if you are done with an object and enqueuing it for
  102. something else or passing it off to something else, there is no reason
  103. to do a get then a put:
  104. /* Silly extra get and put */
  105. kref_get(&obj->ref);
  106. enqueue(obj);
  107. kref_put(&obj->ref, obj_cleanup);
  108. Just do the enqueue. A comment about this is always welcome:
  109. enqueue(obj);
  110. /* We are done with obj, so we pass our refcount off
  111. to the queue. DON'T TOUCH obj AFTER HERE! */
  112. The last rule (rule 3) is the nastiest one to handle. Say, for
  113. instance, you have a list of items that are each kref-ed, and you wish
  114. to get the first one. You can't just pull the first item off the list
  115. and kref_get() it. That violates rule 3 because you are not already
  116. holding a valid pointer. You must add a mutex (or some other lock).
  117. For instance:
  118. static DEFINE_MUTEX(mutex);
  119. static LIST_HEAD(q);
  120. struct my_data
  121. {
  122. struct kref refcount;
  123. struct list_head link;
  124. };
  125. static struct my_data *get_entry()
  126. {
  127. struct my_data *entry = NULL;
  128. mutex_lock(&mutex);
  129. if (!list_empty(&q)) {
  130. entry = container_of(q.next, struct my_data, link);
  131. kref_get(&entry->refcount);
  132. }
  133. mutex_unlock(&mutex);
  134. return entry;
  135. }
  136. static void release_entry(struct kref *ref)
  137. {
  138. struct my_data *entry = container_of(ref, struct my_data, refcount);
  139. list_del(&entry->link);
  140. kfree(entry);
  141. }
  142. static void put_entry(struct my_data *entry)
  143. {
  144. mutex_lock(&mutex);
  145. kref_put(&entry->refcount, release_entry);
  146. mutex_unlock(&mutex);
  147. }
  148. The kref_put() return value is useful if you do not want to hold the
  149. lock during the whole release operation. Say you didn't want to call
  150. kfree() with the lock held in the example above (since it is kind of
  151. pointless to do so). You could use kref_put() as follows:
  152. static void release_entry(struct kref *ref)
  153. {
  154. /* All work is done after the return from kref_put(). */
  155. }
  156. static void put_entry(struct my_data *entry)
  157. {
  158. mutex_lock(&mutex);
  159. if (kref_put(&entry->refcount, release_entry)) {
  160. list_del(&entry->link);
  161. mutex_unlock(&mutex);
  162. kfree(entry);
  163. } else
  164. mutex_unlock(&mutex);
  165. }
  166. This is really more useful if you have to call other routines as part
  167. of the free operations that could take a long time or might claim the
  168. same lock. Note that doing everything in the release routine is still
  169. preferred as it is a little neater.
  170. Corey Minyard <minyard@acm.org>
  171. A lot of this was lifted from Greg Kroah-Hartman's 2004 OLS paper and
  172. presentation on krefs, which can be found at:
  173. http://www.kroah.com/linux/talks/ols_2004_kref_paper/Reprint-Kroah-Hartman-OLS2004.pdf
  174. and:
  175. http://www.kroah.com/linux/talks/ols_2004_kref_talk/
  176. The above example could also be optimized using kref_get_unless_zero() in
  177. the following way:
  178. static struct my_data *get_entry()
  179. {
  180. struct my_data *entry = NULL;
  181. mutex_lock(&mutex);
  182. if (!list_empty(&q)) {
  183. entry = container_of(q.next, struct my_data, link);
  184. if (!kref_get_unless_zero(&entry->refcount))
  185. entry = NULL;
  186. }
  187. mutex_unlock(&mutex);
  188. return entry;
  189. }
  190. static void release_entry(struct kref *ref)
  191. {
  192. struct my_data *entry = container_of(ref, struct my_data, refcount);
  193. mutex_lock(&mutex);
  194. list_del(&entry->link);
  195. mutex_unlock(&mutex);
  196. kfree(entry);
  197. }
  198. static void put_entry(struct my_data *entry)
  199. {
  200. kref_put(&entry->refcount, release_entry);
  201. }
  202. Which is useful to remove the mutex lock around kref_put() in put_entry(), but
  203. it's important that kref_get_unless_zero is enclosed in the same critical
  204. section that finds the entry in the lookup table,
  205. otherwise kref_get_unless_zero may reference already freed memory.
  206. Note that it is illegal to use kref_get_unless_zero without checking its
  207. return value. If you are sure (by already having a valid pointer) that
  208. kref_get_unless_zero() will return true, then use kref_get() instead.
  209. The function kref_get_unless_zero also makes it possible to use rcu
  210. locking for lookups in the above example:
  211. struct my_data
  212. {
  213. struct rcu_head rhead;
  214. .
  215. struct kref refcount;
  216. .
  217. .
  218. };
  219. static struct my_data *get_entry_rcu()
  220. {
  221. struct my_data *entry = NULL;
  222. rcu_read_lock();
  223. if (!list_empty(&q)) {
  224. entry = container_of(q.next, struct my_data, link);
  225. if (!kref_get_unless_zero(&entry->refcount))
  226. entry = NULL;
  227. }
  228. rcu_read_unlock();
  229. return entry;
  230. }
  231. static void release_entry_rcu(struct kref *ref)
  232. {
  233. struct my_data *entry = container_of(ref, struct my_data, refcount);
  234. mutex_lock(&mutex);
  235. list_del_rcu(&entry->link);
  236. mutex_unlock(&mutex);
  237. kfree_rcu(entry, rhead);
  238. }
  239. static void put_entry(struct my_data *entry)
  240. {
  241. kref_put(&entry->refcount, release_entry_rcu);
  242. }
  243. But note that the struct kref member needs to remain in valid memory for a
  244. rcu grace period after release_entry_rcu was called. That can be accomplished
  245. by using kfree_rcu(entry, rhead) as done above, or by calling synchronize_rcu()
  246. before using kfree, but note that synchronize_rcu() may sleep for a
  247. substantial amount of time.
  248. Thomas Hellstrom <thellstrom@vmware.com>