2 * Copyright (c) 2004 Poul-Henning Kamp
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * Unit number allocation functions.
31 * These functions implement a mixed run-length/bitmap management of unit
32 * number spaces in a very memory efficient manner.
34 * Allocation policy is always lowest free number first.
36 * A return value of -1 signals that no more unit numbers are available.
38 * There is no cost associated with the range of unitnumbers, so unless
39 * the resource really is finite, specify INT_MAX to new_unrhdr() and
40 * forget about checking the return value.
42 * If a mutex is not provided when the unit number space is created, a
43 * default global mutex is used. The advantage to passing a mutex in, is
44 * that the alloc_unrl() function can be called with the mutex already
45 * held (it will not be released by alloc_unrl()).
47 * The allocation function alloc_unr{l}() never sleeps (but it may block on
48 * the mutex of course).
50 * Freeing a unit number may require allocating memory, and can therefore
51 * sleep so the free_unr() function does not come in a pre-locked variant.
53 * A userland test program is included.
55 * Memory usage is a very complex function of the exact allocation
56 * pattern, but always very compact:
57 * * For the very typical case where a single unbroken run of unit
58 * numbers are allocated 44 bytes are used on i386.
59 * * For a unit number space of 1000 units and the random pattern
60 * in the usermode test program included, the worst case usage
61 * was 252 bytes on i386 for 500 allocated and 500 free units.
62 * * For a unit number space of 10000 units and the random pattern
63 * in the usermode test program included, the worst case usage
64 * was 798 bytes on i386 for 5000 allocated and 5000 free units.
65 * * The worst case is where every other unit number is allocated and
66 * the rest are free. In that case 44 + N/4 bytes are used where
67 * N is the number of the highest unit allocated.
70 #include <sys/types.h>
71 #include <sys/queue.h>
72 #include <sys/bitstring.h>
76 #include <sys/param.h>
77 #include <sys/malloc.h>
78 #include <sys/kernel.h>
79 #include <sys/systm.h>
80 #include <sys/limits.h>
82 #include <sys/mutex.h>
85 * In theory it would be smarter to allocate the individual blocks
86 * with the zone allocator, but at this time the expectation is that
87 * there will typically not even be enough allocations to fill a single
88 * page, so we stick with malloc for now.
90 static MALLOC_DEFINE(M_UNIT, "Unitno", "Unit number allocation");
92 #define Malloc(foo) malloc(foo, M_UNIT, M_WAITOK | M_ZERO)
93 #define Free(foo) free(foo, M_UNIT)
95 static struct mtx unitmtx;
97 MTX_SYSINIT(unit, &unitmtx, "unit# allocation", MTX_DEF);
99 #else /* ...USERLAND */
105 #define KASSERT(cond, arg) \
114 #define Malloc(foo) _Malloc(foo, __LINE__)
116 _Malloc(size_t foo, int line)
119 KASSERT(no_alloc == 0, ("malloc in wrong place() line %d", line));
120 return (calloc(foo, 1));
122 #define Free(foo) free(foo)
132 mtx_lock(struct mtx *mp)
134 KASSERT(mp->state == 0, ("mutex already locked"));
139 mtx_unlock(struct mtx *mp)
141 KASSERT(mp->state == 1, ("mutex not locked"));
148 mtx_assert(struct mtx *mp, int flag)
150 if (flag == MA_OWNED) {
151 KASSERT(mp->state == 1, ("mtx_assert(MA_OWNED) not true"));
155 #define CTASSERT(foo)
156 #define WITNESS_WARN(flags, lock, fmt, ...) (void)0
158 #endif /* USERLAND */
161 * This is our basic building block.
163 * It can be used in three different ways depending on the value of the ptr
165 * If ptr is NULL, it represents a run of free items.
166 * If ptr points to the unrhdr it represents a run of allocated items.
167 * Otherwise it points to an bitstring of allocated items.
169 * For runs the len field is the length of the run.
170 * For bitmaps the len field represents the number of allocated items.
172 * The bitmap is the same size as struct unr to optimize memory management.
175 TAILQ_ENTRY(unr) list;
182 bitstr_t map[sizeof(struct unr) - 1];
185 CTASSERT(sizeof(struct unr) == sizeof(struct unrb));
187 /* Number of bits in the bitmap */
188 #define NBITS ((int)sizeof(((struct unrb *)NULL)->map) * 8)
190 /* Header element for a unr number space. */
193 TAILQ_HEAD(unrhd,unr) head;
194 u_int low; /* Lowest item */
195 u_int high; /* Highest item */
196 u_int busy; /* Count of allocated items */
197 u_int alloc; /* Count of memory allocations */
198 u_int first; /* items in allocated from start */
199 u_int last; /* items free at end */
201 TAILQ_HEAD(unrfr,unr) ppfree; /* Items to be freed after mtx
206 #if defined(DIAGNOSTIC) || !defined(_KERNEL)
208 * Consistency check function.
210 * Checks the internal consistency as well as we can.
212 * Called at all boundaries of this API.
215 check_unrhdr(struct unrhdr *uh, int line)
223 TAILQ_FOREACH(up, &uh->head, list) {
225 if (up->ptr != uh && up->ptr != NULL) {
227 KASSERT (up->len <= NBITS,
228 ("UNR inconsistency: len %u max %d (line %d)\n",
229 up->len, NBITS, line));
232 for (x = 0; x < up->len; x++)
233 if (bit_test(ub->map, x))
235 KASSERT (w == ub->busy,
236 ("UNR inconsistency: busy %u found %u (line %d)\n",
239 } else if (up->ptr != NULL)
242 KASSERT (y == uh->busy,
243 ("UNR inconsistency: items %u found %u (line %d)\n",
245 KASSERT (z == uh->alloc,
246 ("UNR inconsistency: chunks %u found %u (line %d)\n",
247 uh->alloc, z, line));
253 check_unrhdr(struct unrhdr *uh, int line)
262 * Userland memory management. Just use calloc and keep track of how
263 * many elements we have allocated for check_unrhdr().
266 static __inline void *
267 new_unr(struct unrhdr *uh, void **p1, void **p2)
272 KASSERT(*p1 != NULL || *p2 != NULL, ("Out of cached memory"));
285 delete_unr(struct unrhdr *uh, void *ptr)
291 TAILQ_INSERT_TAIL(&uh->ppfree, up, list);
295 clean_unrhdrl(struct unrhdr *uh)
299 mtx_assert(uh->mtx, MA_OWNED);
300 while ((up = TAILQ_FIRST(&uh->ppfree)) != NULL) {
301 TAILQ_REMOVE(&uh->ppfree, up, list);
310 clean_unrhdr(struct unrhdr *uh)
319 * Allocate a new unrheader set.
321 * Highest and lowest valid values given as parameters.
325 new_unrhdr(int low, int high, struct mtx *mutex)
329 KASSERT(low >= 0 && low <= high,
330 ("UNR: use error: new_unrhdr(%d, %d)", low, high));
331 uh = Malloc(sizeof *uh);
336 TAILQ_INIT(&uh->head);
337 TAILQ_INIT(&uh->ppfree);
341 uh->last = 1 + (high - low);
342 check_unrhdr(uh, __LINE__);
347 delete_unrhdr(struct unrhdr *uh)
350 check_unrhdr(uh, __LINE__);
351 KASSERT(uh->busy == 0, ("unrhdr has %u allocations", uh->busy));
352 KASSERT(uh->alloc == 0, ("UNR memory leak in delete_unrhdr"));
353 KASSERT(TAILQ_FIRST(&uh->ppfree) == NULL,
354 ("unrhdr has postponed item for free"));
359 is_bitmap(struct unrhdr *uh, struct unr *up)
361 return (up->ptr != uh && up->ptr != NULL);
365 * Look for sequence of items which can be combined into a bitmap, if
366 * multiple are present, take the one which saves most memory.
368 * Return (1) if a sequence was found to indicate that another call
369 * might be able to do more. Return (0) if we found no suitable sequence.
371 * NB: called from alloc_unr(), no new memory allocation allowed.
374 optimize_unr(struct unrhdr *uh)
376 struct unr *up, *uf, *us;
377 struct unrb *ub, *ubf;
381 * Look for the run of items (if any) which when collapsed into
382 * a bitmap would save most memory.
386 TAILQ_FOREACH(uf, &uh->head, list) {
387 if (uf->len >= NBITS)
390 if (is_bitmap(uh, uf))
395 up = TAILQ_NEXT(up, list);
398 if ((up->len + l) > NBITS)
401 if (is_bitmap(uh, up))
414 * If the first element is not a bitmap, make it one.
415 * Trying to do so without allocating more memory complicates things
418 if (!is_bitmap(uh, us)) {
419 uf = TAILQ_NEXT(us, list);
420 TAILQ_REMOVE(&uh->head, us, list);
422 l = us->ptr == uh ? 1 : 0;
426 bit_nset(ub->map, 0, a);
429 bit_nclear(ub->map, 0, a);
431 if (!is_bitmap(uh, uf)) {
432 if (uf->ptr == NULL) {
433 bit_nclear(ub->map, a, a + uf->len - 1);
435 bit_nset(ub->map, a, a + uf->len - 1);
443 for (l = 0; l < uf->len; l++, a++) {
444 if (bit_test(ubf->map, l)) {
448 bit_clear(ub->map, a);
452 delete_unr(uh, uf->ptr);
459 uf = TAILQ_NEXT(us, list);
462 if (uf->len + us->len > NBITS)
464 if (uf->ptr == NULL) {
465 bit_nclear(ub->map, us->len, us->len + uf->len - 1);
467 TAILQ_REMOVE(&uh->head, uf, list);
469 } else if (uf->ptr == uh) {
470 bit_nset(ub->map, us->len, us->len + uf->len - 1);
473 TAILQ_REMOVE(&uh->head, uf, list);
477 for (l = 0; l < uf->len; l++, us->len++) {
478 if (bit_test(ubf->map, l)) {
479 bit_set(ub->map, us->len);
482 bit_clear(ub->map, us->len);
485 TAILQ_REMOVE(&uh->head, uf, list);
493 * See if a given unr should be collapsed with a neighbor.
495 * NB: called from alloc_unr(), no new memory allocation allowed.
498 collapse_unr(struct unrhdr *uh, struct unr *up)
503 /* If bitmap is all set or clear, change it to runlength */
504 if (is_bitmap(uh, up)) {
506 if (ub->busy == up->len) {
507 delete_unr(uh, up->ptr);
509 } else if (ub->busy == 0) {
510 delete_unr(uh, up->ptr);
515 /* If nothing left in runlength, delete it */
517 upp = TAILQ_PREV(up, unrhd, list);
519 upp = TAILQ_NEXT(up, list);
520 TAILQ_REMOVE(&uh->head, up, list);
525 /* If we have "hot-spot" still, merge with neighbor if possible */
527 upp = TAILQ_PREV(up, unrhd, list);
528 if (upp != NULL && up->ptr == upp->ptr) {
530 TAILQ_REMOVE(&uh->head, upp, list);
533 upp = TAILQ_NEXT(up, list);
534 if (upp != NULL && up->ptr == upp->ptr) {
536 TAILQ_REMOVE(&uh->head, upp, list);
541 /* Merge into ->first if possible */
542 upp = TAILQ_FIRST(&uh->head);
543 if (upp != NULL && upp->ptr == uh) {
544 uh->first += upp->len;
545 TAILQ_REMOVE(&uh->head, upp, list);
551 /* Merge into ->last if possible */
552 upp = TAILQ_LAST(&uh->head, unrhd);
553 if (upp != NULL && upp->ptr == NULL) {
554 uh->last += upp->len;
555 TAILQ_REMOVE(&uh->head, upp, list);
561 /* Try to make bitmaps */
562 while (optimize_unr(uh))
567 * Allocate a free unr.
570 alloc_unrl(struct unrhdr *uh)
577 mtx_assert(uh->mtx, MA_OWNED);
578 check_unrhdr(uh, __LINE__);
579 x = uh->low + uh->first;
581 up = TAILQ_FIRST(&uh->head);
584 * If we have an ideal split, just adjust the first+last
586 if (up == NULL && uh->last > 0) {
594 * We can always allocate from the first list element, so if we have
595 * nothing on the list, we must have run out of unit numbers.
600 KASSERT(up->ptr != uh, ("UNR first element is allocated"));
602 if (up->ptr == NULL) { /* free run */
605 } else { /* bitmap */
607 KASSERT(ub->busy < up->len, ("UNR bitmap confusion"));
608 bit_ffc(ub->map, up->len, &y);
609 KASSERT(y != -1, ("UNR corruption: No clear bit in bitmap."));
615 collapse_unr(uh, up);
620 alloc_unr(struct unrhdr *uh)
632 alloc_unr_specificl(struct unrhdr *uh, u_int item, void **p1, void **p2)
634 struct unr *up, *upn;
638 mtx_assert(uh->mtx, MA_OWNED);
640 if (item < uh->low + uh->first || item > uh->high)
643 up = TAILQ_FIRST(&uh->head);
645 if (up == NULL && item - uh->low == uh->first) {
649 check_unrhdr(uh, __LINE__);
653 i = item - uh->low - uh->first;
656 up = new_unr(uh, p1, p2);
659 TAILQ_INSERT_TAIL(&uh->head, up, list);
660 up = new_unr(uh, p1, p2);
663 TAILQ_INSERT_TAIL(&uh->head, up, list);
664 uh->last = uh->high - uh->low - i;
666 check_unrhdr(uh, __LINE__);
669 /* Find the item which contains the unit we want to allocate. */
670 TAILQ_FOREACH(up, &uh->head, list) {
679 up = new_unr(uh, p1, p2);
682 TAILQ_INSERT_TAIL(&uh->head, up, list);
684 up = new_unr(uh, p1, p2);
687 TAILQ_INSERT_TAIL(&uh->head, up, list);
691 if (is_bitmap(uh, up)) {
693 if (bit_test(ub->map, i) == 0) {
699 } else if (up->ptr == uh)
702 KASSERT(up->ptr == NULL,
703 ("alloc_unr_specificl: up->ptr != NULL (up=%p)", up));
705 /* Split off the tail end, if any. */
706 tl = up->len - (1 + i);
708 upn = new_unr(uh, p1, p2);
711 TAILQ_INSERT_AFTER(&uh->head, up, upn, list);
714 /* Split off head end, if any */
716 upn = new_unr(uh, p1, p2);
719 TAILQ_INSERT_BEFORE(up, upn, list);
725 last = uh->high - uh->low - (item - uh->low);
729 collapse_unr(uh, up);
730 check_unrhdr(uh, __LINE__);
735 alloc_unr_specific(struct unrhdr *uh, u_int item)
740 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "alloc_unr_specific");
742 p1 = Malloc(sizeof(struct unr));
743 p2 = Malloc(sizeof(struct unr));
746 i = alloc_unr_specificl(uh, item, &p1, &p2);
760 * If we can save unrs by using a bitmap, do so.
763 free_unrl(struct unrhdr *uh, u_int item, void **p1, void **p2)
765 struct unr *up, *upp, *upn;
769 KASSERT(item >= uh->low && item <= uh->high,
770 ("UNR: free_unr(%u) out of range [%u...%u]",
771 item, uh->low, uh->high));
772 check_unrhdr(uh, __LINE__);
774 upp = TAILQ_FIRST(&uh->head);
776 * Freeing in the ideal split case
778 if (item + 1 == uh->first && upp == NULL) {
782 check_unrhdr(uh, __LINE__);
786 * Freeing in the ->first section. Create a run starting at the
787 * freed item. The code below will subdivide it.
789 if (item < uh->first) {
790 up = new_unr(uh, p1, p2);
792 up->len = uh->first - item;
793 TAILQ_INSERT_HEAD(&uh->head, up, list);
794 uh->first -= up->len;
799 /* Find the item which contains the unit we want to free */
800 TAILQ_FOREACH(up, &uh->head, list) {
806 /* Handle bitmap items */
807 if (is_bitmap(uh, up)) {
810 KASSERT(bit_test(ub->map, item) != 0,
811 ("UNR: Freeing free item %d (bitmap)\n", item));
812 bit_clear(ub->map, item);
815 collapse_unr(uh, up);
819 KASSERT(up->ptr == uh, ("UNR Freeing free item %d (run))\n", item));
821 /* Just this one left, reap it */
825 collapse_unr(uh, up);
829 /* Check if we can shift the item into the previous 'free' run */
830 upp = TAILQ_PREV(up, unrhd, list);
831 if (item == 0 && upp != NULL && upp->ptr == NULL) {
835 collapse_unr(uh, up);
839 /* Check if we can shift the item to the next 'free' run */
840 upn = TAILQ_NEXT(up, list);
841 if (item == up->len - 1 && upn != NULL && upn->ptr == NULL) {
845 collapse_unr(uh, up);
849 /* Split off the tail end, if any. */
850 pl = up->len - (1 + item);
852 upp = new_unr(uh, p1, p2);
855 TAILQ_INSERT_AFTER(&uh->head, up, upp, list);
858 /* Split off head end, if any */
860 upp = new_unr(uh, p1, p2);
863 TAILQ_INSERT_BEFORE(up, upp, list);
868 collapse_unr(uh, up);
872 free_unr(struct unrhdr *uh, u_int item)
876 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "free_unr");
877 p1 = Malloc(sizeof(struct unr));
878 p2 = Malloc(sizeof(struct unr));
880 free_unrl(uh, item, &p1, &p2);
889 #ifndef _KERNEL /* USERLAND test driver */
892 * Simple stochastic test driver for the above functions
896 print_unr(struct unrhdr *uh, struct unr *up)
901 printf(" %p len = %5u ", up, up->len);
904 else if (up->ptr == uh)
908 printf("bitmap(%d) [", ub->busy);
909 for (x = 0; x < up->len; x++) {
910 if (bit_test(ub->map, x))
920 print_unrhdr(struct unrhdr *uh)
926 "%p low = %u high = %u first = %u last = %u busy %u chunks = %u\n",
927 uh, uh->low, uh->high, uh->first, uh->last, uh->busy, uh->alloc);
928 x = uh->low + uh->first;
929 TAILQ_FOREACH(up, &uh->head, list) {
930 printf(" from = %5u", x);
932 if (up->ptr == NULL || up->ptr == uh)
940 test_alloc_unr(struct unrhdr *uh, u_int i, char a[])
960 test_alloc_unr_specific(struct unrhdr *uh, u_int i, char a[])
964 j = alloc_unr_specific(uh, i);
975 /* Number of unrs to test */
979 main(int argc __unused, const char **argv __unused)
985 setbuf(stdout, NULL);
986 uh = new_unrhdr(0, NN - 1, NULL);
989 memset(a, 0, sizeof a);
992 fprintf(stderr, "sizeof(struct unr) %zu\n", sizeof(struct unr));
993 fprintf(stderr, "sizeof(struct unrb) %zu\n", sizeof(struct unrb));
994 fprintf(stderr, "sizeof(struct unrhdr) %zu\n", sizeof(struct unrhdr));
995 fprintf(stderr, "NBITS %d\n", NBITS);
997 for (m = 0; m < NN * 100; m++) {
1001 if (a[i] && (j & 1))
1004 if ((random() & 1) != 0)
1005 test_alloc_unr(uh, i, a);
1007 test_alloc_unr_specific(uh, i, a);
1009 if (1) /* XXX: change this for detailed debug printout */
1011 check_unrhdr(uh, __LINE__);
1013 for (i = 0; i < NN; i++) {
1015 printf("C %u\n", i);