Rev 1412 | Rev 3391 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1412 | serge | 1 | /* |
2 | * 2002-10-18 written by Jim Houston jim.houston@ccur.com |
||
3 | * Copyright (C) 2002 by Concurrent Computer Corporation |
||
4 | * Distributed under the GNU GPL license version 2. |
||
5 | * |
||
6 | * Modified by George Anzinger to reuse immediately and to use |
||
7 | * find bit instructions. Also removed _irq on spinlocks. |
||
8 | * |
||
9 | * Modified by Nadia Derbey to make it RCU safe. |
||
10 | * |
||
11 | * Small id to pointer translation service. |
||
12 | * |
||
13 | * It uses a radix tree like structure as a sparse array indexed |
||
14 | * by the id to obtain the pointer. The bitmap makes allocating |
||
15 | * a new id quick. |
||
16 | * |
||
17 | * You call it to allocate an id (an int) an associate with that id a |
||
18 | * pointer or what ever, we treat it as a (void *). You can pass this |
||
19 | * id to a user for him to pass back at a later time. You then pass |
||
20 | * that id to this code and it returns your pointer. |
||
21 | |||
22 | * You can release ids at any time. When all ids are released, most of |
||
23 | * the memory is returned (we keep IDR_FREE_MAX) in a local pool so we |
||
24 | * don't need to go to the memory "store" during an id allocate, just |
||
25 | * so you don't need to be too concerned about locking and conflicts |
||
26 | * with the slab allocator. |
||
27 | */ |
||
28 | |||
29 | #include |
||
30 | #include |
||
31 | #include |
||
32 | #include |
||
33 | //#include |
||
34 | |||
35 | unsigned long find_first_bit(const unsigned long *addr, unsigned long size) |
||
36 | { |
||
37 | const unsigned long *p = addr; |
||
38 | unsigned long result = 0; |
||
39 | unsigned long tmp; |
||
40 | |||
41 | while (size & ~(BITS_PER_LONG-1)) { |
||
42 | if ((tmp = *(p++))) |
||
43 | goto found; |
||
44 | result += BITS_PER_LONG; |
||
45 | size -= BITS_PER_LONG; |
||
46 | } |
||
47 | if (!size) |
||
48 | return result; |
||
49 | |||
50 | tmp = (*p) & (~0UL >> (BITS_PER_LONG - size)); |
||
51 | if (tmp == 0UL) /* Are any bits set? */ |
||
52 | return result + size; /* Nope. */ |
||
53 | found: |
||
54 | return result + __ffs(tmp); |
||
55 | } |
||
56 | |||
57 | int find_next_bit(const unsigned long *addr, int size, int offset) |
||
58 | { |
||
59 | const unsigned long *p = addr + (offset >> 5); |
||
60 | int set = 0, bit = offset & 31, res; |
||
61 | |||
62 | if (bit) |
||
63 | { |
||
64 | /* |
||
65 | * Look for nonzero in the first 32 bits: |
||
66 | */ |
||
67 | __asm__("bsfl %1,%0\n\t" |
||
68 | "jne 1f\n\t" |
||
69 | "movl $32, %0\n" |
||
70 | "1:" |
||
71 | : "=r" (set) |
||
72 | : "r" (*p >> bit)); |
||
73 | if (set < (32 - bit)) |
||
74 | return set + offset; |
||
75 | set = 32 - bit; |
||
76 | p++; |
||
77 | } |
||
78 | /* |
||
79 | * No set bit yet, search remaining full words for a bit |
||
80 | */ |
||
81 | res = find_first_bit (p, size - 32 * (p - addr)); |
||
82 | return (offset + set + res); |
||
83 | } |
||
84 | |||
85 | #define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x)) |
||
86 | |||
87 | #define rcu_dereference(p) ({ \ |
||
88 | typeof(p) _________p1 = ACCESS_ONCE(p); \ |
||
89 | (_________p1); \ |
||
90 | }) |
||
91 | |||
92 | #define rcu_assign_pointer(p, v) \ |
||
93 | ({ \ |
||
94 | if (!__builtin_constant_p(v) || \ |
||
95 | ((v) != NULL)) \ |
||
96 | (p) = (v); \ |
||
97 | }) |
||
98 | |||
99 | //static struct kmem_cache *idr_layer_cache; |
||
100 | |||
101 | |||
102 | |||
103 | |||
104 | |||
105 | static struct idr_layer *get_from_free_list(struct idr *idp) |
||
106 | { |
||
107 | struct idr_layer *p; |
||
108 | unsigned long flags; |
||
109 | |||
110 | // spin_lock_irqsave(&idp->lock, flags); |
||
111 | if ((p = idp->id_free)) { |
||
112 | idp->id_free = p->ary[0]; |
||
113 | idp->id_free_cnt--; |
||
114 | p->ary[0] = NULL; |
||
115 | } |
||
116 | // spin_unlock_irqrestore(&idp->lock, flags); |
||
117 | return(p); |
||
118 | } |
||
119 | |||
120 | static void idr_layer_rcu_free(struct rcu_head *head) |
||
121 | { |
||
122 | struct idr_layer *layer; |
||
123 | |||
124 | layer = container_of(head, struct idr_layer, rcu_head); |
||
125 | kfree(layer); |
||
126 | } |
||
127 | |||
128 | static inline void free_layer(struct idr_layer *p) |
||
129 | { |
||
130 | kfree(p); |
||
131 | } |
||
132 | |||
133 | /* only called when idp->lock is held */ |
||
134 | static void __move_to_free_list(struct idr *idp, struct idr_layer *p) |
||
135 | { |
||
136 | p->ary[0] = idp->id_free; |
||
137 | idp->id_free = p; |
||
138 | idp->id_free_cnt++; |
||
139 | } |
||
140 | |||
141 | static void move_to_free_list(struct idr *idp, struct idr_layer *p) |
||
142 | { |
||
143 | unsigned long flags; |
||
144 | |||
145 | /* |
||
146 | * Depends on the return element being zeroed. |
||
147 | */ |
||
148 | // spin_lock_irqsave(&idp->lock, flags); |
||
149 | __move_to_free_list(idp, p); |
||
150 | // spin_unlock_irqrestore(&idp->lock, flags); |
||
151 | } |
||
152 | |||
153 | static void idr_mark_full(struct idr_layer **pa, int id) |
||
154 | { |
||
155 | struct idr_layer *p = pa[0]; |
||
156 | int l = 0; |
||
157 | |||
158 | __set_bit(id & IDR_MASK, &p->bitmap); |
||
159 | /* |
||
160 | * If this layer is full mark the bit in the layer above to |
||
161 | * show that this part of the radix tree is full. This may |
||
162 | * complete the layer above and require walking up the radix |
||
163 | * tree. |
||
164 | */ |
||
165 | while (p->bitmap == IDR_FULL) { |
||
166 | if (!(p = pa[++l])) |
||
167 | break; |
||
168 | id = id >> IDR_BITS; |
||
169 | __set_bit((id & IDR_MASK), &p->bitmap); |
||
170 | } |
||
171 | } |
||
172 | |||
173 | /** |
||
2966 | Serge | 174 | * idr_pre_get - reserve resources for idr allocation |
1412 | serge | 175 | * @idp: idr handle |
176 | * @gfp_mask: memory allocation flags |
||
177 | * |
||
2966 | Serge | 178 | * This function should be called prior to calling the idr_get_new* functions. |
179 | * It preallocates enough memory to satisfy the worst possible allocation. The |
||
180 | * caller should pass in GFP_KERNEL if possible. This of course requires that |
||
181 | * no spinning locks be held. |
||
1412 | serge | 182 | * |
2966 | Serge | 183 | * If the system is REALLY out of memory this function returns %0, |
184 | * otherwise %1. |
||
1412 | serge | 185 | */ |
2966 | Serge | 186 | int idr_pre_get(struct idr *idp, gfp_t gfp_mask) |
1412 | serge | 187 | { |
188 | while (idp->id_free_cnt < IDR_FREE_MAX) { |
||
189 | struct idr_layer *new; |
||
190 | new = kzalloc(sizeof(struct idr_layer), gfp_mask); |
||
191 | if (new == NULL) |
||
192 | return (0); |
||
193 | move_to_free_list(idp, new); |
||
194 | } |
||
195 | return 1; |
||
196 | } |
||
197 | |||
198 | static int sub_alloc(struct idr *idp, int *starting_id, struct idr_layer **pa) |
||
199 | { |
||
200 | int n, m, sh; |
||
201 | struct idr_layer *p, *new; |
||
202 | int l, id, oid; |
||
203 | unsigned long bm; |
||
204 | |||
205 | id = *starting_id; |
||
206 | restart: |
||
207 | p = idp->top; |
||
208 | l = idp->layers; |
||
209 | pa[l--] = NULL; |
||
210 | while (1) { |
||
211 | /* |
||
212 | * We run around this while until we reach the leaf node... |
||
213 | */ |
||
214 | n = (id >> (IDR_BITS*l)) & IDR_MASK; |
||
215 | bm = ~p->bitmap; |
||
216 | m = find_next_bit(&bm, IDR_SIZE, n); |
||
217 | if (m == IDR_SIZE) { |
||
218 | /* no space available go back to previous layer. */ |
||
219 | l++; |
||
220 | oid = id; |
||
221 | id = (id | ((1 << (IDR_BITS * l)) - 1)) + 1; |
||
222 | |||
223 | /* if already at the top layer, we need to grow */ |
||
224 | if (!(p = pa[l])) { |
||
225 | *starting_id = id; |
||
226 | return IDR_NEED_TO_GROW; |
||
227 | } |
||
228 | |||
229 | /* If we need to go up one layer, continue the |
||
230 | * loop; otherwise, restart from the top. |
||
231 | */ |
||
232 | sh = IDR_BITS * (l + 1); |
||
233 | if (oid >> sh == id >> sh) |
||
234 | continue; |
||
235 | else |
||
236 | goto restart; |
||
237 | } |
||
238 | if (m != n) { |
||
239 | sh = IDR_BITS*l; |
||
240 | id = ((id >> sh) ^ n ^ m) << sh; |
||
241 | } |
||
242 | if ((id >= MAX_ID_BIT) || (id < 0)) |
||
243 | return IDR_NOMORE_SPACE; |
||
244 | if (l == 0) |
||
245 | break; |
||
246 | /* |
||
247 | * Create the layer below if it is missing. |
||
248 | */ |
||
249 | if (!p->ary[m]) { |
||
250 | new = get_from_free_list(idp); |
||
251 | if (!new) |
||
252 | return -1; |
||
253 | new->layer = l-1; |
||
254 | rcu_assign_pointer(p->ary[m], new); |
||
255 | p->count++; |
||
256 | } |
||
257 | pa[l--] = p; |
||
258 | p = p->ary[m]; |
||
259 | } |
||
260 | |||
261 | pa[l] = p; |
||
262 | return id; |
||
263 | } |
||
264 | |||
265 | static int idr_get_empty_slot(struct idr *idp, int starting_id, |
||
266 | struct idr_layer **pa) |
||
267 | { |
||
268 | struct idr_layer *p, *new; |
||
269 | int layers, v, id; |
||
270 | unsigned long flags; |
||
271 | |||
272 | id = starting_id; |
||
273 | build_up: |
||
274 | p = idp->top; |
||
275 | layers = idp->layers; |
||
276 | if (unlikely(!p)) { |
||
277 | if (!(p = get_from_free_list(idp))) |
||
278 | return -1; |
||
279 | p->layer = 0; |
||
280 | layers = 1; |
||
281 | } |
||
282 | /* |
||
283 | * Add a new layer to the top of the tree if the requested |
||
284 | * id is larger than the currently allocated space. |
||
285 | */ |
||
286 | while ((layers < (MAX_LEVEL - 1)) && (id >= (1 << (layers*IDR_BITS)))) { |
||
287 | layers++; |
||
288 | if (!p->count) { |
||
289 | /* special case: if the tree is currently empty, |
||
290 | * then we grow the tree by moving the top node |
||
291 | * upwards. |
||
292 | */ |
||
293 | p->layer++; |
||
294 | continue; |
||
295 | } |
||
296 | if (!(new = get_from_free_list(idp))) { |
||
297 | /* |
||
298 | * The allocation failed. If we built part of |
||
299 | * the structure tear it down. |
||
300 | */ |
||
301 | // spin_lock_irqsave(&idp->lock, flags); |
||
302 | for (new = p; p && p != idp->top; new = p) { |
||
303 | p = p->ary[0]; |
||
304 | new->ary[0] = NULL; |
||
305 | new->bitmap = new->count = 0; |
||
306 | __move_to_free_list(idp, new); |
||
307 | } |
||
308 | // spin_unlock_irqrestore(&idp->lock, flags); |
||
309 | return -1; |
||
310 | } |
||
311 | new->ary[0] = p; |
||
312 | new->count = 1; |
||
313 | new->layer = layers-1; |
||
314 | if (p->bitmap == IDR_FULL) |
||
315 | __set_bit(0, &new->bitmap); |
||
316 | p = new; |
||
317 | } |
||
318 | rcu_assign_pointer(idp->top, p); |
||
319 | idp->layers = layers; |
||
320 | v = sub_alloc(idp, &id, pa); |
||
321 | if (v == IDR_NEED_TO_GROW) |
||
322 | goto build_up; |
||
323 | return(v); |
||
324 | } |
||
325 | |||
326 | static int idr_get_new_above_int(struct idr *idp, void *ptr, int starting_id) |
||
327 | { |
||
328 | struct idr_layer *pa[MAX_LEVEL]; |
||
329 | int id; |
||
330 | |||
331 | id = idr_get_empty_slot(idp, starting_id, pa); |
||
332 | if (id >= 0) { |
||
333 | /* |
||
334 | * Successfully found an empty slot. Install the user |
||
335 | * pointer and mark the slot full. |
||
336 | */ |
||
337 | rcu_assign_pointer(pa[0]->ary[id & IDR_MASK], |
||
338 | (struct idr_layer *)ptr); |
||
339 | pa[0]->count++; |
||
340 | idr_mark_full(pa, id); |
||
341 | } |
||
342 | |||
343 | return id; |
||
344 | } |
||
345 | |||
346 | /** |
||
347 | * idr_get_new_above - allocate new idr entry above or equal to a start id |
||
348 | * @idp: idr handle |
||
2966 | Serge | 349 | * @ptr: pointer you want associated with the id |
350 | * @starting_id: id to start search at |
||
1412 | serge | 351 | * @id: pointer to the allocated handle |
352 | * |
||
353 | * This is the allocate id function. It should be called with any |
||
354 | * required locks. |
||
355 | * |
||
2966 | Serge | 356 | * If allocation from IDR's private freelist fails, idr_get_new_above() will |
357 | * return %-EAGAIN. The caller should retry the idr_pre_get() call to refill |
||
358 | * IDR's preallocation and then retry the idr_get_new_above() call. |
||
1412 | serge | 359 | * |
2966 | Serge | 360 | * If the idr is full idr_get_new_above() will return %-ENOSPC. |
361 | * |
||
362 | * @id returns a value in the range @starting_id ... %0x7fffffff |
||
1412 | serge | 363 | */ |
364 | int idr_get_new_above(struct idr *idp, void *ptr, int starting_id, int *id) |
||
365 | { |
||
366 | int rv; |
||
2966 | Serge | 367 | |
1412 | serge | 368 | rv = idr_get_new_above_int(idp, ptr, starting_id); |
369 | /* |
||
370 | * This is a cheap hack until the IDR code can be fixed to |
||
371 | * return proper error values. |
||
372 | */ |
||
373 | if (rv < 0) |
||
374 | { |
||
375 | dbgprintf("fail\n"); |
||
376 | return _idr_rc_to_errno(rv); |
||
377 | }; |
||
378 | *id = rv; |
||
379 | return 0; |
||
380 | } |
||
381 | |||
382 | /** |
||
383 | * idr_get_new - allocate new idr entry |
||
384 | * @idp: idr handle |
||
2966 | Serge | 385 | * @ptr: pointer you want associated with the id |
1412 | serge | 386 | * @id: pointer to the allocated handle |
387 | * |
||
2966 | Serge | 388 | * If allocation from IDR's private freelist fails, idr_get_new_above() will |
389 | * return %-EAGAIN. The caller should retry the idr_pre_get() call to refill |
||
390 | * IDR's preallocation and then retry the idr_get_new_above() call. |
||
1412 | serge | 391 | * |
2966 | Serge | 392 | * If the idr is full idr_get_new_above() will return %-ENOSPC. |
1412 | serge | 393 | * |
2966 | Serge | 394 | * @id returns a value in the range %0 ... %0x7fffffff |
1412 | serge | 395 | */ |
396 | int idr_get_new(struct idr *idp, void *ptr, int *id) |
||
397 | { |
||
398 | int rv; |
||
399 | |||
400 | rv = idr_get_new_above_int(idp, ptr, 0); |
||
401 | /* |
||
402 | * This is a cheap hack until the IDR code can be fixed to |
||
403 | * return proper error values. |
||
404 | */ |
||
405 | if (rv < 0) |
||
406 | return _idr_rc_to_errno(rv); |
||
407 | *id = rv; |
||
408 | return 0; |
||
409 | } |
||
410 | |||
411 | static void idr_remove_warning(int id) |
||
412 | { |
||
413 | printk(KERN_WARNING |
||
414 | "idr_remove called for id=%d which is not allocated.\n", id); |
||
415 | // dump_stack(); |
||
416 | } |
||
417 | |||
418 | static void sub_remove(struct idr *idp, int shift, int id) |
||
419 | { |
||
420 | struct idr_layer *p = idp->top; |
||
421 | struct idr_layer **pa[MAX_LEVEL]; |
||
422 | struct idr_layer ***paa = &pa[0]; |
||
423 | struct idr_layer *to_free; |
||
424 | int n; |
||
425 | |||
426 | *paa = NULL; |
||
427 | *++paa = &idp->top; |
||
428 | |||
429 | while ((shift > 0) && p) { |
||
430 | n = (id >> shift) & IDR_MASK; |
||
431 | __clear_bit(n, &p->bitmap); |
||
432 | *++paa = &p->ary[n]; |
||
433 | p = p->ary[n]; |
||
434 | shift -= IDR_BITS; |
||
435 | } |
||
436 | n = id & IDR_MASK; |
||
437 | if (likely(p != NULL && test_bit(n, &p->bitmap))){ |
||
438 | __clear_bit(n, &p->bitmap); |
||
439 | rcu_assign_pointer(p->ary[n], NULL); |
||
440 | to_free = NULL; |
||
441 | while(*paa && ! --((**paa)->count)){ |
||
442 | if (to_free) |
||
443 | free_layer(to_free); |
||
444 | to_free = **paa; |
||
445 | **paa-- = NULL; |
||
446 | } |
||
447 | if (!*paa) |
||
448 | idp->layers = 0; |
||
449 | if (to_free) |
||
450 | free_layer(to_free); |
||
451 | } else |
||
452 | idr_remove_warning(id); |
||
453 | } |
||
454 | |||
455 | /** |
||
2966 | Serge | 456 | * idr_remove - remove the given id and free its slot |
1412 | serge | 457 | * @idp: idr handle |
458 | * @id: unique key |
||
459 | */ |
||
460 | void idr_remove(struct idr *idp, int id) |
||
461 | { |
||
462 | struct idr_layer *p; |
||
463 | struct idr_layer *to_free; |
||
464 | |||
465 | /* Mask off upper bits we don't use for the search. */ |
||
466 | id &= MAX_ID_MASK; |
||
467 | |||
468 | sub_remove(idp, (idp->layers - 1) * IDR_BITS, id); |
||
469 | if (idp->top && idp->top->count == 1 && (idp->layers > 1) && |
||
470 | idp->top->ary[0]) { |
||
471 | /* |
||
472 | * Single child at leftmost slot: we can shrink the tree. |
||
473 | * This level is not needed anymore since when layers are |
||
474 | * inserted, they are inserted at the top of the existing |
||
475 | * tree. |
||
476 | */ |
||
477 | to_free = idp->top; |
||
478 | p = idp->top->ary[0]; |
||
479 | rcu_assign_pointer(idp->top, p); |
||
480 | --idp->layers; |
||
481 | to_free->bitmap = to_free->count = 0; |
||
482 | free_layer(to_free); |
||
483 | } |
||
484 | while (idp->id_free_cnt >= IDR_FREE_MAX) { |
||
485 | p = get_from_free_list(idp); |
||
486 | /* |
||
487 | * Note: we don't call the rcu callback here, since the only |
||
488 | * layers that fall into the freelist are those that have been |
||
489 | * preallocated. |
||
490 | */ |
||
491 | kfree(p); |
||
492 | } |
||
493 | return; |
||
494 | } |
||
495 | |||
496 | |||
497 | /** |
||
498 | * idr_remove_all - remove all ids from the given idr tree |
||
499 | * @idp: idr handle |
||
500 | * |
||
501 | * idr_destroy() only frees up unused, cached idp_layers, but this |
||
502 | * function will remove all id mappings and leave all idp_layers |
||
503 | * unused. |
||
504 | * |
||
2966 | Serge | 505 | * A typical clean-up sequence for objects stored in an idr tree will |
1412 | serge | 506 | * use idr_for_each() to free all objects, if necessay, then |
507 | * idr_remove_all() to remove all ids, and idr_destroy() to free |
||
508 | * up the cached idr_layers. |
||
509 | */ |
||
510 | void idr_remove_all(struct idr *idp) |
||
511 | { |
||
512 | int n, id, max; |
||
2966 | Serge | 513 | int bt_mask; |
1412 | serge | 514 | struct idr_layer *p; |
515 | struct idr_layer *pa[MAX_LEVEL]; |
||
516 | struct idr_layer **paa = &pa[0]; |
||
517 | |||
518 | n = idp->layers * IDR_BITS; |
||
519 | p = idp->top; |
||
520 | rcu_assign_pointer(idp->top, NULL); |
||
521 | max = 1 << n; |
||
522 | |||
523 | id = 0; |
||
524 | while (id < max) { |
||
525 | while (n > IDR_BITS && p) { |
||
526 | n -= IDR_BITS; |
||
527 | *paa++ = p; |
||
528 | p = p->ary[(id >> n) & IDR_MASK]; |
||
529 | } |
||
530 | |||
2966 | Serge | 531 | bt_mask = id; |
1412 | serge | 532 | id += 1 << n; |
2966 | Serge | 533 | /* Get the highest bit that the above add changed from 0->1. */ |
534 | while (n < fls(id ^ bt_mask)) { |
||
1412 | serge | 535 | if (p) |
536 | free_layer(p); |
||
537 | n += IDR_BITS; |
||
538 | p = *--paa; |
||
539 | } |
||
540 | } |
||
541 | idp->layers = 0; |
||
542 | } |
||
543 | |||
544 | /** |
||
545 | * idr_destroy - release all cached layers within an idr tree |
||
2966 | Serge | 546 | * @idp: idr handle |
1412 | serge | 547 | */ |
548 | void idr_destroy(struct idr *idp) |
||
549 | { |
||
550 | while (idp->id_free_cnt) { |
||
551 | struct idr_layer *p = get_from_free_list(idp); |
||
552 | kfree(p); |
||
553 | } |
||
554 | } |
||
555 | |||
556 | |||
557 | /** |
||
558 | * idr_find - return pointer for given id |
||
559 | * @idp: idr handle |
||
560 | * @id: lookup key |
||
561 | * |
||
562 | * Return the pointer given the id it has been registered with. A %NULL |
||
563 | * return indicates that @id is not valid or you passed %NULL in |
||
564 | * idr_get_new(). |
||
565 | * |
||
566 | * This function can be called under rcu_read_lock(), given that the leaf |
||
567 | * pointers lifetimes are correctly managed. |
||
568 | */ |
||
569 | void *idr_find(struct idr *idp, int id) |
||
570 | { |
||
571 | int n; |
||
572 | struct idr_layer *p; |
||
573 | |||
574 | p = rcu_dereference(idp->top); |
||
575 | if (!p) |
||
576 | return NULL; |
||
577 | n = (p->layer+1) * IDR_BITS; |
||
578 | |||
579 | /* Mask off upper bits we don't use for the search. */ |
||
580 | id &= MAX_ID_MASK; |
||
581 | |||
582 | if (id >= (1 << n)) |
||
583 | return NULL; |
||
584 | BUG_ON(n == 0); |
||
585 | |||
586 | while (n > 0 && p) { |
||
587 | n -= IDR_BITS; |
||
588 | BUG_ON(n != p->layer*IDR_BITS); |
||
589 | p = rcu_dereference(p->ary[(id >> n) & IDR_MASK]); |
||
590 | } |
||
591 | return((void *)p); |
||
592 | } |
||
593 | |||
594 | #if 0 |
||
595 | /** |
||
596 | * idr_for_each - iterate through all stored pointers |
||
597 | * @idp: idr handle |
||
598 | * @fn: function to be called for each pointer |
||
599 | * @data: data passed back to callback function |
||
600 | * |
||
601 | * Iterate over the pointers registered with the given idr. The |
||
602 | * callback function will be called for each pointer currently |
||
603 | * registered, passing the id, the pointer and the data pointer passed |
||
604 | * to this function. It is not safe to modify the idr tree while in |
||
605 | * the callback, so functions such as idr_get_new and idr_remove are |
||
606 | * not allowed. |
||
607 | * |
||
608 | * We check the return of @fn each time. If it returns anything other |
||
2966 | Serge | 609 | * than %0, we break out and return that value. |
1412 | serge | 610 | * |
611 | * The caller must serialize idr_for_each() vs idr_get_new() and idr_remove(). |
||
612 | */ |
||
613 | int idr_for_each(struct idr *idp, |
||
614 | int (*fn)(int id, void *p, void *data), void *data) |
||
615 | { |
||
616 | int n, id, max, error = 0; |
||
617 | struct idr_layer *p; |
||
618 | struct idr_layer *pa[MAX_LEVEL]; |
||
619 | struct idr_layer **paa = &pa[0]; |
||
620 | |||
621 | n = idp->layers * IDR_BITS; |
||
622 | p = rcu_dereference(idp->top); |
||
623 | max = 1 << n; |
||
624 | |||
625 | id = 0; |
||
626 | while (id < max) { |
||
627 | while (n > 0 && p) { |
||
628 | n -= IDR_BITS; |
||
629 | *paa++ = p; |
||
630 | p = rcu_dereference(p->ary[(id >> n) & IDR_MASK]); |
||
631 | } |
||
632 | |||
633 | if (p) { |
||
634 | error = fn(id, (void *)p, data); |
||
635 | if (error) |
||
636 | break; |
||
637 | } |
||
638 | |||
639 | id += 1 << n; |
||
640 | while (n < fls(id)) { |
||
641 | n += IDR_BITS; |
||
642 | p = *--paa; |
||
643 | } |
||
644 | } |
||
645 | |||
646 | return error; |
||
647 | } |
||
648 | EXPORT_SYMBOL(idr_for_each); |
||
649 | |||
650 | /** |
||
651 | * idr_get_next - lookup next object of id to given id. |
||
652 | * @idp: idr handle |
||
2966 | Serge | 653 | * @nextidp: pointer to lookup key |
1412 | serge | 654 | * |
655 | * Returns pointer to registered object with id, which is next number to |
||
2966 | Serge | 656 | * given id. After being looked up, *@nextidp will be updated for the next |
657 | * iteration. |
||
1412 | serge | 658 | */ |
659 | |||
660 | void *idr_get_next(struct idr *idp, int *nextidp) |
||
661 | { |
||
662 | struct idr_layer *p, *pa[MAX_LEVEL]; |
||
663 | struct idr_layer **paa = &pa[0]; |
||
664 | int id = *nextidp; |
||
665 | int n, max; |
||
666 | |||
667 | /* find first ent */ |
||
668 | n = idp->layers * IDR_BITS; |
||
669 | max = 1 << n; |
||
670 | p = rcu_dereference(idp->top); |
||
671 | if (!p) |
||
672 | return NULL; |
||
673 | |||
674 | while (id < max) { |
||
675 | while (n > 0 && p) { |
||
676 | n -= IDR_BITS; |
||
677 | *paa++ = p; |
||
678 | p = rcu_dereference(p->ary[(id >> n) & IDR_MASK]); |
||
679 | } |
||
680 | |||
681 | if (p) { |
||
682 | *nextidp = id; |
||
683 | return p; |
||
684 | } |
||
685 | |||
686 | id += 1 << n; |
||
687 | while (n < fls(id)) { |
||
688 | n += IDR_BITS; |
||
689 | p = *--paa; |
||
690 | } |
||
691 | } |
||
692 | return NULL; |
||
693 | } |
||
694 | |||
695 | |||
696 | |||
697 | /** |
||
698 | * idr_replace - replace pointer for given id |
||
699 | * @idp: idr handle |
||
700 | * @ptr: pointer you want associated with the id |
||
701 | * @id: lookup key |
||
702 | * |
||
703 | * Replace the pointer registered with an id and return the old value. |
||
2966 | Serge | 704 | * A %-ENOENT return indicates that @id was not found. |
705 | * A %-EINVAL return indicates that @id was not within valid constraints. |
||
1412 | serge | 706 | * |
707 | * The caller must serialize with writers. |
||
708 | */ |
||
709 | void *idr_replace(struct idr *idp, void *ptr, int id) |
||
710 | { |
||
711 | int n; |
||
712 | struct idr_layer *p, *old_p; |
||
713 | |||
714 | p = idp->top; |
||
715 | if (!p) |
||
716 | return ERR_PTR(-EINVAL); |
||
717 | |||
718 | n = (p->layer+1) * IDR_BITS; |
||
719 | |||
720 | id &= MAX_ID_MASK; |
||
721 | |||
722 | if (id >= (1 << n)) |
||
723 | return ERR_PTR(-EINVAL); |
||
724 | |||
725 | n -= IDR_BITS; |
||
726 | while ((n > 0) && p) { |
||
727 | p = p->ary[(id >> n) & IDR_MASK]; |
||
728 | n -= IDR_BITS; |
||
729 | } |
||
730 | |||
731 | n = id & IDR_MASK; |
||
732 | if (unlikely(p == NULL || !test_bit(n, &p->bitmap))) |
||
733 | return ERR_PTR(-ENOENT); |
||
734 | |||
735 | old_p = p->ary[n]; |
||
736 | rcu_assign_pointer(p->ary[n], ptr); |
||
737 | |||
738 | return old_p; |
||
739 | } |
||
740 | EXPORT_SYMBOL(idr_replace); |
||
741 | |||
742 | |||
743 | #endif |
||
744 | |||
745 | |||
746 | void idr_init_cache(void) |
||
747 | { |
||
748 | //idr_layer_cache = kmem_cache_create("idr_layer_cache", |
||
749 | // sizeof(struct idr_layer), 0, SLAB_PANIC, NULL); |
||
750 | } |
||
751 | |||
752 | /** |
||
753 | * idr_init - initialize idr handle |
||
754 | * @idp: idr handle |
||
755 | * |
||
756 | * This function is use to set up the handle (@idp) that you will pass |
||
757 | * to the rest of the functions. |
||
758 | */ |
||
759 | void idr_init(struct idr *idp) |
||
760 | { |
||
761 | memset(idp, 0, sizeof(struct idr)); |
||
762 | // spin_lock_init(&idp->lock); |
||
763 | } |
||
764 | |||
765 | #if 0 |
||
766 | |||
767 | /* |
||
768 | * IDA - IDR based ID allocator |
||
769 | * |
||
2966 | Serge | 770 | * This is id allocator without id -> pointer translation. Memory |
1412 | serge | 771 | * usage is much lower than full blown idr because each id only |
772 | * occupies a bit. ida uses a custom leaf node which contains |
||
773 | * IDA_BITMAP_BITS slots. |
||
774 | * |
||
775 | * 2007-04-25 written by Tejun Heo |
||
776 | */ |
||
777 | |||
778 | static void free_bitmap(struct ida *ida, struct ida_bitmap *bitmap) |
||
779 | { |
||
780 | unsigned long flags; |
||
781 | |||
782 | if (!ida->free_bitmap) { |
||
783 | spin_lock_irqsave(&ida->idr.lock, flags); |
||
784 | if (!ida->free_bitmap) { |
||
785 | ida->free_bitmap = bitmap; |
||
786 | bitmap = NULL; |
||
787 | } |
||
788 | spin_unlock_irqrestore(&ida->idr.lock, flags); |
||
789 | } |
||
790 | |||
791 | kfree(bitmap); |
||
792 | } |
||
793 | |||
794 | /** |
||
795 | * ida_pre_get - reserve resources for ida allocation |
||
796 | * @ida: ida handle |
||
797 | * @gfp_mask: memory allocation flag |
||
798 | * |
||
799 | * This function should be called prior to locking and calling the |
||
800 | * following function. It preallocates enough memory to satisfy the |
||
801 | * worst possible allocation. |
||
802 | * |
||
2966 | Serge | 803 | * If the system is REALLY out of memory this function returns %0, |
804 | * otherwise %1. |
||
1412 | serge | 805 | */ |
806 | int ida_pre_get(struct ida *ida, gfp_t gfp_mask) |
||
807 | { |
||
808 | /* allocate idr_layers */ |
||
809 | if (!idr_pre_get(&ida->idr, gfp_mask)) |
||
810 | return 0; |
||
811 | |||
812 | /* allocate free_bitmap */ |
||
813 | if (!ida->free_bitmap) { |
||
814 | struct ida_bitmap *bitmap; |
||
815 | |||
816 | bitmap = kzalloc(sizeof(struct ida_bitmap), gfp_mask); |
||
817 | if (!bitmap) |
||
818 | return 0; |
||
819 | |||
820 | free_bitmap(ida, bitmap); |
||
821 | } |
||
822 | |||
823 | return 1; |
||
824 | } |
||
825 | EXPORT_SYMBOL(ida_pre_get); |
||
826 | |||
827 | /** |
||
828 | * ida_get_new_above - allocate new ID above or equal to a start id |
||
829 | * @ida: ida handle |
||
2966 | Serge | 830 | * @starting_id: id to start search at |
1412 | serge | 831 | * @p_id: pointer to the allocated handle |
832 | * |
||
2966 | Serge | 833 | * Allocate new ID above or equal to @starting_id. It should be called |
834 | * with any required locks. |
||
1412 | serge | 835 | * |
2966 | Serge | 836 | * If memory is required, it will return %-EAGAIN, you should unlock |
1412 | serge | 837 | * and go back to the ida_pre_get() call. If the ida is full, it will |
2966 | Serge | 838 | * return %-ENOSPC. |
1412 | serge | 839 | * |
2966 | Serge | 840 | * @p_id returns a value in the range @starting_id ... %0x7fffffff. |
1412 | serge | 841 | */ |
842 | int ida_get_new_above(struct ida *ida, int starting_id, int *p_id) |
||
843 | { |
||
844 | struct idr_layer *pa[MAX_LEVEL]; |
||
845 | struct ida_bitmap *bitmap; |
||
846 | unsigned long flags; |
||
847 | int idr_id = starting_id / IDA_BITMAP_BITS; |
||
848 | int offset = starting_id % IDA_BITMAP_BITS; |
||
849 | int t, id; |
||
850 | |||
851 | restart: |
||
852 | /* get vacant slot */ |
||
853 | t = idr_get_empty_slot(&ida->idr, idr_id, pa); |
||
854 | if (t < 0) |
||
855 | return _idr_rc_to_errno(t); |
||
856 | |||
857 | if (t * IDA_BITMAP_BITS >= MAX_ID_BIT) |
||
858 | return -ENOSPC; |
||
859 | |||
860 | if (t != idr_id) |
||
861 | offset = 0; |
||
862 | idr_id = t; |
||
863 | |||
864 | /* if bitmap isn't there, create a new one */ |
||
865 | bitmap = (void *)pa[0]->ary[idr_id & IDR_MASK]; |
||
866 | if (!bitmap) { |
||
867 | spin_lock_irqsave(&ida->idr.lock, flags); |
||
868 | bitmap = ida->free_bitmap; |
||
869 | ida->free_bitmap = NULL; |
||
870 | spin_unlock_irqrestore(&ida->idr.lock, flags); |
||
871 | |||
872 | if (!bitmap) |
||
873 | return -EAGAIN; |
||
874 | |||
875 | memset(bitmap, 0, sizeof(struct ida_bitmap)); |
||
876 | rcu_assign_pointer(pa[0]->ary[idr_id & IDR_MASK], |
||
877 | (void *)bitmap); |
||
878 | pa[0]->count++; |
||
879 | } |
||
880 | |||
881 | /* lookup for empty slot */ |
||
882 | t = find_next_zero_bit(bitmap->bitmap, IDA_BITMAP_BITS, offset); |
||
883 | if (t == IDA_BITMAP_BITS) { |
||
884 | /* no empty slot after offset, continue to the next chunk */ |
||
885 | idr_id++; |
||
886 | offset = 0; |
||
887 | goto restart; |
||
888 | } |
||
889 | |||
890 | id = idr_id * IDA_BITMAP_BITS + t; |
||
891 | if (id >= MAX_ID_BIT) |
||
892 | return -ENOSPC; |
||
893 | |||
894 | __set_bit(t, bitmap->bitmap); |
||
895 | if (++bitmap->nr_busy == IDA_BITMAP_BITS) |
||
896 | idr_mark_full(pa, idr_id); |
||
897 | |||
898 | *p_id = id; |
||
899 | |||
900 | /* Each leaf node can handle nearly a thousand slots and the |
||
901 | * whole idea of ida is to have small memory foot print. |
||
902 | * Throw away extra resources one by one after each successful |
||
903 | * allocation. |
||
904 | */ |
||
905 | if (ida->idr.id_free_cnt || ida->free_bitmap) { |
||
906 | struct idr_layer *p = get_from_free_list(&ida->idr); |
||
907 | if (p) |
||
908 | kmem_cache_free(idr_layer_cache, p); |
||
909 | } |
||
910 | |||
911 | return 0; |
||
912 | } |
||
913 | EXPORT_SYMBOL(ida_get_new_above); |
||
914 | |||
915 | /** |
||
916 | * ida_get_new - allocate new ID |
||
917 | * @ida: idr handle |
||
918 | * @p_id: pointer to the allocated handle |
||
919 | * |
||
920 | * Allocate new ID. It should be called with any required locks. |
||
921 | * |
||
2966 | Serge | 922 | * If memory is required, it will return %-EAGAIN, you should unlock |
1412 | serge | 923 | * and go back to the idr_pre_get() call. If the idr is full, it will |
2966 | Serge | 924 | * return %-ENOSPC. |
1412 | serge | 925 | * |
2966 | Serge | 926 | * @p_id returns a value in the range %0 ... %0x7fffffff. |
1412 | serge | 927 | */ |
928 | int ida_get_new(struct ida *ida, int *p_id) |
||
929 | { |
||
930 | return ida_get_new_above(ida, 0, p_id); |
||
931 | } |
||
932 | EXPORT_SYMBOL(ida_get_new); |
||
933 | |||
934 | /** |
||
935 | * ida_remove - remove the given ID |
||
936 | * @ida: ida handle |
||
937 | * @id: ID to free |
||
938 | */ |
||
939 | void ida_remove(struct ida *ida, int id) |
||
940 | { |
||
941 | struct idr_layer *p = ida->idr.top; |
||
942 | int shift = (ida->idr.layers - 1) * IDR_BITS; |
||
943 | int idr_id = id / IDA_BITMAP_BITS; |
||
944 | int offset = id % IDA_BITMAP_BITS; |
||
945 | int n; |
||
946 | struct ida_bitmap *bitmap; |
||
947 | |||
948 | /* clear full bits while looking up the leaf idr_layer */ |
||
949 | while ((shift > 0) && p) { |
||
950 | n = (idr_id >> shift) & IDR_MASK; |
||
951 | __clear_bit(n, &p->bitmap); |
||
952 | p = p->ary[n]; |
||
953 | shift -= IDR_BITS; |
||
954 | } |
||
955 | |||
956 | if (p == NULL) |
||
957 | goto err; |
||
958 | |||
959 | n = idr_id & IDR_MASK; |
||
960 | __clear_bit(n, &p->bitmap); |
||
961 | |||
962 | bitmap = (void *)p->ary[n]; |
||
963 | if (!test_bit(offset, bitmap->bitmap)) |
||
964 | goto err; |
||
965 | |||
966 | /* update bitmap and remove it if empty */ |
||
967 | __clear_bit(offset, bitmap->bitmap); |
||
968 | if (--bitmap->nr_busy == 0) { |
||
969 | __set_bit(n, &p->bitmap); /* to please idr_remove() */ |
||
970 | idr_remove(&ida->idr, idr_id); |
||
971 | free_bitmap(ida, bitmap); |
||
972 | } |
||
973 | |||
974 | return; |
||
975 | |||
976 | err: |
||
977 | printk(KERN_WARNING |
||
978 | "ida_remove called for id=%d which is not allocated.\n", id); |
||
979 | } |
||
980 | EXPORT_SYMBOL(ida_remove); |
||
981 | |||
982 | /** |
||
983 | * ida_destroy - release all cached layers within an ida tree |
||
2966 | Serge | 984 | * @ida: ida handle |
1412 | serge | 985 | */ |
986 | void ida_destroy(struct ida *ida) |
||
987 | { |
||
988 | idr_destroy(&ida->idr); |
||
989 | kfree(ida->free_bitmap); |
||
990 | } |
||
991 | EXPORT_SYMBOL(ida_destroy); |
||
992 | |||
993 | /** |
||
994 | * ida_init - initialize ida handle |
||
995 | * @ida: ida handle |
||
996 | * |
||
997 | * This function is use to set up the handle (@ida) that you will pass |
||
998 | * to the rest of the functions. |
||
999 | */ |
||
1000 | void ida_init(struct ida *ida) |
||
1001 | { |
||
1002 | memset(ida, 0, sizeof(struct ida)); |
||
1003 | idr_init(&ida->idr); |
||
1004 | |||
1005 | } |
||
1006 | EXPORT_SYMBOL(ida_init); |
||
1007 | |||
1008 | |||
1009 | #endif>><>>><>>><>>><>>><>><>>><>>><>>>><>>>><>><>>> |