These changes are the raw update to linux-4.4.6-rt14. Kernel sources
[kvmfornfv.git] / kernel / lib / idr.c
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
23 #ifndef TEST                        // to test in user space...
24 #include <linux/slab.h>
25 #include <linux/init.h>
26 #include <linux/export.h>
27 #endif
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>
34
35 #define MAX_IDR_SHIFT           (sizeof(int) * 8 - 1)
36 #define MAX_IDR_BIT             (1U << MAX_IDR_SHIFT)
37
38 /* Leave the possibility of an incomplete final layer */
39 #define MAX_IDR_LEVEL ((MAX_IDR_SHIFT + IDR_BITS - 1) / IDR_BITS)
40
41 /* Number of id_layer structs to leave in free list */
42 #define MAX_IDR_FREE (MAX_IDR_LEVEL * 2)
43
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);
48
49 #ifdef CONFIG_PREEMPT_RT_FULL
50 static DEFINE_LOCAL_IRQ_LOCK(idr_lock);
51
52 static inline void idr_preload_lock(void)
53 {
54         local_lock(idr_lock);
55 }
56
57 static inline void idr_preload_unlock(void)
58 {
59         local_unlock(idr_lock);
60 }
61
62 void idr_preload_end(void)
63 {
64         idr_preload_unlock();
65 }
66 EXPORT_SYMBOL(idr_preload_end);
67 #else
68 static inline void idr_preload_lock(void)
69 {
70         preempt_disable();
71 }
72
73 static inline void idr_preload_unlock(void)
74 {
75         preempt_enable();
76 }
77 #endif
78
79
80 /* the maximum ID which can be allocated given idr->layers */
81 static int idr_max(int layers)
82 {
83         int bits = min_t(int, layers * IDR_BITS, MAX_IDR_SHIFT);
84
85         return (1 << bits) - 1;
86 }
87
88 /*
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
91  * so on.
92  */
93 static int idr_layer_prefix_mask(int layer)
94 {
95         return ~idr_max(layer + 1);
96 }
97
98 static struct idr_layer *get_from_free_list(struct idr *idp)
99 {
100         struct idr_layer *p;
101         unsigned long flags;
102
103         spin_lock_irqsave(&idp->lock, flags);
104         if ((p = idp->id_free)) {
105                 idp->id_free = p->ary[0];
106                 idp->id_free_cnt--;
107                 p->ary[0] = NULL;
108         }
109         spin_unlock_irqrestore(&idp->lock, flags);
110         return(p);
111 }
112
113 /**
114  * idr_layer_alloc - allocate a new idr_layer
115  * @gfp_mask: allocation mask
116  * @layer_idr: optional idr to allocate from
117  *
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.
121  *
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.
125  */
126 static struct idr_layer *idr_layer_alloc(gfp_t gfp_mask, struct idr *layer_idr)
127 {
128         struct idr_layer *new;
129
130         /* this is the old path, bypass to get_from_free_list() */
131         if (layer_idr)
132                 return get_from_free_list(layer_idr);
133
134         /*
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
139          * warning this time.
140          */
141         new = kmem_cache_zalloc(idr_layer_cache, gfp_mask | __GFP_NOWARN);
142         if (new)
143                 return new;
144
145         /*
146          * Try to fetch one from the per-cpu preload buffer if in process
147          * context.  See idr_preload() for details.
148          */
149         if (!in_interrupt()) {
150                 idr_preload_lock();
151                 new = __this_cpu_read(idr_preload_head);
152                 if (new) {
153                         __this_cpu_write(idr_preload_head, new->ary[0]);
154                         __this_cpu_dec(idr_preload_cnt);
155                         new->ary[0] = NULL;
156                 }
157                 idr_preload_unlock();
158                 if (new)
159                         return new;
160         }
161
162         /*
163          * Both failed.  Try kmem_cache again w/o adding __GFP_NOWARN so
164          * that memory allocation failure warning is printed as intended.
165          */
166         return kmem_cache_zalloc(idr_layer_cache, gfp_mask);
167 }
168
169 static void idr_layer_rcu_free(struct rcu_head *head)
170 {
171         struct idr_layer *layer;
172
173         layer = container_of(head, struct idr_layer, rcu_head);
174         kmem_cache_free(idr_layer_cache, layer);
175 }
176
177 static inline void free_layer(struct idr *idr, struct idr_layer *p)
178 {
179         if (idr->hint == p)
180                 RCU_INIT_POINTER(idr->hint, NULL);
181         call_rcu(&p->rcu_head, idr_layer_rcu_free);
182 }
183
184 /* only called when idp->lock is held */
185 static void __move_to_free_list(struct idr *idp, struct idr_layer *p)
186 {
187         p->ary[0] = idp->id_free;
188         idp->id_free = p;
189         idp->id_free_cnt++;
190 }
191
192 static void move_to_free_list(struct idr *idp, struct idr_layer *p)
193 {
194         unsigned long flags;
195
196         /*
197          * Depends on the return element being zeroed.
198          */
199         spin_lock_irqsave(&idp->lock, flags);
200         __move_to_free_list(idp, p);
201         spin_unlock_irqrestore(&idp->lock, flags);
202 }
203
204 static void idr_mark_full(struct idr_layer **pa, int id)
205 {
206         struct idr_layer *p = pa[0];
207         int l = 0;
208
209         __set_bit(id & IDR_MASK, p->bitmap);
210         /*
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
214          * tree.
215          */
216         while (bitmap_full(p->bitmap, IDR_SIZE)) {
217                 if (!(p = pa[++l]))
218                         break;
219                 id = id >> IDR_BITS;
220                 __set_bit((id & IDR_MASK), p->bitmap);
221         }
222 }
223
224 static int __idr_pre_get(struct idr *idp, gfp_t gfp_mask)
225 {
226         while (idp->id_free_cnt < MAX_IDR_FREE) {
227                 struct idr_layer *new;
228                 new = kmem_cache_zalloc(idr_layer_cache, gfp_mask);
229                 if (new == NULL)
230                         return (0);
231                 move_to_free_list(idp, new);
232         }
233         return 1;
234 }
235
236 /**
237  * sub_alloc - try to allocate an id without growing the tree depth
238  * @idp: idr handle
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()
243  *
244  * Allocate an id in range [@starting_id, INT_MAX] from @idp without
245  * growing its depth.  Returns
246  *
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.
251  */
252 static int sub_alloc(struct idr *idp, int *starting_id, struct idr_layer **pa,
253                      gfp_t gfp_mask, struct idr *layer_idr)
254 {
255         int n, m, sh;
256         struct idr_layer *p, *new;
257         int l, id, oid;
258
259         id = *starting_id;
260  restart:
261         p = idp->top;
262         l = idp->layers;
263         pa[l--] = NULL;
264         while (1) {
265                 /*
266                  * We run around this while until we reach the leaf node...
267                  */
268                 n = (id >> (IDR_BITS*l)) & IDR_MASK;
269                 m = find_next_zero_bit(p->bitmap, IDR_SIZE, n);
270                 if (m == IDR_SIZE) {
271                         /* no space available go back to previous layer. */
272                         l++;
273                         oid = id;
274                         id = (id | ((1 << (IDR_BITS * l)) - 1)) + 1;
275
276                         /* if already at the top layer, we need to grow */
277                         if (id > idr_max(idp->layers)) {
278                                 *starting_id = id;
279                                 return -EAGAIN;
280                         }
281                         p = pa[l];
282                         BUG_ON(!p);
283
284                         /* If we need to go up one layer, continue the
285                          * loop; otherwise, restart from the top.
286                          */
287                         sh = IDR_BITS * (l + 1);
288                         if (oid >> sh == id >> sh)
289                                 continue;
290                         else
291                                 goto restart;
292                 }
293                 if (m != n) {
294                         sh = IDR_BITS*l;
295                         id = ((id >> sh) ^ n ^ m) << sh;
296                 }
297                 if ((id >= MAX_IDR_BIT) || (id < 0))
298                         return -ENOSPC;
299                 if (l == 0)
300                         break;
301                 /*
302                  * Create the layer below if it is missing.
303                  */
304                 if (!p->ary[m]) {
305                         new = idr_layer_alloc(gfp_mask, layer_idr);
306                         if (!new)
307                                 return -ENOMEM;
308                         new->layer = l-1;
309                         new->prefix = id & idr_layer_prefix_mask(new->layer);
310                         rcu_assign_pointer(p->ary[m], new);
311                         p->count++;
312                 }
313                 pa[l--] = p;
314                 p = p->ary[m];
315         }
316
317         pa[l] = p;
318         return id;
319 }
320
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)
324 {
325         struct idr_layer *p, *new;
326         int layers, v, id;
327         unsigned long flags;
328
329         id = starting_id;
330 build_up:
331         p = idp->top;
332         layers = idp->layers;
333         if (unlikely(!p)) {
334                 if (!(p = idr_layer_alloc(gfp_mask, layer_idr)))
335                         return -ENOMEM;
336                 p->layer = 0;
337                 layers = 1;
338         }
339         /*
340          * Add a new layer to the top of the tree if the requested
341          * id is larger than the currently allocated space.
342          */
343         while (id > idr_max(layers)) {
344                 layers++;
345                 if (!p->count) {
346                         /* special case: if the tree is currently empty,
347                          * then we grow the tree by moving the top node
348                          * upwards.
349                          */
350                         p->layer++;
351                         WARN_ON_ONCE(p->prefix);
352                         continue;
353                 }
354                 if (!(new = idr_layer_alloc(gfp_mask, layer_idr))) {
355                         /*
356                          * The allocation failed.  If we built part of
357                          * the structure tear it down.
358                          */
359                         spin_lock_irqsave(&idp->lock, flags);
360                         for (new = p; p && p != idp->top; new = p) {
361                                 p = p->ary[0];
362                                 new->ary[0] = NULL;
363                                 new->count = 0;
364                                 bitmap_clear(new->bitmap, 0, IDR_SIZE);
365                                 __move_to_free_list(idp, new);
366                         }
367                         spin_unlock_irqrestore(&idp->lock, flags);
368                         return -ENOMEM;
369                 }
370                 new->ary[0] = p;
371                 new->count = 1;
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);
376                 p = new;
377         }
378         rcu_assign_pointer(idp->top, p);
379         idp->layers = layers;
380         v = sub_alloc(idp, &id, pa, gfp_mask, layer_idr);
381         if (v == -EAGAIN)
382                 goto build_up;
383         return(v);
384 }
385
386 /*
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.
389  */
390 static void idr_fill_slot(struct idr *idr, void *ptr, int id,
391                           struct idr_layer **pa)
392 {
393         /* update hint used for lookup, cleared from free_layer() */
394         rcu_assign_pointer(idr->hint, pa[0]);
395
396         rcu_assign_pointer(pa[0]->ary[id & IDR_MASK], (struct idr_layer *)ptr);
397         pa[0]->count++;
398         idr_mark_full(pa, id);
399 }
400
401 /**
402  * idr_preload - preload for idr_alloc()
403  * @gfp_mask: allocation mask to use for preloading
404  *
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.
408  *
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.
412  *
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.
415  *
416  *      idr_preload(GFP_KERNEL);
417  *      spin_lock(lock);
418  *
419  *      id = idr_alloc(idr, ptr, start, end, GFP_NOWAIT);
420  *
421  *      spin_unlock(lock);
422  *      idr_preload_end();
423  *      if (id < 0)
424  *              error;
425  */
426 void idr_preload(gfp_t gfp_mask)
427 {
428         /*
429          * Consuming preload buffer from non-process context breaks preload
430          * allocation guarantee.  Disallow usage from those contexts.
431          */
432         WARN_ON_ONCE(in_interrupt());
433         might_sleep_if(gfpflags_allow_blocking(gfp_mask));
434
435         idr_preload_lock();
436
437         /*
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.
443          */
444         while (__this_cpu_read(idr_preload_cnt) < MAX_IDR_FREE) {
445                 struct idr_layer *new;
446
447                 idr_preload_unlock();
448                 new = kmem_cache_zalloc(idr_layer_cache, gfp_mask);
449                 idr_preload_lock();
450                 if (!new)
451                         break;
452
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);
457         }
458 }
459 EXPORT_SYMBOL(idr_preload);
460
461 /**
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
468  *
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.
472  *
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.
475  *
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.
480  */
481 int idr_alloc(struct idr *idr, void *ptr, int start, int end, gfp_t gfp_mask)
482 {
483         int max = end > 0 ? end - 1 : INT_MAX;  /* inclusive upper limit */
484         struct idr_layer *pa[MAX_IDR_LEVEL + 1];
485         int id;
486
487         might_sleep_if(gfpflags_allow_blocking(gfp_mask));
488
489         /* sanity checks */
490         if (WARN_ON_ONCE(start < 0))
491                 return -EINVAL;
492         if (unlikely(max < start))
493                 return -ENOSPC;
494
495         /* allocate id */
496         id = idr_get_empty_slot(idr, start, pa, gfp_mask, NULL);
497         if (unlikely(id < 0))
498                 return id;
499         if (unlikely(id > max))
500                 return -ENOSPC;
501
502         idr_fill_slot(idr, ptr, id, pa);
503         return id;
504 }
505 EXPORT_SYMBOL_GPL(idr_alloc);
506
507 /**
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
514  *
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.
518  */
519 int idr_alloc_cyclic(struct idr *idr, void *ptr, int start, int end,
520                         gfp_t gfp_mask)
521 {
522         int id;
523
524         id = idr_alloc(idr, ptr, max(start, idr->cur), end, gfp_mask);
525         if (id == -ENOSPC)
526                 id = idr_alloc(idr, ptr, start, end, gfp_mask);
527
528         if (likely(id >= 0))
529                 idr->cur = id + 1;
530         return id;
531 }
532 EXPORT_SYMBOL(idr_alloc_cyclic);
533
534 static void idr_remove_warning(int id)
535 {
536         WARN(1, "idr_remove called for id=%d which is not allocated.\n", id);
537 }
538
539 static void sub_remove(struct idr *idp, int shift, int id)
540 {
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;
545         int n;
546
547         *paa = NULL;
548         *++paa = &idp->top;
549
550         while ((shift > 0) && p) {
551                 n = (id >> shift) & IDR_MASK;
552                 __clear_bit(n, p->bitmap);
553                 *++paa = &p->ary[n];
554                 p = p->ary[n];
555                 shift -= IDR_BITS;
556         }
557         n = id & IDR_MASK;
558         if (likely(p != NULL && test_bit(n, p->bitmap))) {
559                 __clear_bit(n, p->bitmap);
560                 RCU_INIT_POINTER(p->ary[n], NULL);
561                 to_free = NULL;
562                 while(*paa && ! --((**paa)->count)){
563                         if (to_free)
564                                 free_layer(idp, to_free);
565                         to_free = **paa;
566                         **paa-- = NULL;
567                 }
568                 if (!*paa)
569                         idp->layers = 0;
570                 if (to_free)
571                         free_layer(idp, to_free);
572         } else
573                 idr_remove_warning(id);
574 }
575
576 /**
577  * idr_remove - remove the given id and free its slot
578  * @idp: idr handle
579  * @id: unique key
580  */
581 void idr_remove(struct idr *idp, int id)
582 {
583         struct idr_layer *p;
584         struct idr_layer *to_free;
585
586         if (id < 0)
587                 return;
588
589         if (id > idr_max(idp->layers)) {
590                 idr_remove_warning(id);
591                 return;
592         }
593
594         sub_remove(idp, (idp->layers - 1) * IDR_BITS, id);
595         if (idp->top && idp->top->count == 1 && (idp->layers > 1) &&
596             idp->top->ary[0]) {
597                 /*
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
601                  * tree.
602                  */
603                 to_free = idp->top;
604                 p = idp->top->ary[0];
605                 rcu_assign_pointer(idp->top, p);
606                 --idp->layers;
607                 to_free->count = 0;
608                 bitmap_clear(to_free->bitmap, 0, IDR_SIZE);
609                 free_layer(idp, to_free);
610         }
611 }
612 EXPORT_SYMBOL(idr_remove);
613
614 static void __idr_remove_all(struct idr *idp)
615 {
616         int n, id, max;
617         int bt_mask;
618         struct idr_layer *p;
619         struct idr_layer *pa[MAX_IDR_LEVEL + 1];
620         struct idr_layer **paa = &pa[0];
621
622         n = idp->layers * IDR_BITS;
623         *paa = idp->top;
624         RCU_INIT_POINTER(idp->top, NULL);
625         max = idr_max(idp->layers);
626
627         id = 0;
628         while (id >= 0 && id <= max) {
629                 p = *paa;
630                 while (n > IDR_BITS && p) {
631                         n -= IDR_BITS;
632                         p = p->ary[(id >> n) & IDR_MASK];
633                         *++paa = p;
634                 }
635
636                 bt_mask = id;
637                 id += 1 << n;
638                 /* Get the highest bit that the above add changed from 0->1. */
639                 while (n < fls(id ^ bt_mask)) {
640                         if (*paa)
641                                 free_layer(idp, *paa);
642                         n += IDR_BITS;
643                         --paa;
644                 }
645         }
646         idp->layers = 0;
647 }
648
649 /**
650  * idr_destroy - release all cached layers within an idr tree
651  * @idp: idr handle
652  *
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
656  * idr_destroy().
657  *
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.
661  */
662 void idr_destroy(struct idr *idp)
663 {
664         __idr_remove_all(idp);
665
666         while (idp->id_free_cnt) {
667                 struct idr_layer *p = get_from_free_list(idp);
668                 kmem_cache_free(idr_layer_cache, p);
669         }
670 }
671 EXPORT_SYMBOL(idr_destroy);
672
673 void *idr_find_slowpath(struct idr *idp, int id)
674 {
675         int n;
676         struct idr_layer *p;
677
678         if (id < 0)
679                 return NULL;
680
681         p = rcu_dereference_raw(idp->top);
682         if (!p)
683                 return NULL;
684         n = (p->layer+1) * IDR_BITS;
685
686         if (id > idr_max(p->layer + 1))
687                 return NULL;
688         BUG_ON(n == 0);
689
690         while (n > 0 && p) {
691                 n -= IDR_BITS;
692                 BUG_ON(n != p->layer*IDR_BITS);
693                 p = rcu_dereference_raw(p->ary[(id >> n) & IDR_MASK]);
694         }
695         return((void *)p);
696 }
697 EXPORT_SYMBOL(idr_find_slowpath);
698
699 /**
700  * idr_for_each - iterate through all stored pointers
701  * @idp: idr handle
702  * @fn: function to be called for each pointer
703  * @data: data passed back to callback function
704  *
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
710  * not allowed.
711  *
712  * We check the return of @fn each time. If it returns anything other
713  * than %0, we break out and return that value.
714  *
715  * The caller must serialize idr_for_each() vs idr_get_new() and idr_remove().
716  */
717 int idr_for_each(struct idr *idp,
718                  int (*fn)(int id, void *p, void *data), void *data)
719 {
720         int n, id, max, error = 0;
721         struct idr_layer *p;
722         struct idr_layer *pa[MAX_IDR_LEVEL + 1];
723         struct idr_layer **paa = &pa[0];
724
725         n = idp->layers * IDR_BITS;
726         *paa = rcu_dereference_raw(idp->top);
727         max = idr_max(idp->layers);
728
729         id = 0;
730         while (id >= 0 && id <= max) {
731                 p = *paa;
732                 while (n > 0 && p) {
733                         n -= IDR_BITS;
734                         p = rcu_dereference_raw(p->ary[(id >> n) & IDR_MASK]);
735                         *++paa = p;
736                 }
737
738                 if (p) {
739                         error = fn(id, (void *)p, data);
740                         if (error)
741                                 break;
742                 }
743
744                 id += 1 << n;
745                 while (n < fls(id)) {
746                         n += IDR_BITS;
747                         --paa;
748                 }
749         }
750
751         return error;
752 }
753 EXPORT_SYMBOL(idr_for_each);
754
755 /**
756  * idr_get_next - lookup next object of id to given id.
757  * @idp: idr handle
758  * @nextidp:  pointer to lookup key
759  *
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
762  * iteration.
763  *
764  * This function can be called under rcu_read_lock(), given that the leaf
765  * pointers lifetimes are correctly managed.
766  */
767 void *idr_get_next(struct idr *idp, int *nextidp)
768 {
769         struct idr_layer *p, *pa[MAX_IDR_LEVEL + 1];
770         struct idr_layer **paa = &pa[0];
771         int id = *nextidp;
772         int n, max;
773
774         /* find first ent */
775         p = *paa = rcu_dereference_raw(idp->top);
776         if (!p)
777                 return NULL;
778         n = (p->layer + 1) * IDR_BITS;
779         max = idr_max(p->layer + 1);
780
781         while (id >= 0 && id <= max) {
782                 p = *paa;
783                 while (n > 0 && p) {
784                         n -= IDR_BITS;
785                         p = rcu_dereference_raw(p->ary[(id >> n) & IDR_MASK]);
786                         *++paa = p;
787                 }
788
789                 if (p) {
790                         *nextidp = id;
791                         return p;
792                 }
793
794                 /*
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().
800                  */
801                 id = round_up(id + 1, 1 << n);
802                 while (n < fls(id)) {
803                         n += IDR_BITS;
804                         --paa;
805                 }
806         }
807         return NULL;
808 }
809 EXPORT_SYMBOL(idr_get_next);
810
811
812 /**
813  * idr_replace - replace pointer for given id
814  * @idp: idr handle
815  * @ptr: pointer you want associated with the id
816  * @id: lookup key
817  *
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.
821  *
822  * The caller must serialize with writers.
823  */
824 void *idr_replace(struct idr *idp, void *ptr, int id)
825 {
826         int n;
827         struct idr_layer *p, *old_p;
828
829         if (id < 0)
830                 return ERR_PTR(-EINVAL);
831
832         p = idp->top;
833         if (!p)
834                 return ERR_PTR(-ENOENT);
835
836         if (id > idr_max(p->layer + 1))
837                 return ERR_PTR(-ENOENT);
838
839         n = p->layer * IDR_BITS;
840         while ((n > 0) && p) {
841                 p = p->ary[(id >> n) & IDR_MASK];
842                 n -= IDR_BITS;
843         }
844
845         n = id & IDR_MASK;
846         if (unlikely(p == NULL || !test_bit(n, p->bitmap)))
847                 return ERR_PTR(-ENOENT);
848
849         old_p = p->ary[n];
850         rcu_assign_pointer(p->ary[n], ptr);
851
852         return old_p;
853 }
854 EXPORT_SYMBOL(idr_replace);
855
856 void __init idr_init_cache(void)
857 {
858         idr_layer_cache = kmem_cache_create("idr_layer_cache",
859                                 sizeof(struct idr_layer), 0, SLAB_PANIC, NULL);
860 }
861
862 /**
863  * idr_init - initialize idr handle
864  * @idp:        idr handle
865  *
866  * This function is use to set up the handle (@idp) that you will pass
867  * to the rest of the functions.
868  */
869 void idr_init(struct idr *idp)
870 {
871         memset(idp, 0, sizeof(struct idr));
872         spin_lock_init(&idp->lock);
873 }
874 EXPORT_SYMBOL(idr_init);
875
876 static int idr_has_entry(int id, void *p, void *data)
877 {
878         return 1;
879 }
880
881 bool idr_is_empty(struct idr *idp)
882 {
883         return !idr_for_each(idp, idr_has_entry, NULL);
884 }
885 EXPORT_SYMBOL(idr_is_empty);
886
887 /**
888  * DOC: IDA description
889  * IDA - IDR based ID allocator
890  *
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.
895  *
896  * 2007-04-25  written by Tejun Heo <htejun@gmail.com>
897  */
898
899 static void free_bitmap(struct ida *ida, struct ida_bitmap *bitmap)
900 {
901         unsigned long flags;
902
903         if (!ida->free_bitmap) {
904                 spin_lock_irqsave(&ida->idr.lock, flags);
905                 if (!ida->free_bitmap) {
906                         ida->free_bitmap = bitmap;
907                         bitmap = NULL;
908                 }
909                 spin_unlock_irqrestore(&ida->idr.lock, flags);
910         }
911
912         kfree(bitmap);
913 }
914
915 /**
916  * ida_pre_get - reserve resources for ida allocation
917  * @ida:        ida handle
918  * @gfp_mask:   memory allocation flag
919  *
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.
923  *
924  * If the system is REALLY out of memory this function returns %0,
925  * otherwise %1.
926  */
927 int ida_pre_get(struct ida *ida, gfp_t gfp_mask)
928 {
929         /* allocate idr_layers */
930         if (!__idr_pre_get(&ida->idr, gfp_mask))
931                 return 0;
932
933         /* allocate free_bitmap */
934         if (!ida->free_bitmap) {
935                 struct ida_bitmap *bitmap;
936
937                 bitmap = kmalloc(sizeof(struct ida_bitmap), gfp_mask);
938                 if (!bitmap)
939                         return 0;
940
941                 free_bitmap(ida, bitmap);
942         }
943
944         return 1;
945 }
946 EXPORT_SYMBOL(ida_pre_get);
947
948 /**
949  * ida_get_new_above - allocate new ID above or equal to a start id
950  * @ida:        ida handle
951  * @starting_id: id to start search at
952  * @p_id:       pointer to the allocated handle
953  *
954  * Allocate new ID above or equal to @starting_id.  It should be called
955  * with any required locks.
956  *
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
959  * return %-ENOSPC.
960  *
961  * @p_id returns a value in the range @starting_id ... %0x7fffffff.
962  */
963 int ida_get_new_above(struct ida *ida, int starting_id, int *p_id)
964 {
965         struct idr_layer *pa[MAX_IDR_LEVEL + 1];
966         struct ida_bitmap *bitmap;
967         unsigned long flags;
968         int idr_id = starting_id / IDA_BITMAP_BITS;
969         int offset = starting_id % IDA_BITMAP_BITS;
970         int t, id;
971
972  restart:
973         /* get vacant slot */
974         t = idr_get_empty_slot(&ida->idr, idr_id, pa, 0, &ida->idr);
975         if (t < 0)
976                 return t == -ENOMEM ? -EAGAIN : t;
977
978         if (t * IDA_BITMAP_BITS >= MAX_IDR_BIT)
979                 return -ENOSPC;
980
981         if (t != idr_id)
982                 offset = 0;
983         idr_id = t;
984
985         /* if bitmap isn't there, create a new one */
986         bitmap = (void *)pa[0]->ary[idr_id & IDR_MASK];
987         if (!bitmap) {
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);
992
993                 if (!bitmap)
994                         return -EAGAIN;
995
996                 memset(bitmap, 0, sizeof(struct ida_bitmap));
997                 rcu_assign_pointer(pa[0]->ary[idr_id & IDR_MASK],
998                                 (void *)bitmap);
999                 pa[0]->count++;
1000         }
1001
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 */
1006                 idr_id++;
1007                 offset = 0;
1008                 goto restart;
1009         }
1010
1011         id = idr_id * IDA_BITMAP_BITS + t;
1012         if (id >= MAX_IDR_BIT)
1013                 return -ENOSPC;
1014
1015         __set_bit(t, bitmap->bitmap);
1016         if (++bitmap->nr_busy == IDA_BITMAP_BITS)
1017                 idr_mark_full(pa, idr_id);
1018
1019         *p_id = id;
1020
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
1024          * allocation.
1025          */
1026         if (ida->idr.id_free_cnt || ida->free_bitmap) {
1027                 struct idr_layer *p = get_from_free_list(&ida->idr);
1028                 if (p)
1029                         kmem_cache_free(idr_layer_cache, p);
1030         }
1031
1032         return 0;
1033 }
1034 EXPORT_SYMBOL(ida_get_new_above);
1035
1036 /**
1037  * ida_remove - remove the given ID
1038  * @ida:        ida handle
1039  * @id:         ID to free
1040  */
1041 void ida_remove(struct ida *ida, int id)
1042 {
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;
1047         int n;
1048         struct ida_bitmap *bitmap;
1049
1050         if (idr_id > idr_max(ida->idr.layers))
1051                 goto err;
1052
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);
1057                 p = p->ary[n];
1058                 shift -= IDR_BITS;
1059         }
1060
1061         if (p == NULL)
1062                 goto err;
1063
1064         n = idr_id & IDR_MASK;
1065         __clear_bit(n, p->bitmap);
1066
1067         bitmap = (void *)p->ary[n];
1068         if (!bitmap || !test_bit(offset, bitmap->bitmap))
1069                 goto err;
1070
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);
1077         }
1078
1079         return;
1080
1081  err:
1082         WARN(1, "ida_remove called for id=%d which is not allocated.\n", id);
1083 }
1084 EXPORT_SYMBOL(ida_remove);
1085
1086 /**
1087  * ida_destroy - release all cached layers within an ida tree
1088  * @ida:                ida handle
1089  */
1090 void ida_destroy(struct ida *ida)
1091 {
1092         idr_destroy(&ida->idr);
1093         kfree(ida->free_bitmap);
1094 }
1095 EXPORT_SYMBOL(ida_destroy);
1096
1097 /**
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
1103  *
1104  * Allocates an id in the range start <= id < end, or returns -ENOSPC.
1105  * On memory allocation failure, returns -ENOMEM.
1106  *
1107  * Use ida_simple_remove() to get rid of an id.
1108  */
1109 int ida_simple_get(struct ida *ida, unsigned int start, unsigned int end,
1110                    gfp_t gfp_mask)
1111 {
1112         int ret, id;
1113         unsigned int max;
1114         unsigned long flags;
1115
1116         BUG_ON((int)start < 0);
1117         BUG_ON((int)end < 0);
1118
1119         if (end == 0)
1120                 max = 0x80000000;
1121         else {
1122                 BUG_ON(end < start);
1123                 max = end - 1;
1124         }
1125
1126 again:
1127         if (!ida_pre_get(ida, gfp_mask))
1128                 return -ENOMEM;
1129
1130         spin_lock_irqsave(&simple_ida_lock, flags);
1131         ret = ida_get_new_above(ida, start, &id);
1132         if (!ret) {
1133                 if (id > max) {
1134                         ida_remove(ida, id);
1135                         ret = -ENOSPC;
1136                 } else {
1137                         ret = id;
1138                 }
1139         }
1140         spin_unlock_irqrestore(&simple_ida_lock, flags);
1141
1142         if (unlikely(ret == -EAGAIN))
1143                 goto again;
1144
1145         return ret;
1146 }
1147 EXPORT_SYMBOL(ida_simple_get);
1148
1149 /**
1150  * ida_simple_remove - remove an allocated id.
1151  * @ida: the (initialized) ida.
1152  * @id: the id returned by ida_simple_get.
1153  */
1154 void ida_simple_remove(struct ida *ida, unsigned int id)
1155 {
1156         unsigned long flags;
1157
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);
1162 }
1163 EXPORT_SYMBOL(ida_simple_remove);
1164
1165 /**
1166  * ida_init - initialize ida handle
1167  * @ida:        ida handle
1168  *
1169  * This function is use to set up the handle (@ida) that you will pass
1170  * to the rest of the functions.
1171  */
1172 void ida_init(struct ida *ida)
1173 {
1174         memset(ida, 0, sizeof(struct ida));
1175         idr_init(&ida->idr);
1176
1177 }
1178 EXPORT_SYMBOL(ida_init);