kref.txt 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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, 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. kref_put(&data->refcount, data_release);
  71. goto out;
  72. }
  73. .
  74. . do stuff with data here
  75. .
  76. out:
  77. kref_put(&data->refcount, data_release);
  78. return rv;
  79. }
  80. This way, it doesn't matter what order the two threads handle the
  81. data, the kref_put() handles knowing when the data is not referenced
  82. any more and releasing it. The kref_get() does not require a lock,
  83. since we already have a valid pointer that we own a refcount for. The
  84. put needs no lock because nothing tries to get the data without
  85. already holding a pointer.
  86. Note that the "before" in rule 1 is very important. You should never
  87. do something like:
  88. task = kthread_run(more_data_handling, data, "more_data_handling");
  89. if (task == ERR_PTR(-ENOMEM)) {
  90. rv = -ENOMEM;
  91. goto out;
  92. } else
  93. /* BAD BAD BAD - get is after the handoff */
  94. kref_get(&data->refcount);
  95. Don't assume you know what you are doing and use the above construct.
  96. First of all, you may not know what you are doing. Second, you may
  97. know what you are doing (there are some situations where locking is
  98. involved where the above may be legal) but someone else who doesn't
  99. know what they are doing may change the code or copy the code. It's
  100. bad style. Don't do it.
  101. There are some situations where you can optimize the gets and puts.
  102. For instance, if you are done with an object and enqueuing it for
  103. something else or passing it off to something else, there is no reason
  104. to do a get then a put:
  105. /* Silly extra get and put */
  106. kref_get(&obj->ref);
  107. enqueue(obj);
  108. kref_put(&obj->ref, obj_cleanup);
  109. Just do the enqueue. A comment about this is always welcome:
  110. enqueue(obj);
  111. /* We are done with obj, so we pass our refcount off
  112. to the queue. DON'T TOUCH obj AFTER HERE! */
  113. The last rule (rule 3) is the nastiest one to handle. Say, for
  114. instance, you have a list of items that are each kref-ed, and you wish
  115. to get the first one. You can't just pull the first item off the list
  116. and kref_get() it. That violates rule 3 because you are not already
  117. holding a valid pointer. You must add locks or semaphores. For
  118. instance:
  119. static DECLARE_MUTEX(sem);
  120. static LIST_HEAD(q);
  121. struct my_data
  122. {
  123. struct kref refcount;
  124. struct list_head link;
  125. };
  126. static struct my_data *get_entry()
  127. {
  128. struct my_data *entry = NULL;
  129. down(&sem);
  130. if (!list_empty(&q)) {
  131. entry = container_of(q.next, struct my_q_entry, link);
  132. kref_get(&entry->refcount);
  133. }
  134. up(&sem);
  135. return entry;
  136. }
  137. static void release_entry(struct kref *ref)
  138. {
  139. struct my_data *entry = container_of(ref, struct my_data, refcount);
  140. list_del(&entry->link);
  141. kfree(entry);
  142. }
  143. static void put_entry(struct my_data *entry)
  144. {
  145. down(&sem);
  146. kref_put(&entry->refcount, release_entry);
  147. up(&sem);
  148. }
  149. The kref_put() return value is useful if you do not want to hold the
  150. lock during the whole release operation. Say you didn't want to call
  151. kfree() with the lock held in the example above (since it is kind of
  152. pointless to do so). You could use kref_put() as follows:
  153. static void release_entry(struct kref *ref)
  154. {
  155. /* All work is done after the return from kref_put(). */
  156. }
  157. static void put_entry(struct my_data *entry)
  158. {
  159. down(&sem);
  160. if (kref_put(&entry->refcount, release_entry)) {
  161. list_del(&entry->link);
  162. up(&sem);
  163. kfree(entry);
  164. } else
  165. up(&sem);
  166. }
  167. This is really more useful if you have to call other routines as part
  168. of the free operations that could take a long time or might claim the
  169. same lock. Note that doing everything in the release routine is still
  170. preferred as it is a little neater.
  171. Corey Minyard <minyard@acm.org>
  172. A lot of this was lifted from Greg Kroah-Hartman's 2004 OLS paper and
  173. presentation on krefs, which can be found at:
  174. http://www.kroah.com/linux/talks/ols_2004_kref_paper/Reprint-Kroah-Hartman-OLS2004.pdf
  175. and:
  176. http://www.kroah.com/linux/talks/ols_2004_kref_talk/