1 ===================================================
2 Adding reference counters (krefs) to kernel objects
3 ===================================================
5 :Author: Corey Minyard <minyard@acm.org>
6 :Author: Thomas Hellstrom <thellstrom@vmware.com>
8 A lot of this was lifted from Greg Kroah-Hartman's 2004 OLS paper and
9 presentation on krefs, which can be found at:
11 - http://www.kroah.com/linux/talks/ols_2004_kref_paper/Reprint-Kroah-Hartman-OLS2004.pdf
12 - http://www.kroah.com/linux/talks/ols_2004_kref_talk/
17 krefs allow you to add reference counters to your objects. If you
18 have objects that are used in multiple places and passed around, and
19 you don't have refcounts, your code is almost certainly broken. If
20 you want refcounts, krefs are the way to go.
22 To use a kref, add one to your data structures like::
33 The kref can occur anywhere within the data structure.
38 You must initialize the kref after you allocate it. To do this, call
43 data = kmalloc(sizeof(*data), GFP_KERNEL);
46 kref_init(&data->refcount);
48 This sets the refcount in the kref to 1.
53 Once you have an initialized kref, you must follow the following
56 1) If you make a non-temporary copy of a pointer, especially if
57 it can be passed to another thread of execution, you must
58 increment the refcount with kref_get() before passing it off::
60 kref_get(&data->refcount);
62 If you already have a valid pointer to a kref-ed structure (the
63 refcount cannot go to zero) you may do this without a lock.
65 2) When you are done with a pointer, you must call kref_put()::
67 kref_put(&data->refcount, data_release);
69 If this is the last reference to the pointer, the release
70 routine will be called. If the code never tries to get
71 a valid pointer to a kref-ed structure without already
72 holding a valid pointer, it is safe to do this without
75 3) If the code attempts to gain a reference to a kref-ed structure
76 without already holding a valid pointer, it must serialize access
77 where a kref_put() cannot occur during the kref_get(), and the
78 structure must remain valid during the kref_get().
80 For example, if you allocate some data and then pass it to another
83 void data_release(struct kref *ref)
85 struct my_data *data = container_of(ref, struct my_data, refcount);
89 void more_data_handling(void *cb_data)
91 struct my_data *data = cb_data;
93 . do stuff with data here
95 kref_put(&data->refcount, data_release);
98 int my_data_handler(void)
101 struct my_data *data;
102 struct task_struct *task;
103 data = kmalloc(sizeof(*data), GFP_KERNEL);
106 kref_init(&data->refcount);
108 kref_get(&data->refcount);
109 task = kthread_run(more_data_handling, data, "more_data_handling");
110 if (task == ERR_PTR(-ENOMEM)) {
112 kref_put(&data->refcount, data_release);
117 . do stuff with data here
120 kref_put(&data->refcount, data_release);
124 This way, it doesn't matter what order the two threads handle the
125 data, the kref_put() handles knowing when the data is not referenced
126 any more and releasing it. The kref_get() does not require a lock,
127 since we already have a valid pointer that we own a refcount for. The
128 put needs no lock because nothing tries to get the data without
129 already holding a pointer.
131 In the above example, kref_put() will be called 2 times in both success
132 and error paths. This is necessary because the reference count got
133 incremented 2 times by kref_init() and kref_get().
135 Note that the "before" in rule 1 is very important. You should never
138 task = kthread_run(more_data_handling, data, "more_data_handling");
139 if (task == ERR_PTR(-ENOMEM)) {
143 /* BAD BAD BAD - get is after the handoff */
144 kref_get(&data->refcount);
146 Don't assume you know what you are doing and use the above construct.
147 First of all, you may not know what you are doing. Second, you may
148 know what you are doing (there are some situations where locking is
149 involved where the above may be legal) but someone else who doesn't
150 know what they are doing may change the code or copy the code. It's
151 bad style. Don't do it.
153 There are some situations where you can optimize the gets and puts.
154 For instance, if you are done with an object and enqueuing it for
155 something else or passing it off to something else, there is no reason
156 to do a get then a put::
158 /* Silly extra get and put */
161 kref_put(&obj->ref, obj_cleanup);
163 Just do the enqueue. A comment about this is always welcome::
166 /* We are done with obj, so we pass our refcount off
167 to the queue. DON'T TOUCH obj AFTER HERE! */
169 The last rule (rule 3) is the nastiest one to handle. Say, for
170 instance, you have a list of items that are each kref-ed, and you wish
171 to get the first one. You can't just pull the first item off the list
172 and kref_get() it. That violates rule 3 because you are not already
173 holding a valid pointer. You must add a mutex (or some other lock).
176 static DEFINE_MUTEX(mutex);
180 struct kref refcount;
181 struct list_head link;
184 static struct my_data *get_entry()
186 struct my_data *entry = NULL;
188 if (!list_empty(&q)) {
189 entry = container_of(q.next, struct my_data, link);
190 kref_get(&entry->refcount);
192 mutex_unlock(&mutex);
196 static void release_entry(struct kref *ref)
198 struct my_data *entry = container_of(ref, struct my_data, refcount);
200 list_del(&entry->link);
204 static void put_entry(struct my_data *entry)
207 kref_put(&entry->refcount, release_entry);
208 mutex_unlock(&mutex);
211 The kref_put() return value is useful if you do not want to hold the
212 lock during the whole release operation. Say you didn't want to call
213 kfree() with the lock held in the example above (since it is kind of
214 pointless to do so). You could use kref_put() as follows::
216 static void release_entry(struct kref *ref)
218 /* All work is done after the return from kref_put(). */
221 static void put_entry(struct my_data *entry)
224 if (kref_put(&entry->refcount, release_entry)) {
225 list_del(&entry->link);
226 mutex_unlock(&mutex);
229 mutex_unlock(&mutex);
232 This is really more useful if you have to call other routines as part
233 of the free operations that could take a long time or might claim the
234 same lock. Note that doing everything in the release routine is still
235 preferred as it is a little neater.
237 The above example could also be optimized using kref_get_unless_zero() in
240 static struct my_data *get_entry()
242 struct my_data *entry = NULL;
244 if (!list_empty(&q)) {
245 entry = container_of(q.next, struct my_data, link);
246 if (!kref_get_unless_zero(&entry->refcount))
249 mutex_unlock(&mutex);
253 static void release_entry(struct kref *ref)
255 struct my_data *entry = container_of(ref, struct my_data, refcount);
258 list_del(&entry->link);
259 mutex_unlock(&mutex);
263 static void put_entry(struct my_data *entry)
265 kref_put(&entry->refcount, release_entry);
268 Which is useful to remove the mutex lock around kref_put() in put_entry(), but
269 it's important that kref_get_unless_zero is enclosed in the same critical
270 section that finds the entry in the lookup table,
271 otherwise kref_get_unless_zero may reference already freed memory.
272 Note that it is illegal to use kref_get_unless_zero without checking its
273 return value. If you are sure (by already having a valid pointer) that
274 kref_get_unless_zero() will return true, then use kref_get() instead.
279 The function kref_get_unless_zero also makes it possible to use rcu
280 locking for lookups in the above example::
284 struct rcu_head rhead;
286 struct kref refcount;
291 static struct my_data *get_entry_rcu()
293 struct my_data *entry = NULL;
295 if (!list_empty(&q)) {
296 entry = container_of(q.next, struct my_data, link);
297 if (!kref_get_unless_zero(&entry->refcount))
304 static void release_entry_rcu(struct kref *ref)
306 struct my_data *entry = container_of(ref, struct my_data, refcount);
309 list_del_rcu(&entry->link);
310 mutex_unlock(&mutex);
311 kfree_rcu(entry, rhead);
314 static void put_entry(struct my_data *entry)
316 kref_put(&entry->refcount, release_entry_rcu);
319 But note that the struct kref member needs to remain in valid memory for a
320 rcu grace period after release_entry_rcu was called. That can be accomplished
321 by using kfree_rcu(entry, rhead) as done above, or by calling synchronize_rcu()
322 before using kfree, but note that synchronize_rcu() may sleep for a
323 substantial amount of time.