ktree.c
9.61 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
* ktree.c - Routines for manipulating ktrees.
*
* Copyright (C) 2005 Patrick Mochel
*
* This file is released under the GPL v2.
*
* This ktree interface provides a couple of structures that wrap around
* struct list_head to provide explicit list "head" (struct ktree) and list
* "node" (struct ktree_node) objects. For struct ktree, a spinlock is
* included that protects access to the actual list itself. struct
* ktree_node provides a pointer to the ktree that owns it and a kref
* reference count that indicates the number of current users of that node
* in the list.
*
* The entire point is to provide an interface for iterating over a list
* that is safe and allows for modification of the list during the
* iteration (e.g. insertion and removal), including modification of the
* current node on the list.
*
* It works using a 3rd object type - struct ktree_iter - that is declared
* and initialized before an iteration. ktree_iter_next() is used to acquire the
* next element in the list. It returns NULL if there are no more items.
* Internally, that routine takes the ktree's lock, decrements the
* reference count of the previous ktree_node and increments the count of
* the next ktree_node. It then drops the lock and returns.
*
* There are primitives for adding and removing nodes to/from a ktree.
* When deleting, ktree_del_node() will simply decrement the reference count.
* Only when the count goes to 0 is the node removed from the list.
* ktree_remove_node() will try to delete the node from the list and block until
* it is actually removed. This is useful for objects (like devices) that
* have been removed from the system and must be freed (but must wait until
* all accessors have finished).
*/
#include <linux/export.h>
#include <linux/sched.h>
#include <linux/ktree.h>
#include <linux/list_sort.h>
static void knode_release(struct kref *kref);
static void __ktree_put_node(struct ktree_node *node, bool kill);
void ktree_put_node(struct ktree_node *node)
{
if (node)
__ktree_put_node(node, false);
}
EXPORT_SYMBOL_GPL(ktree_put_node);
static void knode_set_parent(struct ktree_node *node, struct ktree_node *parent)
{
struct ktree_node *old_parent = node->parent;
WARN_ON(parent && parent->deleted);
node->parent = ktree_get_node(parent);
ktree_put_node(old_parent);
}
void ktree_init(struct ktree *ktree, void (*get)(struct ktree_node *),
void (*put)(struct ktree_node *))
{
ktree->root = NULL;
spin_lock_init(&ktree->lock);
ktree->get = get;
ktree->put = put;
}
EXPORT_SYMBOL_GPL(ktree_init);
static void ktree_node_init(struct ktree *ktree, struct ktree_node *parent,
struct ktree_node *node)
{
node->deleted = false;
INIT_LIST_HEAD(&node->siblings);
INIT_LIST_HEAD(&node->children);
kref_init(&node->refcount);
node->ktree = ktree;
knode_set_parent(node, parent);
if (ktree->get)
ktree->get(node);
}
void ktree_set_root(struct ktree *ktree, struct ktree_node *root)
{
ktree_node_init(ktree, NULL, root);
ktree_del_node(ktree->root);
ktree->root = root;
}
EXPORT_SYMBOL_GPL(ktree_set_root);
void ktree_add_child_before(struct ktree_node *parent, struct ktree_node *child,
struct ktree_node *pos)
{
struct ktree *ktree = parent->ktree;
struct list_head *head;
ktree_node_init(ktree, parent, child);
spin_lock(&ktree->lock);
head = pos ? &pos->siblings : &parent->children;
list_add_tail(&child->siblings, head);
spin_unlock(&ktree->lock);
}
EXPORT_SYMBOL_GPL(ktree_add_child_before);
void ktree_add_child_after(struct ktree_node *parent, struct ktree_node *child,
struct ktree_node *pos)
{
struct ktree *ktree = parent->ktree;
struct list_head *head;
ktree_node_init(ktree, parent, child);
spin_lock(&ktree->lock);
head = pos ? &pos->siblings : &parent->children;
list_add(&child->siblings, head);
spin_unlock(&ktree->lock);
}
EXPORT_SYMBOL_GPL(ktree_add_child_after);
static void ktree_node_destroy(struct ktree_node *node)
{
struct ktree_node *parent = node->parent;
struct ktree *ktree = node->ktree;
WARN_ON(!ktree_is_leaf(node));
if (ktree->put)
ktree->put(node);
ktree_put_node(parent);
}
struct ktree_waiter {
struct list_head list;
struct ktree_node *node;
struct task_struct *process;
int woken;
};
static DEFINE_SPINLOCK(ktree_remove_lock);
static LIST_HEAD(ktree_remove_waiters);
static void knode_release(struct kref *kref)
{
struct ktree_waiter *waiter, *tmp;
struct ktree_node *node;
struct ktree *ktree;
node = container_of(kref, struct ktree_node, refcount);
WARN_ON(!node->deleted || !node->ktree);
ktree = node->ktree;
assert_spin_locked(&ktree->lock);
list_del(&node->siblings);
if (node == ktree->root)
ktree->root = NULL;
spin_lock(&ktree_remove_lock);
list_for_each_entry_safe(waiter, tmp, &ktree_remove_waiters, list) {
if (waiter->node != node)
continue;
waiter->woken = 1;
mb(); /* barrier before waking up waiting process */
wake_up_process(waiter->process);
list_del(&waiter->list);
}
spin_unlock(&ktree_remove_lock);
}
typedef void (*put_fn)(struct ktree_node *);
static bool __ktree_put_node_locked(struct ktree_node *node)
{
return !!kref_put(&node->refcount, knode_release);
}
static void __ktree_put_node_unlock(struct ktree_node *node)
{
struct ktree *ktree = node->ktree;
if (__ktree_put_node_locked(node)) {
spin_unlock(&ktree->lock);
ktree_node_destroy(node);
spin_lock(&ktree->lock);
}
}
static void __ktree_put_node(struct ktree_node *node, bool kill)
{
struct ktree *ktree = node->ktree;
bool destroy;
spin_lock(&ktree->lock);
if (kill) {
WARN_ON(node->deleted);
node->deleted = true;
}
destroy = __ktree_put_node_locked(node);
spin_unlock(&ktree->lock);
if (destroy)
ktree_node_destroy(node);
}
int __ktree_del_node(struct ktree_node *child, void *arg)
{
ktree_del_node(child);
return 0;
}
void ktree_del_node(struct ktree_node *node)
{
if (node) {
ktree_for_each_child(node, __ktree_del_node, NULL);
__ktree_put_node(node, true);
}
}
EXPORT_SYMBOL_GPL(ktree_del_node);
void ktree_remove_node(struct ktree_node *node)
{
struct ktree_waiter waiter;
waiter.node = node;
waiter.process = current;
waiter.woken = 0;
spin_lock(&ktree_remove_lock);
list_add(&waiter.list, &ktree_remove_waiters);
spin_unlock(&ktree_remove_lock);
ktree_del_node(node);
for (;;) {
set_current_state(TASK_UNINTERRUPTIBLE);
if (waiter.woken)
break;
schedule();
}
__set_current_state(TASK_RUNNING);
}
EXPORT_SYMBOL_GPL(ktree_remove_node);
static struct ktree_node *ktree_get_pos(struct ktree_node *parent,
struct list_head *pos)
{
struct ktree_node *node = NULL;
if (pos != &parent->children)
node = container_of(pos, struct ktree_node, siblings);
return ktree_get_node(node);
}
struct ktree_node *ktree_try_traverse_parent(struct ktree_node *node)
{
struct ktree_node *next;
next = ktree_get_parent(node);
if (next)
ktree_put_node(node);
return next;
}
struct ktree_node *ktree_traverse_parent(struct ktree_node *node)
{
struct ktree_node *next;
next = ktree_get_parent(node);
ktree_put_node(node);
return next;
}
struct ktree_node *ktree_traverse_sibling(struct ktree_node *node, bool reverse)
{
struct ktree_node *next, *parent;
struct list_head *pos;
parent = node->parent;
if (!parent)
return NULL;
spin_lock(&node->ktree->lock);
for (;;) {
pos = reverse ? node->siblings.prev : node->siblings.next;
next = ktree_get_pos(parent, pos);
__ktree_put_node_unlock(node);
if (!next || !next->deleted)
break;
node = next;
}
spin_unlock(&node->ktree->lock);
return next;
}
struct ktree_node *ktree_try_traverse_sibling(struct ktree_node *node,
bool reverse)
{
struct ktree_node *next;
node = ktree_get_node(node);
next = ktree_traverse_sibling(node, reverse);
if (next)
ktree_put_node(node);
return next;
}
struct ktree_node *ktree_traverse_child(struct ktree_node *node, bool reverse)
{
struct ktree_node *child;
struct list_head *pos;
spin_lock(&node->ktree->lock);
pos = reverse ? node->children.prev : node->children.next;
child = ktree_get_pos(node, pos);
spin_unlock(&node->ktree->lock);
ktree_put_node(node);
if (child && child->deleted)
child = ktree_traverse_sibling(child, reverse);
return child;
}
struct ktree_node *ktree_try_traverse_child(struct ktree_node *node,
bool reverse)
{
struct ktree_node *next;
node = ktree_get_node(node);
next = ktree_traverse_child(node, reverse);
if (next)
ktree_put_node(node);
return next;
}
int ktree_for_each_child(struct ktree_node *parent,
int (*visitor)(struct ktree_node *child, void *arg),
void *arg) {
struct ktree_node *node;
bool reverse = false;
int error;
/* grab a reference that we drop as we traverse */
node = ktree_get_node(parent);
for (node = ktree_traverse_child(node, reverse); node;
node = ktree_traverse_sibling(node, reverse)) {
error = visitor(node, arg);
if (error) {
ktree_put_node(node);
return error;
}
}
return 0;
}
EXPORT_SYMBOL_GPL(ktree_for_each_child);
struct ktree_sort_params {
int (*cmp)(struct ktree_node *a, struct ktree_node *b,
void *arg);
void *arg;
};
static int __ktree_cmp(void *arg, struct list_head *_a, struct list_head *_b)
{
struct ktree_node *a = container_of(_a, struct ktree_node, siblings);
struct ktree_node *b = container_of(_b, struct ktree_node, siblings);
struct ktree_sort_params *params = arg;
return params->cmp(a, b, params->arg);
}
void ktree_sort_children(struct ktree_node *parent,
int (*cmp)(struct ktree_node *a, struct ktree_node *b,
void *arg),
void *arg)
{
struct ktree_sort_params params = { .cmp = cmp, .arg = arg };
spin_lock(&parent->ktree->lock);
list_sort(¶ms, &parent->children, __ktree_cmp);
spin_unlock(&parent->ktree->lock);
}
EXPORT_SYMBOL_GPL(ktree_sort_children);