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#ifndef TEST
30#include <linux/slab.h>
31#include <linux/init.h>
32#include <linux/module.h>
33#endif
34#include <linux/err.h>
35#include <linux/string.h>
36#include <linux/idr.h>
37
38static struct kmem_cache *idr_layer_cache;
39
40static struct idr_layer *get_from_free_list(struct idr *idp)
41{
42 struct idr_layer *p;
43 unsigned long flags;
44
45 spin_lock_irqsave(&idp->lock, flags);
46 if ((p = idp->id_free)) {
47 idp->id_free = p->ary[0];
48 idp->id_free_cnt--;
49 p->ary[0] = NULL;
50 }
51 spin_unlock_irqrestore(&idp->lock, flags);
52 return(p);
53}
54
55static void idr_layer_rcu_free(struct rcu_head *head)
56{
57 struct idr_layer *layer;
58
59 layer = container_of(head, struct idr_layer, rcu_head);
60 kmem_cache_free(idr_layer_cache, layer);
61}
62
63static inline void free_layer(struct idr_layer *p)
64{
65 call_rcu(&p->rcu_head, idr_layer_rcu_free);
66}
67
68
69static void __move_to_free_list(struct idr *idp, struct idr_layer *p)
70{
71 p->ary[0] = idp->id_free;
72 idp->id_free = p;
73 idp->id_free_cnt++;
74}
75
76static void move_to_free_list(struct idr *idp, struct idr_layer *p)
77{
78 unsigned long flags;
79
80
81
82
83 spin_lock_irqsave(&idp->lock, flags);
84 __move_to_free_list(idp, p);
85 spin_unlock_irqrestore(&idp->lock, flags);
86}
87
88static void idr_mark_full(struct idr_layer **pa, int id)
89{
90 struct idr_layer *p = pa[0];
91 int l = 0;
92
93 __set_bit(id & IDR_MASK, &p->bitmap);
94
95
96
97
98
99
100 while (p->bitmap == IDR_FULL) {
101 if (!(p = pa[++l]))
102 break;
103 id = id >> IDR_BITS;
104 __set_bit((id & IDR_MASK), &p->bitmap);
105 }
106}
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121int idr_pre_get(struct idr *idp, gfp_t gfp_mask)
122{
123 while (idp->id_free_cnt < IDR_FREE_MAX) {
124 struct idr_layer *new;
125 new = kmem_cache_zalloc(idr_layer_cache, gfp_mask);
126 if (new == NULL)
127 return (0);
128 move_to_free_list(idp, new);
129 }
130 return 1;
131}
132EXPORT_SYMBOL(idr_pre_get);
133
134static int sub_alloc(struct idr *idp, int *starting_id, struct idr_layer **pa)
135{
136 int n, m, sh;
137 struct idr_layer *p, *new;
138 int l, id, oid;
139 unsigned long bm;
140
141 id = *starting_id;
142 restart:
143 p = idp->top;
144 l = idp->layers;
145 pa[l--] = NULL;
146 while (1) {
147
148
149
150 n = (id >> (IDR_BITS*l)) & IDR_MASK;
151 bm = ~p->bitmap;
152 m = find_next_bit(&bm, IDR_SIZE, n);
153 if (m == IDR_SIZE) {
154
155 l++;
156 oid = id;
157 id = (id | ((1 << (IDR_BITS * l)) - 1)) + 1;
158
159
160 if (id >= 1 << (idp->layers * IDR_BITS)) {
161 *starting_id = id;
162 return IDR_NEED_TO_GROW;
163 }
164 p = pa[l];
165 BUG_ON(!p);
166
167
168
169
170 sh = IDR_BITS * (l + 1);
171 if (oid >> sh == id >> sh)
172 continue;
173 else
174 goto restart;
175 }
176 if (m != n) {
177 sh = IDR_BITS*l;
178 id = ((id >> sh) ^ n ^ m) << sh;
179 }
180 if ((id >= MAX_ID_BIT) || (id < 0))
181 return IDR_NOMORE_SPACE;
182 if (l == 0)
183 break;
184
185
186
187 if (!p->ary[m]) {
188 new = get_from_free_list(idp);
189 if (!new)
190 return -1;
191 new->layer = l-1;
192 rcu_assign_pointer(p->ary[m], new);
193 p->count++;
194 }
195 pa[l--] = p;
196 p = p->ary[m];
197 }
198
199 pa[l] = p;
200 return id;
201}
202
203static int idr_get_empty_slot(struct idr *idp, int starting_id,
204 struct idr_layer **pa)
205{
206 struct idr_layer *p, *new;
207 int layers, v, id;
208 unsigned long flags;
209
210 id = starting_id;
211build_up:
212 p = idp->top;
213 layers = idp->layers;
214 if (unlikely(!p)) {
215 if (!(p = get_from_free_list(idp)))
216 return -1;
217 p->layer = 0;
218 layers = 1;
219 }
220
221
222
223
224 while ((layers < (MAX_LEVEL - 1)) && (id >= (1 << (layers*IDR_BITS)))) {
225 layers++;
226 if (!p->count) {
227
228
229
230
231 p->layer++;
232 continue;
233 }
234 if (!(new = get_from_free_list(idp))) {
235
236
237
238
239 spin_lock_irqsave(&idp->lock, flags);
240 for (new = p; p && p != idp->top; new = p) {
241 p = p->ary[0];
242 new->ary[0] = NULL;
243 new->bitmap = new->count = 0;
244 __move_to_free_list(idp, new);
245 }
246 spin_unlock_irqrestore(&idp->lock, flags);
247 return -1;
248 }
249 new->ary[0] = p;
250 new->count = 1;
251 new->layer = layers-1;
252 if (p->bitmap == IDR_FULL)
253 __set_bit(0, &new->bitmap);
254 p = new;
255 }
256 rcu_assign_pointer(idp->top, p);
257 idp->layers = layers;
258 v = sub_alloc(idp, &id, pa);
259 if (v == IDR_NEED_TO_GROW)
260 goto build_up;
261 return(v);
262}
263
264static int idr_get_new_above_int(struct idr *idp, void *ptr, int starting_id)
265{
266 struct idr_layer *pa[MAX_LEVEL];
267 int id;
268
269 id = idr_get_empty_slot(idp, starting_id, pa);
270 if (id >= 0) {
271
272
273
274
275 rcu_assign_pointer(pa[0]->ary[id & IDR_MASK],
276 (struct idr_layer *)ptr);
277 pa[0]->count++;
278 idr_mark_full(pa, id);
279 }
280
281 return id;
282}
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302int idr_get_new_above(struct idr *idp, void *ptr, int starting_id, int *id)
303{
304 int rv;
305
306 rv = idr_get_new_above_int(idp, ptr, starting_id);
307
308
309
310
311 if (rv < 0)
312 return _idr_rc_to_errno(rv);
313 *id = rv;
314 return 0;
315}
316EXPORT_SYMBOL(idr_get_new_above);
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332int idr_get_new(struct idr *idp, void *ptr, int *id)
333{
334 int rv;
335
336 rv = idr_get_new_above_int(idp, ptr, 0);
337
338
339
340
341 if (rv < 0)
342 return _idr_rc_to_errno(rv);
343 *id = rv;
344 return 0;
345}
346EXPORT_SYMBOL(idr_get_new);
347
348static void idr_remove_warning(int id)
349{
350 printk(KERN_WARNING
351 "idr_remove called for id=%d which is not allocated.\n", id);
352 dump_stack();
353}
354
355static void sub_remove(struct idr *idp, int shift, int id)
356{
357 struct idr_layer *p = idp->top;
358 struct idr_layer **pa[MAX_LEVEL];
359 struct idr_layer ***paa = &pa[0];
360 struct idr_layer *to_free;
361 int n;
362
363 *paa = NULL;
364 *++paa = &idp->top;
365
366 while ((shift > 0) && p) {
367 n = (id >> shift) & IDR_MASK;
368 __clear_bit(n, &p->bitmap);
369 *++paa = &p->ary[n];
370 p = p->ary[n];
371 shift -= IDR_BITS;
372 }
373 n = id & IDR_MASK;
374 if (likely(p != NULL && test_bit(n, &p->bitmap))){
375 __clear_bit(n, &p->bitmap);
376 rcu_assign_pointer(p->ary[n], NULL);
377 to_free = NULL;
378 while(*paa && ! --((**paa)->count)){
379 if (to_free)
380 free_layer(to_free);
381 to_free = **paa;
382 **paa-- = NULL;
383 }
384 if (!*paa)
385 idp->layers = 0;
386 if (to_free)
387 free_layer(to_free);
388 } else
389 idr_remove_warning(id);
390}
391
392
393
394
395
396
397void idr_remove(struct idr *idp, int id)
398{
399 struct idr_layer *p;
400 struct idr_layer *to_free;
401
402
403 id &= MAX_ID_MASK;
404
405 sub_remove(idp, (idp->layers - 1) * IDR_BITS, id);
406 if (idp->top && idp->top->count == 1 && (idp->layers > 1) &&
407 idp->top->ary[0]) {
408
409
410
411
412
413
414 to_free = idp->top;
415 p = idp->top->ary[0];
416 rcu_assign_pointer(idp->top, p);
417 --idp->layers;
418 to_free->bitmap = to_free->count = 0;
419 free_layer(to_free);
420 }
421 while (idp->id_free_cnt >= IDR_FREE_MAX) {
422 p = get_from_free_list(idp);
423
424
425
426
427
428 kmem_cache_free(idr_layer_cache, p);
429 }
430 return;
431}
432EXPORT_SYMBOL(idr_remove);
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447void idr_remove_all(struct idr *idp)
448{
449 int n, id, max;
450 int bt_mask;
451 struct idr_layer *p;
452 struct idr_layer *pa[MAX_LEVEL];
453 struct idr_layer **paa = &pa[0];
454
455 n = idp->layers * IDR_BITS;
456 p = idp->top;
457 rcu_assign_pointer(idp->top, NULL);
458 max = 1 << n;
459
460 id = 0;
461 while (id < max) {
462 while (n > IDR_BITS && p) {
463 n -= IDR_BITS;
464 *paa++ = p;
465 p = p->ary[(id >> n) & IDR_MASK];
466 }
467
468 bt_mask = id;
469 id += 1 << n;
470
471 while (n < fls(id ^ bt_mask)) {
472 if (p)
473 free_layer(p);
474 n += IDR_BITS;
475 p = *--paa;
476 }
477 }
478 idp->layers = 0;
479}
480EXPORT_SYMBOL(idr_remove_all);
481
482
483
484
485
486void idr_destroy(struct idr *idp)
487{
488 while (idp->id_free_cnt) {
489 struct idr_layer *p = get_from_free_list(idp);
490 kmem_cache_free(idr_layer_cache, p);
491 }
492}
493EXPORT_SYMBOL(idr_destroy);
494
495
496
497
498
499
500
501
502
503
504
505
506
507void *idr_find(struct idr *idp, int id)
508{
509 int n;
510 struct idr_layer *p;
511
512 p = rcu_dereference_raw(idp->top);
513 if (!p)
514 return NULL;
515 n = (p->layer+1) * IDR_BITS;
516
517
518 id &= MAX_ID_MASK;
519
520 if (id >= (1 << n))
521 return NULL;
522 BUG_ON(n == 0);
523
524 while (n > 0 && p) {
525 n -= IDR_BITS;
526 BUG_ON(n != p->layer*IDR_BITS);
527 p = rcu_dereference_raw(p->ary[(id >> n) & IDR_MASK]);
528 }
529 return((void *)p);
530}
531EXPORT_SYMBOL(idr_find);
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551int idr_for_each(struct idr *idp,
552 int (*fn)(int id, void *p, void *data), void *data)
553{
554 int n, id, max, error = 0;
555 struct idr_layer *p;
556 struct idr_layer *pa[MAX_LEVEL];
557 struct idr_layer **paa = &pa[0];
558
559 n = idp->layers * IDR_BITS;
560 p = rcu_dereference_raw(idp->top);
561 max = 1 << n;
562
563 id = 0;
564 while (id < max) {
565 while (n > 0 && p) {
566 n -= IDR_BITS;
567 *paa++ = p;
568 p = rcu_dereference_raw(p->ary[(id >> n) & IDR_MASK]);
569 }
570
571 if (p) {
572 error = fn(id, (void *)p, data);
573 if (error)
574 break;
575 }
576
577 id += 1 << n;
578 while (n < fls(id)) {
579 n += IDR_BITS;
580 p = *--paa;
581 }
582 }
583
584 return error;
585}
586EXPORT_SYMBOL(idr_for_each);
587
588
589
590
591
592
593
594
595
596
597
598void *idr_get_next(struct idr *idp, int *nextidp)
599{
600 struct idr_layer *p, *pa[MAX_LEVEL];
601 struct idr_layer **paa = &pa[0];
602 int id = *nextidp;
603 int n, max;
604
605
606 n = idp->layers * IDR_BITS;
607 max = 1 << n;
608 p = rcu_dereference_raw(idp->top);
609 if (!p)
610 return NULL;
611
612 while (id < max) {
613 while (n > 0 && p) {
614 n -= IDR_BITS;
615 *paa++ = p;
616 p = rcu_dereference_raw(p->ary[(id >> n) & IDR_MASK]);
617 }
618
619 if (p) {
620 *nextidp = id;
621 return p;
622 }
623
624 id += 1 << n;
625 while (n < fls(id)) {
626 n += IDR_BITS;
627 p = *--paa;
628 }
629 }
630 return NULL;
631}
632EXPORT_SYMBOL(idr_get_next);
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647void *idr_replace(struct idr *idp, void *ptr, int id)
648{
649 int n;
650 struct idr_layer *p, *old_p;
651
652 p = idp->top;
653 if (!p)
654 return ERR_PTR(-EINVAL);
655
656 n = (p->layer+1) * IDR_BITS;
657
658 id &= MAX_ID_MASK;
659
660 if (id >= (1 << n))
661 return ERR_PTR(-EINVAL);
662
663 n -= IDR_BITS;
664 while ((n > 0) && p) {
665 p = p->ary[(id >> n) & IDR_MASK];
666 n -= IDR_BITS;
667 }
668
669 n = id & IDR_MASK;
670 if (unlikely(p == NULL || !test_bit(n, &p->bitmap)))
671 return ERR_PTR(-ENOENT);
672
673 old_p = p->ary[n];
674 rcu_assign_pointer(p->ary[n], ptr);
675
676 return old_p;
677}
678EXPORT_SYMBOL(idr_replace);
679
680void __init idr_init_cache(void)
681{
682 idr_layer_cache = kmem_cache_create("idr_layer_cache",
683 sizeof(struct idr_layer), 0, SLAB_PANIC, NULL);
684}
685
686
687
688
689
690
691
692
693void idr_init(struct idr *idp)
694{
695 memset(idp, 0, sizeof(struct idr));
696 spin_lock_init(&idp->lock);
697}
698EXPORT_SYMBOL(idr_init);
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713static void free_bitmap(struct ida *ida, struct ida_bitmap *bitmap)
714{
715 unsigned long flags;
716
717 if (!ida->free_bitmap) {
718 spin_lock_irqsave(&ida->idr.lock, flags);
719 if (!ida->free_bitmap) {
720 ida->free_bitmap = bitmap;
721 bitmap = NULL;
722 }
723 spin_unlock_irqrestore(&ida->idr.lock, flags);
724 }
725
726 kfree(bitmap);
727}
728
729
730
731
732
733
734
735
736
737
738
739
740
741int ida_pre_get(struct ida *ida, gfp_t gfp_mask)
742{
743
744 if (!idr_pre_get(&ida->idr, gfp_mask))
745 return 0;
746
747
748 if (!ida->free_bitmap) {
749 struct ida_bitmap *bitmap;
750
751 bitmap = kmalloc(sizeof(struct ida_bitmap), gfp_mask);
752 if (!bitmap)
753 return 0;
754
755 free_bitmap(ida, bitmap);
756 }
757
758 return 1;
759}
760EXPORT_SYMBOL(ida_pre_get);
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777int ida_get_new_above(struct ida *ida, int starting_id, int *p_id)
778{
779 struct idr_layer *pa[MAX_LEVEL];
780 struct ida_bitmap *bitmap;
781 unsigned long flags;
782 int idr_id = starting_id / IDA_BITMAP_BITS;
783 int offset = starting_id % IDA_BITMAP_BITS;
784 int t, id;
785
786 restart:
787
788 t = idr_get_empty_slot(&ida->idr, idr_id, pa);
789 if (t < 0)
790 return _idr_rc_to_errno(t);
791
792 if (t * IDA_BITMAP_BITS >= MAX_ID_BIT)
793 return -ENOSPC;
794
795 if (t != idr_id)
796 offset = 0;
797 idr_id = t;
798
799
800 bitmap = (void *)pa[0]->ary[idr_id & IDR_MASK];
801 if (!bitmap) {
802 spin_lock_irqsave(&ida->idr.lock, flags);
803 bitmap = ida->free_bitmap;
804 ida->free_bitmap = NULL;
805 spin_unlock_irqrestore(&ida->idr.lock, flags);
806
807 if (!bitmap)
808 return -EAGAIN;
809
810 memset(bitmap, 0, sizeof(struct ida_bitmap));
811 rcu_assign_pointer(pa[0]->ary[idr_id & IDR_MASK],
812 (void *)bitmap);
813 pa[0]->count++;
814 }
815
816
817 t = find_next_zero_bit(bitmap->bitmap, IDA_BITMAP_BITS, offset);
818 if (t == IDA_BITMAP_BITS) {
819
820 idr_id++;
821 offset = 0;
822 goto restart;
823 }
824
825 id = idr_id * IDA_BITMAP_BITS + t;
826 if (id >= MAX_ID_BIT)
827 return -ENOSPC;
828
829 __set_bit(t, bitmap->bitmap);
830 if (++bitmap->nr_busy == IDA_BITMAP_BITS)
831 idr_mark_full(pa, idr_id);
832
833 *p_id = id;
834
835
836
837
838
839
840 if (ida->idr.id_free_cnt || ida->free_bitmap) {
841 struct idr_layer *p = get_from_free_list(&ida->idr);
842 if (p)
843 kmem_cache_free(idr_layer_cache, p);
844 }
845
846 return 0;
847}
848EXPORT_SYMBOL(ida_get_new_above);
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863int ida_get_new(struct ida *ida, int *p_id)
864{
865 return ida_get_new_above(ida, 0, p_id);
866}
867EXPORT_SYMBOL(ida_get_new);
868
869
870
871
872
873
874void ida_remove(struct ida *ida, int id)
875{
876 struct idr_layer *p = ida->idr.top;
877 int shift = (ida->idr.layers - 1) * IDR_BITS;
878 int idr_id = id / IDA_BITMAP_BITS;
879 int offset = id % IDA_BITMAP_BITS;
880 int n;
881 struct ida_bitmap *bitmap;
882
883
884 while ((shift > 0) && p) {
885 n = (idr_id >> shift) & IDR_MASK;
886 __clear_bit(n, &p->bitmap);
887 p = p->ary[n];
888 shift -= IDR_BITS;
889 }
890
891 if (p == NULL)
892 goto err;
893
894 n = idr_id & IDR_MASK;
895 __clear_bit(n, &p->bitmap);
896
897 bitmap = (void *)p->ary[n];
898 if (!test_bit(offset, bitmap->bitmap))
899 goto err;
900
901
902 __clear_bit(offset, bitmap->bitmap);
903 if (--bitmap->nr_busy == 0) {
904 __set_bit(n, &p->bitmap);
905 idr_remove(&ida->idr, idr_id);
906 free_bitmap(ida, bitmap);
907 }
908
909 return;
910
911 err:
912 printk(KERN_WARNING
913 "ida_remove called for id=%d which is not allocated.\n", id);
914}
915EXPORT_SYMBOL(ida_remove);
916
917
918
919
920
921void ida_destroy(struct ida *ida)
922{
923 idr_destroy(&ida->idr);
924 kfree(ida->free_bitmap);
925}
926EXPORT_SYMBOL(ida_destroy);
927
928
929
930
931
932
933
934
935void ida_init(struct ida *ida)
936{
937 memset(ida, 0, sizeof(struct ida));
938 idr_init(&ida->idr);
939
940}
941EXPORT_SYMBOL(ida_init);
942