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.
6 * Modified by George Anzinger to reuse immediately and to use
7 * find bit instructions. Also removed _irq on spinlocks.
9 * Modified by Nadia Derbey to make it RCU safe.
11 * Small id to pointer translation service.
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
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.
23 #ifndef TEST // to test in user space...
24 #include <linux/slab.h>
25 #include <linux/init.h>
26 #include <linux/export.h>
28 #include <linux/err.h>
29 #include <linux/string.h>
30 #include <linux/idr.h>
31 #include <linux/spinlock.h>
32 #include <linux/percpu.h>
33 #include <linux/locallock.h>
35 #define MAX_IDR_SHIFT (sizeof(int) * 8 - 1)
36 #define MAX_IDR_BIT (1U << MAX_IDR_SHIFT)
38 /* Leave the possibility of an incomplete final layer */
39 #define MAX_IDR_LEVEL ((MAX_IDR_SHIFT + IDR_BITS - 1) / IDR_BITS)
41 /* Number of id_layer structs to leave in free list */
42 #define MAX_IDR_FREE (MAX_IDR_LEVEL * 2)
44 static struct kmem_cache *idr_layer_cache;
45 static DEFINE_PER_CPU(struct idr_layer *, idr_preload_head);
46 static DEFINE_PER_CPU(int, idr_preload_cnt);
47 static DEFINE_SPINLOCK(simple_ida_lock);
49 #ifdef CONFIG_PREEMPT_RT_FULL
50 static DEFINE_LOCAL_IRQ_LOCK(idr_lock);
52 static inline void idr_preload_lock(void)
57 static inline void idr_preload_unlock(void)
59 local_unlock(idr_lock);
62 void idr_preload_end(void)
66 EXPORT_SYMBOL(idr_preload_end);
68 static inline void idr_preload_lock(void)
73 static inline void idr_preload_unlock(void)
80 /* the maximum ID which can be allocated given idr->layers */
81 static int idr_max(int layers)
83 int bits = min_t(int, layers * IDR_BITS, MAX_IDR_SHIFT);
85 return (1 << bits) - 1;
89 * Prefix mask for an idr_layer at @layer. For layer 0, the prefix mask is
90 * all bits except for the lower IDR_BITS. For layer 1, 2 * IDR_BITS, and
93 static int idr_layer_prefix_mask(int layer)
95 return ~idr_max(layer + 1);
98 static struct idr_layer *get_from_free_list(struct idr *idp)
103 spin_lock_irqsave(&idp->lock, flags);
104 if ((p = idp->id_free)) {
105 idp->id_free = p->ary[0];
109 spin_unlock_irqrestore(&idp->lock, flags);
114 * idr_layer_alloc - allocate a new idr_layer
115 * @gfp_mask: allocation mask
116 * @layer_idr: optional idr to allocate from
118 * If @layer_idr is %NULL, directly allocate one using @gfp_mask or fetch
119 * one from the per-cpu preload buffer. If @layer_idr is not %NULL, fetch
120 * an idr_layer from @idr->id_free.
122 * @layer_idr is to maintain backward compatibility with the old alloc
123 * interface - idr_pre_get() and idr_get_new*() - and will be removed
124 * together with per-pool preload buffer.
126 static struct idr_layer *idr_layer_alloc(gfp_t gfp_mask, struct idr *layer_idr)
128 struct idr_layer *new;
130 /* this is the old path, bypass to get_from_free_list() */
132 return get_from_free_list(layer_idr);
135 * Try to allocate directly from kmem_cache. We want to try this
136 * before preload buffer; otherwise, non-preloading idr_alloc()
137 * users will end up taking advantage of preloading ones. As the
138 * following is allowed to fail for preloaded cases, suppress
141 new = kmem_cache_zalloc(idr_layer_cache, gfp_mask | __GFP_NOWARN);
146 * Try to fetch one from the per-cpu preload buffer if in process
147 * context. See idr_preload() for details.
149 if (!in_interrupt()) {
151 new = __this_cpu_read(idr_preload_head);
153 __this_cpu_write(idr_preload_head, new->ary[0]);
154 __this_cpu_dec(idr_preload_cnt);
157 idr_preload_unlock();
163 * Both failed. Try kmem_cache again w/o adding __GFP_NOWARN so
164 * that memory allocation failure warning is printed as intended.
166 return kmem_cache_zalloc(idr_layer_cache, gfp_mask);
169 static void idr_layer_rcu_free(struct rcu_head *head)
171 struct idr_layer *layer;
173 layer = container_of(head, struct idr_layer, rcu_head);
174 kmem_cache_free(idr_layer_cache, layer);
177 static inline void free_layer(struct idr *idr, struct idr_layer *p)
180 RCU_INIT_POINTER(idr->hint, NULL);
181 call_rcu(&p->rcu_head, idr_layer_rcu_free);
184 /* only called when idp->lock is held */
185 static void __move_to_free_list(struct idr *idp, struct idr_layer *p)
187 p->ary[0] = idp->id_free;
192 static void move_to_free_list(struct idr *idp, struct idr_layer *p)
197 * Depends on the return element being zeroed.
199 spin_lock_irqsave(&idp->lock, flags);
200 __move_to_free_list(idp, p);
201 spin_unlock_irqrestore(&idp->lock, flags);
204 static void idr_mark_full(struct idr_layer **pa, int id)
206 struct idr_layer *p = pa[0];
209 __set_bit(id & IDR_MASK, p->bitmap);
211 * If this layer is full mark the bit in the layer above to
212 * show that this part of the radix tree is full. This may
213 * complete the layer above and require walking up the radix
216 while (bitmap_full(p->bitmap, IDR_SIZE)) {
220 __set_bit((id & IDR_MASK), p->bitmap);
224 static int __idr_pre_get(struct idr *idp, gfp_t gfp_mask)
226 while (idp->id_free_cnt < MAX_IDR_FREE) {
227 struct idr_layer *new;
228 new = kmem_cache_zalloc(idr_layer_cache, gfp_mask);
231 move_to_free_list(idp, new);
237 * sub_alloc - try to allocate an id without growing the tree depth
239 * @starting_id: id to start search at
240 * @pa: idr_layer[MAX_IDR_LEVEL] used as backtrack buffer
241 * @gfp_mask: allocation mask for idr_layer_alloc()
242 * @layer_idr: optional idr passed to idr_layer_alloc()
244 * Allocate an id in range [@starting_id, INT_MAX] from @idp without
245 * growing its depth. Returns
247 * the allocated id >= 0 if successful,
248 * -EAGAIN if the tree needs to grow for allocation to succeed,
249 * -ENOSPC if the id space is exhausted,
250 * -ENOMEM if more idr_layers need to be allocated.
252 static int sub_alloc(struct idr *idp, int *starting_id, struct idr_layer **pa,
253 gfp_t gfp_mask, struct idr *layer_idr)
256 struct idr_layer *p, *new;
266 * We run around this while until we reach the leaf node...
268 n = (id >> (IDR_BITS*l)) & IDR_MASK;
269 m = find_next_zero_bit(p->bitmap, IDR_SIZE, n);
271 /* no space available go back to previous layer. */
274 id = (id | ((1 << (IDR_BITS * l)) - 1)) + 1;
276 /* if already at the top layer, we need to grow */
277 if (id > idr_max(idp->layers)) {
284 /* If we need to go up one layer, continue the
285 * loop; otherwise, restart from the top.
287 sh = IDR_BITS * (l + 1);
288 if (oid >> sh == id >> sh)
295 id = ((id >> sh) ^ n ^ m) << sh;
297 if ((id >= MAX_IDR_BIT) || (id < 0))
302 * Create the layer below if it is missing.
305 new = idr_layer_alloc(gfp_mask, layer_idr);
309 new->prefix = id & idr_layer_prefix_mask(new->layer);
310 rcu_assign_pointer(p->ary[m], new);
321 static int idr_get_empty_slot(struct idr *idp, int starting_id,
322 struct idr_layer **pa, gfp_t gfp_mask,
323 struct idr *layer_idr)
325 struct idr_layer *p, *new;
332 layers = idp->layers;
334 if (!(p = idr_layer_alloc(gfp_mask, layer_idr)))
340 * Add a new layer to the top of the tree if the requested
341 * id is larger than the currently allocated space.
343 while (id > idr_max(layers)) {
346 /* special case: if the tree is currently empty,
347 * then we grow the tree by moving the top node
351 WARN_ON_ONCE(p->prefix);
354 if (!(new = idr_layer_alloc(gfp_mask, layer_idr))) {
356 * The allocation failed. If we built part of
357 * the structure tear it down.
359 spin_lock_irqsave(&idp->lock, flags);
360 for (new = p; p && p != idp->top; new = p) {
364 bitmap_clear(new->bitmap, 0, IDR_SIZE);
365 __move_to_free_list(idp, new);
367 spin_unlock_irqrestore(&idp->lock, flags);
372 new->layer = layers-1;
373 new->prefix = id & idr_layer_prefix_mask(new->layer);
374 if (bitmap_full(p->bitmap, IDR_SIZE))
375 __set_bit(0, new->bitmap);
378 rcu_assign_pointer(idp->top, p);
379 idp->layers = layers;
380 v = sub_alloc(idp, &id, pa, gfp_mask, layer_idr);
387 * @id and @pa are from a successful allocation from idr_get_empty_slot().
388 * Install the user pointer @ptr and mark the slot full.
390 static void idr_fill_slot(struct idr *idr, void *ptr, int id,
391 struct idr_layer **pa)
393 /* update hint used for lookup, cleared from free_layer() */
394 rcu_assign_pointer(idr->hint, pa[0]);
396 rcu_assign_pointer(pa[0]->ary[id & IDR_MASK], (struct idr_layer *)ptr);
398 idr_mark_full(pa, id);
402 * idr_preload - preload for idr_alloc()
403 * @gfp_mask: allocation mask to use for preloading
405 * Preload per-cpu layer buffer for idr_alloc(). Can only be used from
406 * process context and each idr_preload() invocation should be matched with
407 * idr_preload_end(). Note that preemption is disabled while preloaded.
409 * The first idr_alloc() in the preloaded section can be treated as if it
410 * were invoked with @gfp_mask used for preloading. This allows using more
411 * permissive allocation masks for idrs protected by spinlocks.
413 * For example, if idr_alloc() below fails, the failure can be treated as
414 * if idr_alloc() were called with GFP_KERNEL rather than GFP_NOWAIT.
416 * idr_preload(GFP_KERNEL);
419 * id = idr_alloc(idr, ptr, start, end, GFP_NOWAIT);
426 void idr_preload(gfp_t gfp_mask)
429 * Consuming preload buffer from non-process context breaks preload
430 * allocation guarantee. Disallow usage from those contexts.
432 WARN_ON_ONCE(in_interrupt());
433 might_sleep_if(gfpflags_allow_blocking(gfp_mask));
438 * idr_alloc() is likely to succeed w/o full idr_layer buffer and
439 * return value from idr_alloc() needs to be checked for failure
440 * anyway. Silently give up if allocation fails. The caller can
441 * treat failures from idr_alloc() as if idr_alloc() were called
442 * with @gfp_mask which should be enough.
444 while (__this_cpu_read(idr_preload_cnt) < MAX_IDR_FREE) {
445 struct idr_layer *new;
447 idr_preload_unlock();
448 new = kmem_cache_zalloc(idr_layer_cache, gfp_mask);
453 /* link the new one to per-cpu preload list */
454 new->ary[0] = __this_cpu_read(idr_preload_head);
455 __this_cpu_write(idr_preload_head, new);
456 __this_cpu_inc(idr_preload_cnt);
459 EXPORT_SYMBOL(idr_preload);
462 * idr_alloc - allocate new idr entry
463 * @idr: the (initialized) idr
464 * @ptr: pointer to be associated with the new id
465 * @start: the minimum id (inclusive)
466 * @end: the maximum id (exclusive, <= 0 for max)
467 * @gfp_mask: memory allocation flags
469 * Allocate an id in [start, end) and associate it with @ptr. If no ID is
470 * available in the specified range, returns -ENOSPC. On memory allocation
471 * failure, returns -ENOMEM.
473 * Note that @end is treated as max when <= 0. This is to always allow
474 * using @start + N as @end as long as N is inside integer range.
476 * The user is responsible for exclusively synchronizing all operations
477 * which may modify @idr. However, read-only accesses such as idr_find()
478 * or iteration can be performed under RCU read lock provided the user
479 * destroys @ptr in RCU-safe way after removal from idr.
481 int idr_alloc(struct idr *idr, void *ptr, int start, int end, gfp_t gfp_mask)
483 int max = end > 0 ? end - 1 : INT_MAX; /* inclusive upper limit */
484 struct idr_layer *pa[MAX_IDR_LEVEL + 1];
487 might_sleep_if(gfpflags_allow_blocking(gfp_mask));
490 if (WARN_ON_ONCE(start < 0))
492 if (unlikely(max < start))
496 id = idr_get_empty_slot(idr, start, pa, gfp_mask, NULL);
497 if (unlikely(id < 0))
499 if (unlikely(id > max))
502 idr_fill_slot(idr, ptr, id, pa);
505 EXPORT_SYMBOL_GPL(idr_alloc);
508 * idr_alloc_cyclic - allocate new idr entry in a cyclical fashion
509 * @idr: the (initialized) idr
510 * @ptr: pointer to be associated with the new id
511 * @start: the minimum id (inclusive)
512 * @end: the maximum id (exclusive, <= 0 for max)
513 * @gfp_mask: memory allocation flags
515 * Essentially the same as idr_alloc, but prefers to allocate progressively
516 * higher ids if it can. If the "cur" counter wraps, then it will start again
517 * at the "start" end of the range and allocate one that has already been used.
519 int idr_alloc_cyclic(struct idr *idr, void *ptr, int start, int end,
524 id = idr_alloc(idr, ptr, max(start, idr->cur), end, gfp_mask);
526 id = idr_alloc(idr, ptr, start, end, gfp_mask);
532 EXPORT_SYMBOL(idr_alloc_cyclic);
534 static void idr_remove_warning(int id)
536 WARN(1, "idr_remove called for id=%d which is not allocated.\n", id);
539 static void sub_remove(struct idr *idp, int shift, int id)
541 struct idr_layer *p = idp->top;
542 struct idr_layer **pa[MAX_IDR_LEVEL + 1];
543 struct idr_layer ***paa = &pa[0];
544 struct idr_layer *to_free;
550 while ((shift > 0) && p) {
551 n = (id >> shift) & IDR_MASK;
552 __clear_bit(n, p->bitmap);
558 if (likely(p != NULL && test_bit(n, p->bitmap))) {
559 __clear_bit(n, p->bitmap);
560 RCU_INIT_POINTER(p->ary[n], NULL);
562 while(*paa && ! --((**paa)->count)){
564 free_layer(idp, to_free);
571 free_layer(idp, to_free);
573 idr_remove_warning(id);
577 * idr_remove - remove the given id and free its slot
581 void idr_remove(struct idr *idp, int id)
584 struct idr_layer *to_free;
589 if (id > idr_max(idp->layers)) {
590 idr_remove_warning(id);
594 sub_remove(idp, (idp->layers - 1) * IDR_BITS, id);
595 if (idp->top && idp->top->count == 1 && (idp->layers > 1) &&
598 * Single child at leftmost slot: we can shrink the tree.
599 * This level is not needed anymore since when layers are
600 * inserted, they are inserted at the top of the existing
604 p = idp->top->ary[0];
605 rcu_assign_pointer(idp->top, p);
608 bitmap_clear(to_free->bitmap, 0, IDR_SIZE);
609 free_layer(idp, to_free);
612 EXPORT_SYMBOL(idr_remove);
614 static void __idr_remove_all(struct idr *idp)
619 struct idr_layer *pa[MAX_IDR_LEVEL + 1];
620 struct idr_layer **paa = &pa[0];
622 n = idp->layers * IDR_BITS;
624 RCU_INIT_POINTER(idp->top, NULL);
625 max = idr_max(idp->layers);
628 while (id >= 0 && id <= max) {
630 while (n > IDR_BITS && p) {
632 p = p->ary[(id >> n) & IDR_MASK];
638 /* Get the highest bit that the above add changed from 0->1. */
639 while (n < fls(id ^ bt_mask)) {
641 free_layer(idp, *paa);
650 * idr_destroy - release all cached layers within an idr tree
653 * Free all id mappings and all idp_layers. After this function, @idp is
654 * completely unused and can be freed / recycled. The caller is
655 * responsible for ensuring that no one else accesses @idp during or after
658 * A typical clean-up sequence for objects stored in an idr tree will use
659 * idr_for_each() to free all objects, if necessary, then idr_destroy() to
660 * free up the id mappings and cached idr_layers.
662 void idr_destroy(struct idr *idp)
664 __idr_remove_all(idp);
666 while (idp->id_free_cnt) {
667 struct idr_layer *p = get_from_free_list(idp);
668 kmem_cache_free(idr_layer_cache, p);
671 EXPORT_SYMBOL(idr_destroy);
673 void *idr_find_slowpath(struct idr *idp, int id)
681 p = rcu_dereference_raw(idp->top);
684 n = (p->layer+1) * IDR_BITS;
686 if (id > idr_max(p->layer + 1))
692 BUG_ON(n != p->layer*IDR_BITS);
693 p = rcu_dereference_raw(p->ary[(id >> n) & IDR_MASK]);
697 EXPORT_SYMBOL(idr_find_slowpath);
700 * idr_for_each - iterate through all stored pointers
702 * @fn: function to be called for each pointer
703 * @data: data passed back to callback function
705 * Iterate over the pointers registered with the given idr. The
706 * callback function will be called for each pointer currently
707 * registered, passing the id, the pointer and the data pointer passed
708 * to this function. It is not safe to modify the idr tree while in
709 * the callback, so functions such as idr_get_new and idr_remove are
712 * We check the return of @fn each time. If it returns anything other
713 * than %0, we break out and return that value.
715 * The caller must serialize idr_for_each() vs idr_get_new() and idr_remove().
717 int idr_for_each(struct idr *idp,
718 int (*fn)(int id, void *p, void *data), void *data)
720 int n, id, max, error = 0;
722 struct idr_layer *pa[MAX_IDR_LEVEL + 1];
723 struct idr_layer **paa = &pa[0];
725 n = idp->layers * IDR_BITS;
726 *paa = rcu_dereference_raw(idp->top);
727 max = idr_max(idp->layers);
730 while (id >= 0 && id <= max) {
734 p = rcu_dereference_raw(p->ary[(id >> n) & IDR_MASK]);
739 error = fn(id, (void *)p, data);
745 while (n < fls(id)) {
753 EXPORT_SYMBOL(idr_for_each);
756 * idr_get_next - lookup next object of id to given id.
758 * @nextidp: pointer to lookup key
760 * Returns pointer to registered object with id, which is next number to
761 * given id. After being looked up, *@nextidp will be updated for the next
764 * This function can be called under rcu_read_lock(), given that the leaf
765 * pointers lifetimes are correctly managed.
767 void *idr_get_next(struct idr *idp, int *nextidp)
769 struct idr_layer *p, *pa[MAX_IDR_LEVEL + 1];
770 struct idr_layer **paa = &pa[0];
775 p = *paa = rcu_dereference_raw(idp->top);
778 n = (p->layer + 1) * IDR_BITS;
779 max = idr_max(p->layer + 1);
781 while (id >= 0 && id <= max) {
785 p = rcu_dereference_raw(p->ary[(id >> n) & IDR_MASK]);
795 * Proceed to the next layer at the current level. Unlike
796 * idr_for_each(), @id isn't guaranteed to be aligned to
797 * layer boundary at this point and adding 1 << n may
798 * incorrectly skip IDs. Make sure we jump to the
799 * beginning of the next layer using round_up().
801 id = round_up(id + 1, 1 << n);
802 while (n < fls(id)) {
809 EXPORT_SYMBOL(idr_get_next);
813 * idr_replace - replace pointer for given id
815 * @ptr: pointer you want associated with the id
818 * Replace the pointer registered with an id and return the old value.
819 * A %-ENOENT return indicates that @id was not found.
820 * A %-EINVAL return indicates that @id was not within valid constraints.
822 * The caller must serialize with writers.
824 void *idr_replace(struct idr *idp, void *ptr, int id)
827 struct idr_layer *p, *old_p;
830 return ERR_PTR(-EINVAL);
834 return ERR_PTR(-ENOENT);
836 if (id > idr_max(p->layer + 1))
837 return ERR_PTR(-ENOENT);
839 n = p->layer * IDR_BITS;
840 while ((n > 0) && p) {
841 p = p->ary[(id >> n) & IDR_MASK];
846 if (unlikely(p == NULL || !test_bit(n, p->bitmap)))
847 return ERR_PTR(-ENOENT);
850 rcu_assign_pointer(p->ary[n], ptr);
854 EXPORT_SYMBOL(idr_replace);
856 void __init idr_init_cache(void)
858 idr_layer_cache = kmem_cache_create("idr_layer_cache",
859 sizeof(struct idr_layer), 0, SLAB_PANIC, NULL);
863 * idr_init - initialize idr handle
866 * This function is use to set up the handle (@idp) that you will pass
867 * to the rest of the functions.
869 void idr_init(struct idr *idp)
871 memset(idp, 0, sizeof(struct idr));
872 spin_lock_init(&idp->lock);
874 EXPORT_SYMBOL(idr_init);
876 static int idr_has_entry(int id, void *p, void *data)
881 bool idr_is_empty(struct idr *idp)
883 return !idr_for_each(idp, idr_has_entry, NULL);
885 EXPORT_SYMBOL(idr_is_empty);
888 * DOC: IDA description
889 * IDA - IDR based ID allocator
891 * This is id allocator without id -> pointer translation. Memory
892 * usage is much lower than full blown idr because each id only
893 * occupies a bit. ida uses a custom leaf node which contains
894 * IDA_BITMAP_BITS slots.
896 * 2007-04-25 written by Tejun Heo <htejun@gmail.com>
899 static void free_bitmap(struct ida *ida, struct ida_bitmap *bitmap)
903 if (!ida->free_bitmap) {
904 spin_lock_irqsave(&ida->idr.lock, flags);
905 if (!ida->free_bitmap) {
906 ida->free_bitmap = bitmap;
909 spin_unlock_irqrestore(&ida->idr.lock, flags);
916 * ida_pre_get - reserve resources for ida allocation
918 * @gfp_mask: memory allocation flag
920 * This function should be called prior to locking and calling the
921 * following function. It preallocates enough memory to satisfy the
922 * worst possible allocation.
924 * If the system is REALLY out of memory this function returns %0,
927 int ida_pre_get(struct ida *ida, gfp_t gfp_mask)
929 /* allocate idr_layers */
930 if (!__idr_pre_get(&ida->idr, gfp_mask))
933 /* allocate free_bitmap */
934 if (!ida->free_bitmap) {
935 struct ida_bitmap *bitmap;
937 bitmap = kmalloc(sizeof(struct ida_bitmap), gfp_mask);
941 free_bitmap(ida, bitmap);
946 EXPORT_SYMBOL(ida_pre_get);
949 * ida_get_new_above - allocate new ID above or equal to a start id
951 * @starting_id: id to start search at
952 * @p_id: pointer to the allocated handle
954 * Allocate new ID above or equal to @starting_id. It should be called
955 * with any required locks.
957 * If memory is required, it will return %-EAGAIN, you should unlock
958 * and go back to the ida_pre_get() call. If the ida is full, it will
961 * @p_id returns a value in the range @starting_id ... %0x7fffffff.
963 int ida_get_new_above(struct ida *ida, int starting_id, int *p_id)
965 struct idr_layer *pa[MAX_IDR_LEVEL + 1];
966 struct ida_bitmap *bitmap;
968 int idr_id = starting_id / IDA_BITMAP_BITS;
969 int offset = starting_id % IDA_BITMAP_BITS;
973 /* get vacant slot */
974 t = idr_get_empty_slot(&ida->idr, idr_id, pa, 0, &ida->idr);
976 return t == -ENOMEM ? -EAGAIN : t;
978 if (t * IDA_BITMAP_BITS >= MAX_IDR_BIT)
985 /* if bitmap isn't there, create a new one */
986 bitmap = (void *)pa[0]->ary[idr_id & IDR_MASK];
988 spin_lock_irqsave(&ida->idr.lock, flags);
989 bitmap = ida->free_bitmap;
990 ida->free_bitmap = NULL;
991 spin_unlock_irqrestore(&ida->idr.lock, flags);
996 memset(bitmap, 0, sizeof(struct ida_bitmap));
997 rcu_assign_pointer(pa[0]->ary[idr_id & IDR_MASK],
1002 /* lookup for empty slot */
1003 t = find_next_zero_bit(bitmap->bitmap, IDA_BITMAP_BITS, offset);
1004 if (t == IDA_BITMAP_BITS) {
1005 /* no empty slot after offset, continue to the next chunk */
1011 id = idr_id * IDA_BITMAP_BITS + t;
1012 if (id >= MAX_IDR_BIT)
1015 __set_bit(t, bitmap->bitmap);
1016 if (++bitmap->nr_busy == IDA_BITMAP_BITS)
1017 idr_mark_full(pa, idr_id);
1021 /* Each leaf node can handle nearly a thousand slots and the
1022 * whole idea of ida is to have small memory foot print.
1023 * Throw away extra resources one by one after each successful
1026 if (ida->idr.id_free_cnt || ida->free_bitmap) {
1027 struct idr_layer *p = get_from_free_list(&ida->idr);
1029 kmem_cache_free(idr_layer_cache, p);
1034 EXPORT_SYMBOL(ida_get_new_above);
1037 * ida_remove - remove the given ID
1041 void ida_remove(struct ida *ida, int id)
1043 struct idr_layer *p = ida->idr.top;
1044 int shift = (ida->idr.layers - 1) * IDR_BITS;
1045 int idr_id = id / IDA_BITMAP_BITS;
1046 int offset = id % IDA_BITMAP_BITS;
1048 struct ida_bitmap *bitmap;
1050 if (idr_id > idr_max(ida->idr.layers))
1053 /* clear full bits while looking up the leaf idr_layer */
1054 while ((shift > 0) && p) {
1055 n = (idr_id >> shift) & IDR_MASK;
1056 __clear_bit(n, p->bitmap);
1064 n = idr_id & IDR_MASK;
1065 __clear_bit(n, p->bitmap);
1067 bitmap = (void *)p->ary[n];
1068 if (!bitmap || !test_bit(offset, bitmap->bitmap))
1071 /* update bitmap and remove it if empty */
1072 __clear_bit(offset, bitmap->bitmap);
1073 if (--bitmap->nr_busy == 0) {
1074 __set_bit(n, p->bitmap); /* to please idr_remove() */
1075 idr_remove(&ida->idr, idr_id);
1076 free_bitmap(ida, bitmap);
1082 WARN(1, "ida_remove called for id=%d which is not allocated.\n", id);
1084 EXPORT_SYMBOL(ida_remove);
1087 * ida_destroy - release all cached layers within an ida tree
1090 void ida_destroy(struct ida *ida)
1092 idr_destroy(&ida->idr);
1093 kfree(ida->free_bitmap);
1095 EXPORT_SYMBOL(ida_destroy);
1098 * ida_simple_get - get a new id.
1099 * @ida: the (initialized) ida.
1100 * @start: the minimum id (inclusive, < 0x8000000)
1101 * @end: the maximum id (exclusive, < 0x8000000 or 0)
1102 * @gfp_mask: memory allocation flags
1104 * Allocates an id in the range start <= id < end, or returns -ENOSPC.
1105 * On memory allocation failure, returns -ENOMEM.
1107 * Use ida_simple_remove() to get rid of an id.
1109 int ida_simple_get(struct ida *ida, unsigned int start, unsigned int end,
1114 unsigned long flags;
1116 BUG_ON((int)start < 0);
1117 BUG_ON((int)end < 0);
1122 BUG_ON(end < start);
1127 if (!ida_pre_get(ida, gfp_mask))
1130 spin_lock_irqsave(&simple_ida_lock, flags);
1131 ret = ida_get_new_above(ida, start, &id);
1134 ida_remove(ida, id);
1140 spin_unlock_irqrestore(&simple_ida_lock, flags);
1142 if (unlikely(ret == -EAGAIN))
1147 EXPORT_SYMBOL(ida_simple_get);
1150 * ida_simple_remove - remove an allocated id.
1151 * @ida: the (initialized) ida.
1152 * @id: the id returned by ida_simple_get.
1154 void ida_simple_remove(struct ida *ida, unsigned int id)
1156 unsigned long flags;
1158 BUG_ON((int)id < 0);
1159 spin_lock_irqsave(&simple_ida_lock, flags);
1160 ida_remove(ida, id);
1161 spin_unlock_irqrestore(&simple_ida_lock, flags);
1163 EXPORT_SYMBOL(ida_simple_remove);
1166 * ida_init - initialize ida handle
1169 * This function is use to set up the handle (@ida) that you will pass
1170 * to the rest of the functions.
1172 void ida_init(struct ida *ida)
1174 memset(ida, 0, sizeof(struct ida));
1175 idr_init(&ida->idr);
1178 EXPORT_SYMBOL(ida_init);