1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37#include <linux/klist.h>
38#include <linux/export.h>
39#include <linux/sched.h>
40
41
42
43
44
45#define KNODE_DEAD 1LU
46#define KNODE_KLIST_MASK ~KNODE_DEAD
47
48static struct klist *knode_klist(struct klist_node *knode)
49{
50 return (struct klist *)
51 ((unsigned long)knode->n_klist & KNODE_KLIST_MASK);
52}
53
54static bool knode_dead(struct klist_node *knode)
55{
56 return (unsigned long)knode->n_klist & KNODE_DEAD;
57}
58
59static void knode_set_klist(struct klist_node *knode, struct klist *klist)
60{
61 knode->n_klist = klist;
62
63 WARN_ON(knode_dead(knode));
64}
65
66static void knode_kill(struct klist_node *knode)
67{
68
69 WARN_ON(knode_dead(knode));
70 *(unsigned long *)&knode->n_klist |= KNODE_DEAD;
71}
72
73
74
75
76
77
78
79
80
81
82
83
84
85void klist_init(struct klist *k, void (*get)(struct klist_node *),
86 void (*put)(struct klist_node *))
87{
88 INIT_LIST_HEAD(&k->k_list);
89 spin_lock_init(&k->k_lock);
90 k->get = get;
91 k->put = put;
92}
93EXPORT_SYMBOL_GPL(klist_init);
94
95static void add_head(struct klist *k, struct klist_node *n)
96{
97 spin_lock(&k->k_lock);
98 list_add(&n->n_node, &k->k_list);
99 spin_unlock(&k->k_lock);
100}
101
102static void add_tail(struct klist *k, struct klist_node *n)
103{
104 spin_lock(&k->k_lock);
105 list_add_tail(&n->n_node, &k->k_list);
106 spin_unlock(&k->k_lock);
107}
108
109static void klist_node_init(struct klist *k, struct klist_node *n)
110{
111 INIT_LIST_HEAD(&n->n_node);
112 kref_init(&n->n_ref);
113 knode_set_klist(n, k);
114 if (k->get)
115 k->get(n);
116}
117
118
119
120
121
122
123void klist_add_head(struct klist_node *n, struct klist *k)
124{
125 klist_node_init(k, n);
126 add_head(k, n);
127}
128EXPORT_SYMBOL_GPL(klist_add_head);
129
130
131
132
133
134
135void klist_add_tail(struct klist_node *n, struct klist *k)
136{
137 klist_node_init(k, n);
138 add_tail(k, n);
139}
140EXPORT_SYMBOL_GPL(klist_add_tail);
141
142
143
144
145
146
147void klist_add_after(struct klist_node *n, struct klist_node *pos)
148{
149 struct klist *k = knode_klist(pos);
150
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
160
161
162
163void klist_add_before(struct klist_node *n, struct klist_node *pos)
164{
165 struct klist *k = knode_klist(pos);
166
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
174struct klist_waiter {
175 struct list_head list;
176 struct klist_node *node;
177 struct task_struct *process;
178 int woken;
179};
180
181static DEFINE_SPINLOCK(klist_remove_lock);
182static LIST_HEAD(klist_remove_waiters);
183
184static void klist_release(struct kref *kref)
185{
186 struct klist_waiter *waiter, *tmp;
187 struct klist_node *n = container_of(kref, struct klist_node, n_ref);
188
189 WARN_ON(!knode_dead(n));
190 list_del(&n->n_node);
191 spin_lock(&klist_remove_lock);
192 list_for_each_entry_safe(waiter, tmp, &klist_remove_waiters, list) {
193 if (waiter->node != n)
194 continue;
195
196 list_del(&waiter->list);
197 waiter->woken = 1;
198 mb();
199 wake_up_process(waiter->process);
200 }
201 spin_unlock(&klist_remove_lock);
202 knode_set_klist(n, NULL);
203}
204
205static int klist_dec_and_del(struct klist_node *n)
206{
207 return kref_put(&n->n_ref, klist_release);
208}
209
210static void klist_put(struct klist_node *n, bool kill)
211{
212 struct klist *k = knode_klist(n);
213 void (*put)(struct klist_node *) = k->put;
214
215 spin_lock(&k->k_lock);
216 if (kill)
217 knode_kill(n);
218 if (!klist_dec_and_del(n))
219 put = NULL;
220 spin_unlock(&k->k_lock);
221 if (put)
222 put(n);
223}
224
225
226
227
228
229void klist_del(struct klist_node *n)
230{
231 klist_put(n, true);
232}
233EXPORT_SYMBOL_GPL(klist_del);
234
235
236
237
238
239void klist_remove(struct klist_node *n)
240{
241 struct klist_waiter waiter;
242
243 waiter.node = n;
244 waiter.process = current;
245 waiter.woken = 0;
246 spin_lock(&klist_remove_lock);
247 list_add(&waiter.list, &klist_remove_waiters);
248 spin_unlock(&klist_remove_lock);
249
250 klist_del(n);
251
252 for (;;) {
253 set_current_state(TASK_UNINTERRUPTIBLE);
254 if (waiter.woken)
255 break;
256 schedule();
257 }
258 __set_current_state(TASK_RUNNING);
259}
260EXPORT_SYMBOL_GPL(klist_remove);
261
262
263
264
265
266int klist_node_attached(struct klist_node *n)
267{
268 return (n->n_klist != NULL);
269}
270EXPORT_SYMBOL_GPL(klist_node_attached);
271
272
273
274
275
276
277
278
279
280
281void klist_iter_init_node(struct klist *k, struct klist_iter *i,
282 struct klist_node *n)
283{
284 i->i_klist = k;
285 i->i_cur = NULL;
286 if (n && kref_get_unless_zero(&n->n_ref))
287 i->i_cur = n;
288}
289EXPORT_SYMBOL_GPL(klist_iter_init_node);
290
291
292
293
294
295
296
297
298void klist_iter_init(struct klist *k, struct klist_iter *i)
299{
300 klist_iter_init_node(k, i, NULL);
301}
302EXPORT_SYMBOL_GPL(klist_iter_init);
303
304
305
306
307
308
309
310
311
312void klist_iter_exit(struct klist_iter *i)
313{
314 if (i->i_cur) {
315 klist_put(i->i_cur, false);
316 i->i_cur = NULL;
317 }
318}
319EXPORT_SYMBOL_GPL(klist_iter_exit);
320
321static struct klist_node *to_klist_node(struct list_head *n)
322{
323 return container_of(n, struct klist_node, n_node);
324}
325
326
327
328
329
330
331
332
333
334struct klist_node *klist_prev(struct klist_iter *i)
335{
336 void (*put)(struct klist_node *) = i->i_klist->put;
337 struct klist_node *last = i->i_cur;
338 struct klist_node *prev;
339 unsigned long flags;
340
341 spin_lock_irqsave(&i->i_klist->k_lock, flags);
342
343 if (last) {
344 prev = to_klist_node(last->n_node.prev);
345 if (!klist_dec_and_del(last))
346 put = NULL;
347 } else
348 prev = to_klist_node(i->i_klist->k_list.prev);
349
350 i->i_cur = NULL;
351 while (prev != to_klist_node(&i->i_klist->k_list)) {
352 if (likely(!knode_dead(prev))) {
353 kref_get(&prev->n_ref);
354 i->i_cur = prev;
355 break;
356 }
357 prev = to_klist_node(prev->n_node.prev);
358 }
359
360 spin_unlock_irqrestore(&i->i_klist->k_lock, flags);
361
362 if (put && last)
363 put(last);
364 return i->i_cur;
365}
366EXPORT_SYMBOL_GPL(klist_prev);
367
368
369
370
371
372
373
374
375
376struct klist_node *klist_next(struct klist_iter *i)
377{
378 void (*put)(struct klist_node *) = i->i_klist->put;
379 struct klist_node *last = i->i_cur;
380 struct klist_node *next;
381 unsigned long flags;
382
383 spin_lock_irqsave(&i->i_klist->k_lock, flags);
384
385 if (last) {
386 next = to_klist_node(last->n_node.next);
387 if (!klist_dec_and_del(last))
388 put = NULL;
389 } else
390 next = to_klist_node(i->i_klist->k_list.next);
391
392 i->i_cur = NULL;
393 while (next != to_klist_node(&i->i_klist->k_list)) {
394 if (likely(!knode_dead(next))) {
395 kref_get(&next->n_ref);
396 i->i_cur = next;
397 break;
398 }
399 next = to_klist_node(next->n_node.next);
400 }
401
402 spin_unlock_irqrestore(&i->i_klist->k_lock, flags);
403
404 if (put && last)
405 put(last);
406 return i->i_cur;
407}
408EXPORT_SYMBOL_GPL(klist_next);
409