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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
|
/*-
* Copyright (c) 2010 Isilon Systems, Inc.
* Copyright (c) 2010 iX Systems, Inc.
* Copyright (c) 2010 Panasas, Inc.
* Copyright (c) 2013-2017 Mellanox Technologies, Ltd.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice unmodified, this list of conditions, and the following
* disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/malloc.h>
#include <sys/kernel.h>
#include <sys/sysctl.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <machine/stdarg.h>
#include <linux/bitmap.h>
#include <linux/kobject.h>
#include <linux/slab.h>
#include <linux/idr.h>
#include <linux/err.h>
#define MAX_IDR_LEVEL ((MAX_IDR_SHIFT + IDR_BITS - 1) / IDR_BITS)
#define MAX_IDR_FREE (MAX_IDR_LEVEL * 2)
struct linux_idr_cache {
spinlock_t lock;
struct idr_layer *head;
unsigned count;
};
static DPCPU_DEFINE(struct linux_idr_cache, linux_idr_cache);
/*
* IDR Implementation.
*
* This is quick and dirty and not as re-entrant as the linux version
* however it should be fairly fast. It is basically a radix tree with
* a builtin bitmap for allocation.
*/
static MALLOC_DEFINE(M_IDR, "idr", "Linux IDR compat");
static struct idr_layer *
idr_preload_dequeue_locked(struct linux_idr_cache *lic)
{
struct idr_layer *retval;
/* check if wrong thread is trying to dequeue */
if (mtx_owned(&lic->lock.m) == 0)
return (NULL);
retval = lic->head;
if (likely(retval != NULL)) {
lic->head = retval->ary[0];
lic->count--;
retval->ary[0] = NULL;
}
return (retval);
}
static void
idr_preload_init(void *arg)
{
int cpu;
CPU_FOREACH(cpu) {
struct linux_idr_cache *lic =
DPCPU_ID_PTR(cpu, linux_idr_cache);
spin_lock_init(&lic->lock);
}
}
SYSINIT(idr_preload_init, SI_SUB_CPU, SI_ORDER_ANY, idr_preload_init, NULL);
static void
idr_preload_uninit(void *arg)
{
int cpu;
CPU_FOREACH(cpu) {
struct idr_layer *cacheval;
struct linux_idr_cache *lic =
DPCPU_ID_PTR(cpu, linux_idr_cache);
while (1) {
spin_lock(&lic->lock);
cacheval = idr_preload_dequeue_locked(lic);
spin_unlock(&lic->lock);
if (cacheval == NULL)
break;
free(cacheval, M_IDR);
}
spin_lock_destroy(&lic->lock);
}
}
SYSUNINIT(idr_preload_uninit, SI_SUB_LOCK, SI_ORDER_FIRST, idr_preload_uninit, NULL);
void
idr_preload(gfp_t gfp_mask)
{
struct linux_idr_cache *lic;
struct idr_layer *cacheval;
sched_pin();
lic = &DPCPU_GET(linux_idr_cache);
/* fill up cache */
spin_lock(&lic->lock);
while (lic->count < MAX_IDR_FREE) {
spin_unlock(&lic->lock);
cacheval = malloc(sizeof(*cacheval), M_IDR, M_ZERO | gfp_mask);
spin_lock(&lic->lock);
if (cacheval == NULL)
break;
cacheval->ary[0] = lic->head;
lic->head = cacheval;
lic->count++;
}
}
void
idr_preload_end(void)
{
struct linux_idr_cache *lic;
lic = &DPCPU_GET(linux_idr_cache);
spin_unlock(&lic->lock);
sched_unpin();
}
static inline int
idr_max(struct idr *idr)
{
return (1 << (idr->layers * IDR_BITS)) - 1;
}
static inline int
idr_pos(int id, int layer)
{
return (id >> (IDR_BITS * layer)) & IDR_MASK;
}
void
idr_init(struct idr *idr)
{
bzero(idr, sizeof(*idr));
mtx_init(&idr->lock, "idr", NULL, MTX_DEF);
}
/* Only frees cached pages. */
void
idr_destroy(struct idr *idr)
{
struct idr_layer *il, *iln;
idr_remove_all(idr);
mtx_lock(&idr->lock);
for (il = idr->free; il != NULL; il = iln) {
iln = il->ary[0];
free(il, M_IDR);
}
mtx_unlock(&idr->lock);
mtx_destroy(&idr->lock);
}
static void
idr_remove_layer(struct idr_layer *il, int layer)
{
int i;
if (il == NULL)
return;
if (layer == 0) {
free(il, M_IDR);
return;
}
for (i = 0; i < IDR_SIZE; i++)
if (il->ary[i])
idr_remove_layer(il->ary[i], layer - 1);
}
void
idr_remove_all(struct idr *idr)
{
mtx_lock(&idr->lock);
idr_remove_layer(idr->top, idr->layers - 1);
idr->top = NULL;
idr->layers = 0;
mtx_unlock(&idr->lock);
}
static void
idr_remove_locked(struct idr *idr, int id)
{
struct idr_layer *il;
int layer;
int idx;
id &= MAX_ID_MASK;
il = idr->top;
layer = idr->layers - 1;
if (il == NULL || id > idr_max(idr))
return;
/*
* Walk down the tree to this item setting bitmaps along the way
* as we know at least one item will be free along this path.
*/
while (layer && il) {
idx = idr_pos(id, layer);
il->bitmap |= 1 << idx;
il = il->ary[idx];
layer--;
}
idx = id & IDR_MASK;
/*
* At this point we've set free space bitmaps up the whole tree.
* We could make this non-fatal and unwind but linux dumps a stack
* and a warning so I don't think it's necessary.
*/
if (il == NULL || (il->bitmap & (1 << idx)) != 0)
panic("idr_remove: Item %d not allocated (%p, %p)\n",
id, idr, il);
il->ary[idx] = NULL;
il->bitmap |= 1 << idx;
}
void
idr_remove(struct idr *idr, int id)
{
mtx_lock(&idr->lock);
idr_remove_locked(idr, id);
mtx_unlock(&idr->lock);
}
static inline struct idr_layer *
idr_find_layer_locked(struct idr *idr, int id)
{
struct idr_layer *il;
int layer;
id &= MAX_ID_MASK;
il = idr->top;
layer = idr->layers - 1;
if (il == NULL || id > idr_max(idr))
return (NULL);
while (layer && il) {
il = il->ary[idr_pos(id, layer)];
layer--;
}
return (il);
}
void *
idr_replace(struct idr *idr, void *ptr, int id)
{
struct idr_layer *il;
void *res;
int idx;
mtx_lock(&idr->lock);
il = idr_find_layer_locked(idr, id);
idx = id & IDR_MASK;
/* Replace still returns an error if the item was not allocated. */
if (il == NULL || (il->bitmap & (1 << idx))) {
res = ERR_PTR(-ENOENT);
} else {
res = il->ary[idx];
il->ary[idx] = ptr;
}
mtx_unlock(&idr->lock);
return (res);
}
static inline void *
idr_find_locked(struct idr *idr, int id)
{
struct idr_layer *il;
void *res;
mtx_assert(&idr->lock, MA_OWNED);
il = idr_find_layer_locked(idr, id);
if (il != NULL)
res = il->ary[id & IDR_MASK];
else
res = NULL;
return (res);
}
void *
idr_find(struct idr *idr, int id)
{
void *res;
mtx_lock(&idr->lock);
res = idr_find_locked(idr, id);
mtx_unlock(&idr->lock);
return (res);
}
void *
idr_get_next(struct idr *idr, int *nextidp)
{
void *res = NULL;
int id = *nextidp;
mtx_lock(&idr->lock);
for (; id <= idr_max(idr); id++) {
res = idr_find_locked(idr, id);
if (res == NULL)
continue;
*nextidp = id;
break;
}
mtx_unlock(&idr->lock);
return (res);
}
int
idr_pre_get(struct idr *idr, gfp_t gfp_mask)
{
struct idr_layer *il, *iln;
struct idr_layer *head;
int need;
mtx_lock(&idr->lock);
for (;;) {
need = idr->layers + 1;
for (il = idr->free; il != NULL; il = il->ary[0])
need--;
mtx_unlock(&idr->lock);
if (need <= 0)
break;
for (head = NULL; need; need--) {
iln = malloc(sizeof(*il), M_IDR, M_ZERO | gfp_mask);
if (iln == NULL)
break;
bitmap_fill(&iln->bitmap, IDR_SIZE);
if (head != NULL) {
il->ary[0] = iln;
il = iln;
} else
head = il = iln;
}
if (head == NULL)
return (0);
mtx_lock(&idr->lock);
il->ary[0] = idr->free;
idr->free = head;
}
return (1);
}
static struct idr_layer *
idr_free_list_get(struct idr *idp)
{
struct idr_layer *il;
if ((il = idp->free) != NULL) {
idp->free = il->ary[0];
il->ary[0] = NULL;
}
return (il);
}
static inline struct idr_layer *
idr_get(struct idr *idp)
{
struct idr_layer *il;
if ((il = idr_free_list_get(idp)) != NULL) {
MPASS(il->bitmap != 0);
} else if ((il = malloc(sizeof(*il), M_IDR, M_ZERO | M_NOWAIT)) != NULL) {
bitmap_fill(&il->bitmap, IDR_SIZE);
} else if ((il = idr_preload_dequeue_locked(&DPCPU_GET(linux_idr_cache))) != NULL) {
bitmap_fill(&il->bitmap, IDR_SIZE);
} else {
return (NULL);
}
return (il);
}
/*
* Could be implemented as get_new_above(idr, ptr, 0, idp) but written
* first for simplicity sake.
*/
static int
idr_get_new_locked(struct idr *idr, void *ptr, int *idp)
{
struct idr_layer *stack[MAX_LEVEL];
struct idr_layer *il;
int error;
int layer;
int idx;
int id;
mtx_assert(&idr->lock, MA_OWNED);
error = -EAGAIN;
/*
* Expand the tree until there is free space.
*/
if (idr->top == NULL || idr->top->bitmap == 0) {
if (idr->layers == MAX_LEVEL + 1) {
error = -ENOSPC;
goto out;
}
il = idr_get(idr);
if (il == NULL)
goto out;
il->ary[0] = idr->top;
if (idr->top)
il->bitmap &= ~1;
idr->top = il;
idr->layers++;
}
il = idr->top;
id = 0;
/*
* Walk the tree following free bitmaps, record our path.
*/
for (layer = idr->layers - 1;; layer--) {
stack[layer] = il;
idx = ffsl(il->bitmap);
if (idx == 0)
panic("idr_get_new: Invalid leaf state (%p, %p)\n",
idr, il);
idx--;
id |= idx << (layer * IDR_BITS);
if (layer == 0)
break;
if (il->ary[idx] == NULL) {
il->ary[idx] = idr_get(idr);
if (il->ary[idx] == NULL)
goto out;
}
il = il->ary[idx];
}
/*
* Allocate the leaf to the consumer.
*/
il->bitmap &= ~(1 << idx);
il->ary[idx] = ptr;
*idp = id;
/*
* Clear bitmaps potentially up to the root.
*/
while (il->bitmap == 0 && ++layer < idr->layers) {
il = stack[layer];
il->bitmap &= ~(1 << idr_pos(id, layer));
}
error = 0;
out:
#ifdef INVARIANTS
if (error == 0 && idr_find_locked(idr, id) != ptr) {
panic("idr_get_new: Failed for idr %p, id %d, ptr %p\n",
idr, id, ptr);
}
#endif
return (error);
}
int
idr_get_new(struct idr *idr, void *ptr, int *idp)
{
int retval;
mtx_lock(&idr->lock);
retval = idr_get_new_locked(idr, ptr, idp);
mtx_unlock(&idr->lock);
return (retval);
}
static int
idr_get_new_above_locked(struct idr *idr, void *ptr, int starting_id, int *idp)
{
struct idr_layer *stack[MAX_LEVEL];
struct idr_layer *il;
int error;
int layer;
int idx, sidx;
int id;
mtx_assert(&idr->lock, MA_OWNED);
error = -EAGAIN;
/*
* Compute the layers required to support starting_id and the mask
* at the top layer.
*/
restart:
idx = starting_id;
layer = 0;
while (idx & ~IDR_MASK) {
layer++;
idx >>= IDR_BITS;
}
if (layer == MAX_LEVEL + 1) {
error = -ENOSPC;
goto out;
}
/*
* Expand the tree until there is free space at or beyond starting_id.
*/
while (idr->layers <= layer ||
idr->top->bitmap < (1 << idr_pos(starting_id, idr->layers - 1))) {
if (idr->layers == MAX_LEVEL + 1) {
error = -ENOSPC;
goto out;
}
il = idr_get(idr);
if (il == NULL)
goto out;
il->ary[0] = idr->top;
if (idr->top && idr->top->bitmap == 0)
il->bitmap &= ~1;
idr->top = il;
idr->layers++;
}
il = idr->top;
id = 0;
/*
* Walk the tree following free bitmaps, record our path.
*/
for (layer = idr->layers - 1;; layer--) {
stack[layer] = il;
sidx = idr_pos(starting_id, layer);
/* Returns index numbered from 0 or size if none exists. */
idx = find_next_bit(&il->bitmap, IDR_SIZE, sidx);
if (idx == IDR_SIZE && sidx == 0)
panic("idr_get_new: Invalid leaf state (%p, %p)\n",
idr, il);
/*
* We may have walked a path where there was a free bit but
* it was lower than what we wanted. Restart the search with
* a larger starting id. id contains the progress we made so
* far. Search the leaf one above this level. This may
* restart as many as MAX_LEVEL times but that is expected
* to be rare.
*/
if (idx == IDR_SIZE) {
starting_id = id + (1 << ((layer + 1) * IDR_BITS));
goto restart;
}
if (idx > sidx)
starting_id = 0; /* Search the whole subtree. */
id |= idx << (layer * IDR_BITS);
if (layer == 0)
break;
if (il->ary[idx] == NULL) {
il->ary[idx] = idr_get(idr);
if (il->ary[idx] == NULL)
goto out;
}
il = il->ary[idx];
}
/*
* Allocate the leaf to the consumer.
*/
il->bitmap &= ~(1 << idx);
il->ary[idx] = ptr;
*idp = id;
/*
* Clear bitmaps potentially up to the root.
*/
while (il->bitmap == 0 && ++layer < idr->layers) {
il = stack[layer];
il->bitmap &= ~(1 << idr_pos(id, layer));
}
error = 0;
out:
#ifdef INVARIANTS
if (error == 0 && idr_find_locked(idr, id) != ptr) {
panic("idr_get_new_above: Failed for idr %p, id %d, ptr %p\n",
idr, id, ptr);
}
#endif
return (error);
}
int
idr_get_new_above(struct idr *idr, void *ptr, int starting_id, int *idp)
{
int retval;
mtx_lock(&idr->lock);
retval = idr_get_new_above_locked(idr, ptr, starting_id, idp);
mtx_unlock(&idr->lock);
return (retval);
}
int
ida_get_new_above(struct ida *ida, int starting_id, int *p_id)
{
return (idr_get_new_above(&ida->idr, NULL, starting_id, p_id));
}
static int
idr_alloc_locked(struct idr *idr, void *ptr, int start, int end)
{
int max = end > 0 ? end - 1 : INT_MAX;
int error;
int id;
mtx_assert(&idr->lock, MA_OWNED);
if (unlikely(start < 0))
return (-EINVAL);
if (unlikely(max < start))
return (-ENOSPC);
if (start == 0)
error = idr_get_new_locked(idr, ptr, &id);
else
error = idr_get_new_above_locked(idr, ptr, start, &id);
if (unlikely(error < 0))
return (error);
if (unlikely(id > max)) {
idr_remove_locked(idr, id);
return (-ENOSPC);
}
return (id);
}
int
idr_alloc(struct idr *idr, void *ptr, int start, int end, gfp_t gfp_mask)
{
int retval;
mtx_lock(&idr->lock);
retval = idr_alloc_locked(idr, ptr, start, end);
mtx_unlock(&idr->lock);
return (retval);
}
int
idr_alloc_cyclic(struct idr *idr, void *ptr, int start, int end, gfp_t gfp_mask)
{
int retval;
mtx_lock(&idr->lock);
retval = idr_alloc_locked(idr, ptr, max(start, idr->next_cyclic_id), end);
if (unlikely(retval == -ENOSPC))
retval = idr_alloc_locked(idr, ptr, start, end);
if (likely(retval >= 0))
idr->next_cyclic_id = retval + 1;
mtx_unlock(&idr->lock);
return (retval);
}
static int
idr_for_each_layer(struct idr_layer *il, int offset, int layer,
int (*f)(int id, void *p, void *data), void *data)
{
int i, err;
if (il == NULL)
return (0);
if (layer == 0) {
for (i = 0; i < IDR_SIZE; i++) {
if (il->ary[i] == NULL)
continue;
err = f(i + offset, il->ary[i], data);
if (err)
return (err);
}
return (0);
}
for (i = 0; i < IDR_SIZE; i++) {
if (il->ary[i] == NULL)
continue;
err = idr_for_each_layer(il->ary[i],
(i + offset) * IDR_SIZE, layer - 1, f, data);
if (err)
return (err);
}
return (0);
}
/* NOTE: It is not allowed to modify the IDR tree while it is being iterated */
int
idr_for_each(struct idr *idp, int (*f)(int id, void *p, void *data), void *data)
{
return (idr_for_each_layer(idp->top, 0, idp->layers - 1, f, data));
}
int
ida_pre_get(struct ida *ida, gfp_t flags)
{
if (idr_pre_get(&ida->idr, flags) == 0)
return (0);
if (ida->free_bitmap == NULL) {
ida->free_bitmap =
malloc(sizeof(struct ida_bitmap), M_IDR, flags);
}
return (ida->free_bitmap != NULL);
}
int
ida_simple_get(struct ida *ida, unsigned int start, unsigned int end,
gfp_t flags)
{
int ret, id;
unsigned int max;
MPASS((int)start >= 0);
MPASS((int)end >= 0);
if (end == 0)
max = 0x80000000;
else {
MPASS(end > start);
max = end - 1;
}
again:
if (!ida_pre_get(ida, flags))
return (-ENOMEM);
if ((ret = ida_get_new_above(ida, start, &id)) == 0) {
if (id > max) {
ida_remove(ida, id);
ret = -ENOSPC;
} else {
ret = id;
}
}
if (__predict_false(ret == -EAGAIN))
goto again;
return (ret);
}
void
ida_simple_remove(struct ida *ida, unsigned int id)
{
idr_remove(&ida->idr, id);
}
void
ida_remove(struct ida *ida, int id)
{
idr_remove(&ida->idr, id);
}
void
ida_init(struct ida *ida)
{
idr_init(&ida->idr);
}
void
ida_destroy(struct ida *ida)
{
idr_destroy(&ida->idr);
free(ida->free_bitmap, M_IDR);
}
|