2 * Copyright (c) 2010 Isilon Systems, Inc.
3 * Copyright (c) 2010 iX Systems, Inc.
4 * Copyright (c) 2010 Panasas, Inc.
5 * Copyright (c) 2013-2017 Mellanox Technologies, Ltd.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice unmodified, this list of conditions, and the following
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/malloc.h>
36 #include <sys/kernel.h>
37 #include <sys/sysctl.h>
39 #include <sys/mutex.h>
41 #include <machine/stdarg.h>
43 #include <linux/bitmap.h>
44 #include <linux/kobject.h>
45 #include <linux/slab.h>
46 #include <linux/idr.h>
47 #include <linux/err.h>
49 #define MAX_IDR_LEVEL ((MAX_IDR_SHIFT + IDR_BITS - 1) / IDR_BITS)
50 #define MAX_IDR_FREE (MAX_IDR_LEVEL * 2)
52 struct linux_idr_cache {
54 struct idr_layer *head;
58 DPCPU_DEFINE_STATIC(struct linux_idr_cache, linux_idr_cache);
63 * This is quick and dirty and not as re-entrant as the linux version
64 * however it should be fairly fast. It is basically a radix tree with
65 * a builtin bitmap for allocation.
67 static MALLOC_DEFINE(M_IDR, "idr", "Linux IDR compat");
69 static struct idr_layer *
70 idr_preload_dequeue_locked(struct linux_idr_cache *lic)
72 struct idr_layer *retval;
74 /* check if wrong thread is trying to dequeue */
75 if (mtx_owned(&lic->lock.m) == 0)
79 if (likely(retval != NULL)) {
80 lic->head = retval->ary[0];
82 retval->ary[0] = NULL;
88 idr_preload_init(void *arg)
93 struct linux_idr_cache *lic =
94 DPCPU_ID_PTR(cpu, linux_idr_cache);
96 spin_lock_init(&lic->lock);
99 SYSINIT(idr_preload_init, SI_SUB_CPU, SI_ORDER_ANY, idr_preload_init, NULL);
102 idr_preload_uninit(void *arg)
107 struct idr_layer *cacheval;
108 struct linux_idr_cache *lic =
109 DPCPU_ID_PTR(cpu, linux_idr_cache);
112 spin_lock(&lic->lock);
113 cacheval = idr_preload_dequeue_locked(lic);
114 spin_unlock(&lic->lock);
116 if (cacheval == NULL)
118 free(cacheval, M_IDR);
120 spin_lock_destroy(&lic->lock);
123 SYSUNINIT(idr_preload_uninit, SI_SUB_LOCK, SI_ORDER_FIRST, idr_preload_uninit, NULL);
126 idr_preload(gfp_t gfp_mask)
128 struct linux_idr_cache *lic;
129 struct idr_layer *cacheval;
133 lic = &DPCPU_GET(linux_idr_cache);
136 spin_lock(&lic->lock);
137 while (lic->count < MAX_IDR_FREE) {
138 spin_unlock(&lic->lock);
139 cacheval = malloc(sizeof(*cacheval), M_IDR, M_ZERO | gfp_mask);
140 spin_lock(&lic->lock);
141 if (cacheval == NULL)
143 cacheval->ary[0] = lic->head;
144 lic->head = cacheval;
150 idr_preload_end(void)
152 struct linux_idr_cache *lic;
154 lic = &DPCPU_GET(linux_idr_cache);
155 spin_unlock(&lic->lock);
160 idr_max(struct idr *idr)
162 return (1 << (idr->layers * IDR_BITS)) - 1;
166 idr_pos(int id, int layer)
168 return (id >> (IDR_BITS * layer)) & IDR_MASK;
172 idr_init(struct idr *idr)
174 bzero(idr, sizeof(*idr));
175 mtx_init(&idr->lock, "idr", NULL, MTX_DEF);
178 /* Only frees cached pages. */
180 idr_destroy(struct idr *idr)
182 struct idr_layer *il, *iln;
185 mtx_lock(&idr->lock);
186 for (il = idr->free; il != NULL; il = iln) {
190 mtx_unlock(&idr->lock);
191 mtx_destroy(&idr->lock);
195 idr_remove_layer(struct idr_layer *il, int layer)
205 for (i = 0; i < IDR_SIZE; i++)
207 idr_remove_layer(il->ary[i], layer - 1);
211 idr_remove_all(struct idr *idr)
214 mtx_lock(&idr->lock);
215 idr_remove_layer(idr->top, idr->layers - 1);
218 mtx_unlock(&idr->lock);
222 idr_remove_locked(struct idr *idr, int id)
224 struct idr_layer *il;
231 layer = idr->layers - 1;
232 if (il == NULL || id > idr_max(idr))
235 * Walk down the tree to this item setting bitmaps along the way
236 * as we know at least one item will be free along this path.
238 while (layer && il) {
239 idx = idr_pos(id, layer);
240 il->bitmap |= 1 << idx;
246 * At this point we've set free space bitmaps up the whole tree.
247 * We could make this non-fatal and unwind but linux dumps a stack
248 * and a warning so I don't think it's necessary.
250 if (il == NULL || (il->bitmap & (1 << idx)) != 0)
251 panic("idr_remove: Item %d not allocated (%p, %p)\n",
255 il->bitmap |= 1 << idx;
261 idr_remove(struct idr *idr, int id)
265 mtx_lock(&idr->lock);
266 res = idr_remove_locked(idr, id);
267 mtx_unlock(&idr->lock);
272 static inline struct idr_layer *
273 idr_find_layer_locked(struct idr *idr, int id)
275 struct idr_layer *il;
280 layer = idr->layers - 1;
281 if (il == NULL || id > idr_max(idr))
283 while (layer && il) {
284 il = il->ary[idr_pos(id, layer)];
291 idr_replace(struct idr *idr, void *ptr, int id)
293 struct idr_layer *il;
297 mtx_lock(&idr->lock);
298 il = idr_find_layer_locked(idr, id);
301 /* Replace still returns an error if the item was not allocated. */
302 if (il == NULL || (il->bitmap & (1 << idx))) {
303 res = ERR_PTR(-ENOENT);
308 mtx_unlock(&idr->lock);
313 idr_find_locked(struct idr *idr, int id)
315 struct idr_layer *il;
318 mtx_assert(&idr->lock, MA_OWNED);
319 il = idr_find_layer_locked(idr, id);
321 res = il->ary[id & IDR_MASK];
328 idr_find(struct idr *idr, int id)
332 mtx_lock(&idr->lock);
333 res = idr_find_locked(idr, id);
334 mtx_unlock(&idr->lock);
339 idr_get_next(struct idr *idr, int *nextidp)
344 mtx_lock(&idr->lock);
345 for (; id <= idr_max(idr); id++) {
346 res = idr_find_locked(idr, id);
352 mtx_unlock(&idr->lock);
357 idr_pre_get(struct idr *idr, gfp_t gfp_mask)
359 struct idr_layer *il, *iln;
360 struct idr_layer *head;
363 mtx_lock(&idr->lock);
365 need = idr->layers + 1;
366 for (il = idr->free; il != NULL; il = il->ary[0])
368 mtx_unlock(&idr->lock);
371 for (head = NULL; need; need--) {
372 iln = malloc(sizeof(*il), M_IDR, M_ZERO | gfp_mask);
375 bitmap_fill(&iln->bitmap, IDR_SIZE);
384 mtx_lock(&idr->lock);
385 il->ary[0] = idr->free;
391 static struct idr_layer *
392 idr_free_list_get(struct idr *idp)
394 struct idr_layer *il;
396 if ((il = idp->free) != NULL) {
397 idp->free = il->ary[0];
403 static inline struct idr_layer *
404 idr_get(struct idr *idp)
406 struct idr_layer *il;
408 if ((il = idr_free_list_get(idp)) != NULL) {
409 MPASS(il->bitmap != 0);
410 } else if ((il = malloc(sizeof(*il), M_IDR, M_ZERO | M_NOWAIT)) != NULL) {
411 bitmap_fill(&il->bitmap, IDR_SIZE);
412 } else if ((il = idr_preload_dequeue_locked(&DPCPU_GET(linux_idr_cache))) != NULL) {
413 bitmap_fill(&il->bitmap, IDR_SIZE);
421 * Could be implemented as get_new_above(idr, ptr, 0, idp) but written
422 * first for simplicity sake.
425 idr_get_new_locked(struct idr *idr, void *ptr, int *idp)
427 struct idr_layer *stack[MAX_LEVEL];
428 struct idr_layer *il;
434 mtx_assert(&idr->lock, MA_OWNED);
438 * Expand the tree until there is free space.
440 if (idr->top == NULL || idr->top->bitmap == 0) {
441 if (idr->layers == MAX_LEVEL + 1) {
448 il->ary[0] = idr->top;
457 * Walk the tree following free bitmaps, record our path.
459 for (layer = idr->layers - 1;; layer--) {
461 idx = ffsl(il->bitmap);
463 panic("idr_get_new: Invalid leaf state (%p, %p)\n",
466 id |= idx << (layer * IDR_BITS);
469 if (il->ary[idx] == NULL) {
470 il->ary[idx] = idr_get(idr);
471 if (il->ary[idx] == NULL)
477 * Allocate the leaf to the consumer.
479 il->bitmap &= ~(1 << idx);
483 * Clear bitmaps potentially up to the root.
485 while (il->bitmap == 0 && ++layer < idr->layers) {
487 il->bitmap &= ~(1 << idr_pos(id, layer));
492 if (error == 0 && idr_find_locked(idr, id) != ptr) {
493 panic("idr_get_new: Failed for idr %p, id %d, ptr %p\n",
501 idr_get_new(struct idr *idr, void *ptr, int *idp)
505 mtx_lock(&idr->lock);
506 retval = idr_get_new_locked(idr, ptr, idp);
507 mtx_unlock(&idr->lock);
512 idr_get_new_above_locked(struct idr *idr, void *ptr, int starting_id, int *idp)
514 struct idr_layer *stack[MAX_LEVEL];
515 struct idr_layer *il;
521 mtx_assert(&idr->lock, MA_OWNED);
525 * Compute the layers required to support starting_id and the mask
531 while (idx & ~IDR_MASK) {
535 if (layer == MAX_LEVEL + 1) {
540 * Expand the tree until there is free space at or beyond starting_id.
542 while (idr->layers <= layer ||
543 idr->top->bitmap < (1 << idr_pos(starting_id, idr->layers - 1))) {
544 if (idr->layers == MAX_LEVEL + 1) {
551 il->ary[0] = idr->top;
552 if (idr->top && idr->top->bitmap == 0)
560 * Walk the tree following free bitmaps, record our path.
562 for (layer = idr->layers - 1;; layer--) {
564 sidx = idr_pos(starting_id, layer);
565 /* Returns index numbered from 0 or size if none exists. */
566 idx = find_next_bit(&il->bitmap, IDR_SIZE, sidx);
567 if (idx == IDR_SIZE && sidx == 0)
568 panic("idr_get_new: Invalid leaf state (%p, %p)\n",
571 * We may have walked a path where there was a free bit but
572 * it was lower than what we wanted. Restart the search with
573 * a larger starting id. id contains the progress we made so
574 * far. Search the leaf one above this level. This may
575 * restart as many as MAX_LEVEL times but that is expected
578 if (idx == IDR_SIZE) {
579 starting_id = id + (1 << ((layer + 1) * IDR_BITS));
583 starting_id = 0; /* Search the whole subtree. */
584 id |= idx << (layer * IDR_BITS);
587 if (il->ary[idx] == NULL) {
588 il->ary[idx] = idr_get(idr);
589 if (il->ary[idx] == NULL)
595 * Allocate the leaf to the consumer.
597 il->bitmap &= ~(1 << idx);
601 * Clear bitmaps potentially up to the root.
603 while (il->bitmap == 0 && ++layer < idr->layers) {
605 il->bitmap &= ~(1 << idr_pos(id, layer));
610 if (error == 0 && idr_find_locked(idr, id) != ptr) {
611 panic("idr_get_new_above: Failed for idr %p, id %d, ptr %p\n",
619 idr_get_new_above(struct idr *idr, void *ptr, int starting_id, int *idp)
623 mtx_lock(&idr->lock);
624 retval = idr_get_new_above_locked(idr, ptr, starting_id, idp);
625 mtx_unlock(&idr->lock);
630 ida_get_new_above(struct ida *ida, int starting_id, int *p_id)
632 return (idr_get_new_above(&ida->idr, NULL, starting_id, p_id));
636 idr_alloc_locked(struct idr *idr, void *ptr, int start, int end)
638 int max = end > 0 ? end - 1 : INT_MAX;
642 mtx_assert(&idr->lock, MA_OWNED);
644 if (unlikely(start < 0))
646 if (unlikely(max < start))
650 error = idr_get_new_locked(idr, ptr, &id);
652 error = idr_get_new_above_locked(idr, ptr, start, &id);
654 if (unlikely(error < 0))
656 if (unlikely(id > max)) {
657 idr_remove_locked(idr, id);
664 idr_alloc(struct idr *idr, void *ptr, int start, int end, gfp_t gfp_mask)
668 mtx_lock(&idr->lock);
669 retval = idr_alloc_locked(idr, ptr, start, end);
670 mtx_unlock(&idr->lock);
675 idr_alloc_cyclic(struct idr *idr, void *ptr, int start, int end, gfp_t gfp_mask)
679 mtx_lock(&idr->lock);
680 retval = idr_alloc_locked(idr, ptr, max(start, idr->next_cyclic_id), end);
681 if (unlikely(retval == -ENOSPC))
682 retval = idr_alloc_locked(idr, ptr, start, end);
683 if (likely(retval >= 0))
684 idr->next_cyclic_id = retval + 1;
685 mtx_unlock(&idr->lock);
690 idr_for_each_layer(struct idr_layer *il, int offset, int layer,
691 int (*f)(int id, void *p, void *data), void *data)
698 for (i = 0; i < IDR_SIZE; i++) {
699 if (il->ary[i] == NULL)
701 err = f(i + offset, il->ary[i], data);
707 for (i = 0; i < IDR_SIZE; i++) {
708 if (il->ary[i] == NULL)
710 err = idr_for_each_layer(il->ary[i],
711 (i + offset) * IDR_SIZE, layer - 1, f, data);
718 /* NOTE: It is not allowed to modify the IDR tree while it is being iterated */
720 idr_for_each(struct idr *idp, int (*f)(int id, void *p, void *data), void *data)
722 return (idr_for_each_layer(idp->top, 0, idp->layers - 1, f, data));
726 idr_has_entry(int id, void *p, void *data)
733 idr_is_empty(struct idr *idp)
736 return (idr_for_each(idp, idr_has_entry, NULL) == 0);
740 ida_pre_get(struct ida *ida, gfp_t flags)
742 if (idr_pre_get(&ida->idr, flags) == 0)
745 if (ida->free_bitmap == NULL) {
747 malloc(sizeof(struct ida_bitmap), M_IDR, flags);
749 return (ida->free_bitmap != NULL);
753 ida_simple_get(struct ida *ida, unsigned int start, unsigned int end,
759 MPASS((int)start >= 0);
760 MPASS((int)end >= 0);
769 if (!ida_pre_get(ida, flags))
772 if ((ret = ida_get_new_above(ida, start, &id)) == 0) {
780 if (__predict_false(ret == -EAGAIN))
787 ida_simple_remove(struct ida *ida, unsigned int id)
789 idr_remove(&ida->idr, id);
793 ida_remove(struct ida *ida, int id)
795 idr_remove(&ida->idr, id);
799 ida_init(struct ida *ida)
805 ida_destroy(struct ida *ida)
807 idr_destroy(&ida->idr);
808 free(ida->free_bitmap, M_IDR);