blob: bbdd3015c2c787430acfb64f1802919619da6399 [file] [log] [blame]
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -08001/*
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -07002 * klist.c - Routines for manipulating klists.
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -08003 *
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -07004 * Copyright (C) 2005 Patrick Mochel
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -08005 *
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -07006 * This file is released under the GPL v2.
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -08007 *
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -07008 * This klist interface provides a couple of structures that wrap around
9 * struct list_head to provide explicit list "head" (struct klist) and list
10 * "node" (struct klist_node) objects. For struct klist, a spinlock is
11 * included that protects access to the actual list itself. struct
12 * klist_node provides a pointer to the klist that owns it and a kref
13 * reference count that indicates the number of current users of that node
14 * in the list.
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -080015 *
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -070016 * The entire point is to provide an interface for iterating over a list
17 * that is safe and allows for modification of the list during the
18 * iteration (e.g. insertion and removal), including modification of the
19 * current node on the list.
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -080020 *
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -070021 * It works using a 3rd object type - struct klist_iter - that is declared
22 * and initialized before an iteration. klist_next() is used to acquire the
23 * next element in the list. It returns NULL if there are no more items.
24 * Internally, that routine takes the klist's lock, decrements the
25 * reference count of the previous klist_node and increments the count of
26 * the next klist_node. It then drops the lock and returns.
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -080027 *
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -070028 * There are primitives for adding and removing nodes to/from a klist.
29 * When deleting, klist_del() will simply decrement the reference count.
30 * Only when the count goes to 0 is the node removed from the list.
31 * klist_remove() will try to delete the node from the list and block until
32 * it is actually removed. This is useful for objects (like devices) that
33 * have been removed from the system and must be freed (but must wait until
34 * all accessors have finished).
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -080035 */
36
37#include <linux/klist.h>
38#include <linux/module.h>
39
Tejun Heoa1ed5b02008-08-25 19:50:16 +020040/*
41 * Use the lowest bit of n_klist to mark deleted nodes and exclude
42 * dead ones from iteration.
43 */
44#define KNODE_DEAD 1LU
45#define KNODE_KLIST_MASK ~KNODE_DEAD
46
47static struct klist *knode_klist(struct klist_node *knode)
48{
49 return (struct klist *)
50 ((unsigned long)knode->n_klist & KNODE_KLIST_MASK);
51}
52
53static bool knode_dead(struct klist_node *knode)
54{
55 return (unsigned long)knode->n_klist & KNODE_DEAD;
56}
57
58static void knode_set_klist(struct klist_node *knode, struct klist *klist)
59{
60 knode->n_klist = klist;
61 /* no knode deserves to start its life dead */
62 WARN_ON(knode_dead(knode));
63}
64
65static void knode_kill(struct klist_node *knode)
66{
67 /* and no knode should die twice ever either, see we're very humane */
68 WARN_ON(knode_dead(knode));
69 *(unsigned long *)&knode->n_klist |= KNODE_DEAD;
70}
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -080071
72/**
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -070073 * klist_init - Initialize a klist structure.
74 * @k: The klist we're initializing.
75 * @get: The get function for the embedding object (NULL if none)
76 * @put: The put function for the embedding object (NULL if none)
James Bottomley34bb61f2005-09-06 16:56:51 -070077 *
78 * Initialises the klist structure. If the klist_node structures are
79 * going to be embedded in refcounted objects (necessary for safe
80 * deletion) then the get/put arguments are used to initialise
81 * functions that take and release references on the embedding
82 * objects.
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -080083 */
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -070084void klist_init(struct klist *k, void (*get)(struct klist_node *),
James Bottomley34bb61f2005-09-06 16:56:51 -070085 void (*put)(struct klist_node *))
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -080086{
87 INIT_LIST_HEAD(&k->k_list);
88 spin_lock_init(&k->k_lock);
James Bottomley34bb61f2005-09-06 16:56:51 -070089 k->get = get;
90 k->put = put;
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -080091}
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -080092EXPORT_SYMBOL_GPL(klist_init);
93
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -070094static void add_head(struct klist *k, struct klist_node *n)
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -080095{
96 spin_lock(&k->k_lock);
97 list_add(&n->n_node, &k->k_list);
98 spin_unlock(&k->k_lock);
99}
100
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700101static void add_tail(struct klist *k, struct klist_node *n)
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800102{
103 spin_lock(&k->k_lock);
104 list_add_tail(&n->n_node, &k->k_list);
105 spin_unlock(&k->k_lock);
106}
107
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700108static void klist_node_init(struct klist *k, struct klist_node *n)
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800109{
110 INIT_LIST_HEAD(&n->n_node);
111 init_completion(&n->n_removed);
112 kref_init(&n->n_ref);
Tejun Heoa1ed5b02008-08-25 19:50:16 +0200113 knode_set_klist(n, k);
James Bottomley34bb61f2005-09-06 16:56:51 -0700114 if (k->get)
115 k->get(n);
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800116}
117
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800118/**
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700119 * klist_add_head - Initialize a klist_node and add it to front.
120 * @n: node we're adding.
121 * @k: klist it's going on.
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800122 */
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700123void klist_add_head(struct klist_node *n, struct klist *k)
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800124{
125 klist_node_init(k, n);
126 add_head(k, n);
127}
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800128EXPORT_SYMBOL_GPL(klist_add_head);
129
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800130/**
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700131 * klist_add_tail - Initialize a klist_node and add it to back.
132 * @n: node we're adding.
133 * @k: klist it's going on.
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800134 */
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700135void klist_add_tail(struct klist_node *n, struct klist *k)
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800136{
137 klist_node_init(k, n);
138 add_tail(k, n);
139}
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800140EXPORT_SYMBOL_GPL(klist_add_tail);
141
Tejun Heo93dd4002008-04-22 18:58:46 +0900142/**
143 * klist_add_after - Init a klist_node and add it after an existing node
144 * @n: node we're adding.
145 * @pos: node to put @n after
146 */
147void klist_add_after(struct klist_node *n, struct klist_node *pos)
148{
Tejun Heoa1ed5b02008-08-25 19:50:16 +0200149 struct klist *k = knode_klist(pos);
Tejun Heo93dd4002008-04-22 18:58:46 +0900150
151 klist_node_init(k, n);
152 spin_lock(&k->k_lock);
153 list_add(&n->n_node, &pos->n_node);
154 spin_unlock(&k->k_lock);
155}
156EXPORT_SYMBOL_GPL(klist_add_after);
157
158/**
159 * klist_add_before - Init a klist_node and add it before an existing node
160 * @n: node we're adding.
161 * @pos: node to put @n after
162 */
163void klist_add_before(struct klist_node *n, struct klist_node *pos)
164{
Tejun Heoa1ed5b02008-08-25 19:50:16 +0200165 struct klist *k = knode_klist(pos);
Tejun Heo93dd4002008-04-22 18:58:46 +0900166
167 klist_node_init(k, n);
168 spin_lock(&k->k_lock);
169 list_add_tail(&n->n_node, &pos->n_node);
170 spin_unlock(&k->k_lock);
171}
172EXPORT_SYMBOL_GPL(klist_add_before);
173
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700174static void klist_release(struct kref *kref)
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800175{
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700176 struct klist_node *n = container_of(kref, struct klist_node, n_ref);
Alan Stern7e9f4b22006-09-18 16:28:06 -0400177
Tejun Heoa1ed5b02008-08-25 19:50:16 +0200178 WARN_ON(!knode_dead(n));
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800179 list_del(&n->n_node);
180 complete(&n->n_removed);
Tejun Heoa1ed5b02008-08-25 19:50:16 +0200181 knode_set_klist(n, NULL);
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800182}
183
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700184static int klist_dec_and_del(struct klist_node *n)
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800185{
186 return kref_put(&n->n_ref, klist_release);
187}
188
Tejun Heoa1ed5b02008-08-25 19:50:16 +0200189static void klist_put(struct klist_node *n, bool kill)
190{
191 struct klist *k = knode_klist(n);
192 void (*put)(struct klist_node *) = k->put;
193
194 spin_lock(&k->k_lock);
195 if (kill)
196 knode_kill(n);
197 if (!klist_dec_and_del(n))
198 put = NULL;
199 spin_unlock(&k->k_lock);
200 if (put)
201 put(n);
202}
203
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800204/**
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700205 * klist_del - Decrement the reference count of node and try to remove.
206 * @n: node we're deleting.
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800207 */
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700208void klist_del(struct klist_node *n)
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800209{
Tejun Heoa1ed5b02008-08-25 19:50:16 +0200210 klist_put(n, true);
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800211}
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800212EXPORT_SYMBOL_GPL(klist_del);
213
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800214/**
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700215 * klist_remove - Decrement the refcount of node and wait for it to go away.
216 * @n: node we're removing.
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800217 */
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700218void klist_remove(struct klist_node *n)
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800219{
Alan Stern7e9f4b22006-09-18 16:28:06 -0400220 klist_del(n);
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800221 wait_for_completion(&n->n_removed);
222}
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800223EXPORT_SYMBOL_GPL(klist_remove);
224
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800225/**
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700226 * klist_node_attached - Say whether a node is bound to a list or not.
227 * @n: Node that we're testing.
mochel@digitalimplant.org8b0c250b2005-03-24 12:58:57 -0800228 */
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700229int klist_node_attached(struct klist_node *n)
mochel@digitalimplant.org8b0c250b2005-03-24 12:58:57 -0800230{
231 return (n->n_klist != NULL);
232}
mochel@digitalimplant.org8b0c250b2005-03-24 12:58:57 -0800233EXPORT_SYMBOL_GPL(klist_node_attached);
234
mochel@digitalimplant.org8b0c250b2005-03-24 12:58:57 -0800235/**
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700236 * klist_iter_init_node - Initialize a klist_iter structure.
237 * @k: klist we're iterating.
238 * @i: klist_iter we're filling.
239 * @n: node to start with.
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800240 *
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700241 * Similar to klist_iter_init(), but starts the action off with @n,
242 * instead of with the list head.
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800243 */
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700244void klist_iter_init_node(struct klist *k, struct klist_iter *i,
245 struct klist_node *n)
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800246{
247 i->i_klist = k;
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800248 i->i_cur = n;
Frank Pavlice22dafb2005-11-26 20:48:40 -0800249 if (n)
250 kref_get(&n->n_ref);
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800251}
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800252EXPORT_SYMBOL_GPL(klist_iter_init_node);
253
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800254/**
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700255 * klist_iter_init - Iniitalize a klist_iter structure.
256 * @k: klist we're iterating.
257 * @i: klist_iter structure we're filling.
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800258 *
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700259 * Similar to klist_iter_init_node(), but start with the list head.
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800260 */
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700261void klist_iter_init(struct klist *k, struct klist_iter *i)
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800262{
263 klist_iter_init_node(k, i, NULL);
264}
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800265EXPORT_SYMBOL_GPL(klist_iter_init);
266
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800267/**
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700268 * klist_iter_exit - Finish a list iteration.
269 * @i: Iterator structure.
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800270 *
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700271 * Must be called when done iterating over list, as it decrements the
272 * refcount of the current node. Necessary in case iteration exited before
273 * the end of the list was reached, and always good form.
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800274 */
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700275void klist_iter_exit(struct klist_iter *i)
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800276{
277 if (i->i_cur) {
Tejun Heoa1ed5b02008-08-25 19:50:16 +0200278 klist_put(i->i_cur, false);
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800279 i->i_cur = NULL;
280 }
281}
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800282EXPORT_SYMBOL_GPL(klist_iter_exit);
283
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700284static struct klist_node *to_klist_node(struct list_head *n)
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800285{
286 return container_of(n, struct klist_node, n_node);
287}
288
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800289/**
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700290 * klist_next - Ante up next node in list.
291 * @i: Iterator structure.
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800292 *
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700293 * First grab list lock. Decrement the reference count of the previous
294 * node, if there was one. Grab the next node, increment its reference
295 * count, drop the lock, and return that next node.
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800296 */
Greg Kroah-Hartmanc3bb7fad2008-04-30 16:43:45 -0700297struct klist_node *klist_next(struct klist_iter *i)
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800298{
Alan Stern7e9f4b22006-09-18 16:28:06 -0400299 void (*put)(struct klist_node *) = i->i_klist->put;
Tejun Heoa1ed5b02008-08-25 19:50:16 +0200300 struct klist_node *last = i->i_cur;
301 struct klist_node *next;
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800302
303 spin_lock(&i->i_klist->k_lock);
Tejun Heoa1ed5b02008-08-25 19:50:16 +0200304
305 if (last) {
306 next = to_klist_node(last->n_node.next);
307 if (!klist_dec_and_del(last))
Alan Stern7e9f4b22006-09-18 16:28:06 -0400308 put = NULL;
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800309 } else
Tejun Heoa1ed5b02008-08-25 19:50:16 +0200310 next = to_klist_node(i->i_klist->k_list.next);
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800311
Tejun Heoa1ed5b02008-08-25 19:50:16 +0200312 i->i_cur = NULL;
313 while (next != to_klist_node(&i->i_klist->k_list)) {
314 if (likely(!knode_dead(next))) {
315 kref_get(&next->n_ref);
316 i->i_cur = next;
317 break;
318 }
319 next = to_klist_node(next->n_node.next);
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800320 }
Tejun Heoa1ed5b02008-08-25 19:50:16 +0200321
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800322 spin_unlock(&i->i_klist->k_lock);
Tejun Heoa1ed5b02008-08-25 19:50:16 +0200323
324 if (put && last)
325 put(last);
326 return i->i_cur;
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800327}
mochel@digitalimplant.org9a19fea2005-03-21 11:45:16 -0800328EXPORT_SYMBOL_GPL(klist_next);