1 /*- 2 * Copyright (c) 2004 Poul-Henning Kamp 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 13 * 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 24 * SUCH DAMAGE. 25 * 26 * $FreeBSD$ 27 * 28 * 29 * Unit number allocation functions. 30 * 31 * These functions implement a mixed run-length/bitmap management of unit 32 * number spaces in a very memory efficient manner. 33 * 34 * Allocation policy is always lowest free number first. 35 * 36 * A return value of -1 signals that no more unit numbers are available. 37 * 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. 41 * 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()). 46 * 47 * The allocation function alloc_unr{l}() never sleeps (but it may block on 48 * the mutex of course). 49 * 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. 52 * 53 * A userland test program is included. 54 * 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. 68 */ 69 70 #include <sys/param.h> 71 #include <sys/types.h> 72 #include <sys/_unrhdr.h> 73 74 #ifdef _KERNEL 75 76 #include <sys/bitstring.h> 77 #include <sys/malloc.h> 78 #include <sys/kernel.h> 79 #include <sys/systm.h> 80 #include <sys/limits.h> 81 #include <sys/lock.h> 82 #include <sys/mutex.h> 83 84 /* 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. 89 */ 90 static MALLOC_DEFINE(M_UNIT, "Unitno", "Unit number allocation"); 91 92 #define Malloc(foo) malloc(foo, M_UNIT, M_WAITOK | M_ZERO) 93 #define Free(foo) free(foo, M_UNIT) 94 95 static struct mtx unitmtx; 96 97 MTX_SYSINIT(unit, &unitmtx, "unit# allocation", MTX_DEF); 98 99 #else /* ...USERLAND */ 100 101 #include <bitstring.h> 102 #include <err.h> 103 #include <errno.h> 104 #include <getopt.h> 105 #include <stdbool.h> 106 #include <stdio.h> 107 #include <stdlib.h> 108 #include <string.h> 109 110 #define KASSERT(cond, arg) \ 111 do { \ 112 if (!(cond)) { \ 113 printf arg; \ 114 abort(); \ 115 } \ 116 } while (0) 117 118 static int no_alloc; 119 #define Malloc(foo) _Malloc(foo, __LINE__) 120 static void * 121 _Malloc(size_t foo, int line) 122 { 123 124 KASSERT(no_alloc == 0, ("malloc in wrong place() line %d", line)); 125 return (calloc(foo, 1)); 126 } 127 #define Free(foo) free(foo) 128 129 struct unrhdr; 130 131 132 struct mtx { 133 int state; 134 } unitmtx; 135 136 static void 137 mtx_lock(struct mtx *mp) 138 { 139 KASSERT(mp->state == 0, ("mutex already locked")); 140 mp->state = 1; 141 } 142 143 static void 144 mtx_unlock(struct mtx *mp) 145 { 146 KASSERT(mp->state == 1, ("mutex not locked")); 147 mp->state = 0; 148 } 149 150 #define MA_OWNED 9 151 152 static void 153 mtx_assert(struct mtx *mp, int flag) 154 { 155 if (flag == MA_OWNED) { 156 KASSERT(mp->state == 1, ("mtx_assert(MA_OWNED) not true")); 157 } 158 } 159 160 #define CTASSERT(foo) 161 #define WITNESS_WARN(flags, lock, fmt, ...) (void)0 162 163 #endif /* USERLAND */ 164 165 /* 166 * This is our basic building block. 167 * 168 * It can be used in three different ways depending on the value of the ptr 169 * element: 170 * If ptr is NULL, it represents a run of free items. 171 * If ptr points to the unrhdr it represents a run of allocated items. 172 * Otherwise it points to a bitstring of allocated items. 173 * 174 * For runs the len field is the length of the run. 175 * For bitmaps the len field represents the number of allocated items. 176 * 177 * The bitmap is the same size as struct unr to optimize memory management. 178 */ 179 struct unr { 180 TAILQ_ENTRY(unr) list; 181 u_int len; 182 void *ptr; 183 }; 184 185 struct unrb { 186 bitstr_t map[sizeof(struct unr) / sizeof(bitstr_t)]; 187 }; 188 189 CTASSERT((sizeof(struct unr) % sizeof(bitstr_t)) == 0); 190 191 /* Number of bits we can store in the bitmap */ 192 #define NBITS (8 * sizeof(((struct unrb*)NULL)->map)) 193 194 /* Is the unrb empty in at least the first len bits? */ 195 static inline bool 196 ub_empty(struct unrb *ub, int len) { 197 int first_set; 198 199 bit_ffs(ub->map, len, &first_set); 200 return (first_set == -1); 201 } 202 203 /* Is the unrb full? That is, is the number of set elements equal to len? */ 204 static inline bool 205 ub_full(struct unrb *ub, int len) 206 { 207 int first_clear; 208 209 bit_ffc(ub->map, len, &first_clear); 210 return (first_clear == -1); 211 } 212 213 214 #if defined(DIAGNOSTIC) || !defined(_KERNEL) 215 /* 216 * Consistency check function. 217 * 218 * Checks the internal consistency as well as we can. 219 * 220 * Called at all boundaries of this API. 221 */ 222 static void 223 check_unrhdr(struct unrhdr *uh, int line) 224 { 225 struct unr *up; 226 struct unrb *ub; 227 int w; 228 u_int y, z; 229 230 y = uh->first; 231 z = 0; 232 TAILQ_FOREACH(up, &uh->head, list) { 233 z++; 234 if (up->ptr != uh && up->ptr != NULL) { 235 ub = up->ptr; 236 KASSERT (up->len <= NBITS, 237 ("UNR inconsistency: len %u max %zd (line %d)\n", 238 up->len, NBITS, line)); 239 z++; 240 w = 0; 241 bit_count(ub->map, 0, up->len, &w); 242 y += w; 243 } else if (up->ptr != NULL) 244 y += up->len; 245 } 246 KASSERT (y == uh->busy, 247 ("UNR inconsistency: items %u found %u (line %d)\n", 248 uh->busy, y, line)); 249 KASSERT (z == uh->alloc, 250 ("UNR inconsistency: chunks %u found %u (line %d)\n", 251 uh->alloc, z, line)); 252 } 253 254 #else 255 256 static __inline void 257 check_unrhdr(struct unrhdr *uh __unused, int line __unused) 258 { 259 260 } 261 262 #endif 263 264 265 /* 266 * Userland memory management. Just use calloc and keep track of how 267 * many elements we have allocated for check_unrhdr(). 268 */ 269 270 static __inline void * 271 new_unr(struct unrhdr *uh, void **p1, void **p2) 272 { 273 void *p; 274 275 uh->alloc++; 276 KASSERT(*p1 != NULL || *p2 != NULL, ("Out of cached memory")); 277 if (*p1 != NULL) { 278 p = *p1; 279 *p1 = NULL; 280 return (p); 281 } else { 282 p = *p2; 283 *p2 = NULL; 284 return (p); 285 } 286 } 287 288 static __inline void 289 delete_unr(struct unrhdr *uh, void *ptr) 290 { 291 struct unr *up; 292 293 uh->alloc--; 294 up = ptr; 295 TAILQ_INSERT_TAIL(&uh->ppfree, up, list); 296 } 297 298 void 299 clean_unrhdrl(struct unrhdr *uh) 300 { 301 struct unr *up; 302 303 mtx_assert(uh->mtx, MA_OWNED); 304 while ((up = TAILQ_FIRST(&uh->ppfree)) != NULL) { 305 TAILQ_REMOVE(&uh->ppfree, up, list); 306 mtx_unlock(uh->mtx); 307 Free(up); 308 mtx_lock(uh->mtx); 309 } 310 311 } 312 313 void 314 clean_unrhdr(struct unrhdr *uh) 315 { 316 317 mtx_lock(uh->mtx); 318 clean_unrhdrl(uh); 319 mtx_unlock(uh->mtx); 320 } 321 322 void 323 init_unrhdr(struct unrhdr *uh, int low, int high, struct mtx *mutex) 324 { 325 326 KASSERT(low >= 0 && low <= high, 327 ("UNR: use error: new_unrhdr(%d, %d)", low, high)); 328 if (mutex != NULL) 329 uh->mtx = mutex; 330 else 331 uh->mtx = &unitmtx; 332 TAILQ_INIT(&uh->head); 333 TAILQ_INIT(&uh->ppfree); 334 uh->low = low; 335 uh->high = high; 336 uh->first = 0; 337 uh->last = 1 + (high - low); 338 check_unrhdr(uh, __LINE__); 339 } 340 341 /* 342 * Allocate a new unrheader set. 343 * 344 * Highest and lowest valid values given as parameters. 345 */ 346 347 struct unrhdr * 348 new_unrhdr(int low, int high, struct mtx *mutex) 349 { 350 struct unrhdr *uh; 351 352 uh = Malloc(sizeof *uh); 353 init_unrhdr(uh, low, high, mutex); 354 return (uh); 355 } 356 357 void 358 delete_unrhdr(struct unrhdr *uh) 359 { 360 361 check_unrhdr(uh, __LINE__); 362 KASSERT(uh->busy == 0, ("unrhdr has %u allocations", uh->busy)); 363 KASSERT(uh->alloc == 0, ("UNR memory leak in delete_unrhdr")); 364 KASSERT(TAILQ_FIRST(&uh->ppfree) == NULL, 365 ("unrhdr has postponed item for free")); 366 Free(uh); 367 } 368 369 void 370 clear_unrhdr(struct unrhdr *uh) 371 { 372 struct unr *up, *uq; 373 374 KASSERT(TAILQ_EMPTY(&uh->ppfree), 375 ("unrhdr has postponed item for free")); 376 up = TAILQ_FIRST(&uh->head); 377 while (up != NULL) { 378 uq = TAILQ_NEXT(up, list); 379 if (up->ptr != uh) { 380 Free(up->ptr); 381 } 382 Free(up); 383 up = uq; 384 } 385 TAILQ_INIT(&uh->head); 386 uh->busy = 0; 387 uh->alloc = 0; 388 } 389 390 static __inline int 391 is_bitmap(struct unrhdr *uh, struct unr *up) 392 { 393 return (up->ptr != uh && up->ptr != NULL); 394 } 395 396 /* 397 * Look for sequence of items which can be combined into a bitmap, if 398 * multiple are present, take the one which saves most memory. 399 * 400 * Return (1) if a sequence was found to indicate that another call 401 * might be able to do more. Return (0) if we found no suitable sequence. 402 * 403 * NB: called from alloc_unr(), no new memory allocation allowed. 404 */ 405 static int 406 optimize_unr(struct unrhdr *uh) 407 { 408 struct unr *up, *uf, *us; 409 struct unrb *ub, *ubf; 410 u_int a, l, ba; 411 412 /* 413 * Look for the run of items (if any) which when collapsed into 414 * a bitmap would save most memory. 415 */ 416 us = NULL; 417 ba = 0; 418 TAILQ_FOREACH(uf, &uh->head, list) { 419 if (uf->len >= NBITS) 420 continue; 421 a = 1; 422 if (is_bitmap(uh, uf)) 423 a++; 424 l = uf->len; 425 up = uf; 426 while (1) { 427 up = TAILQ_NEXT(up, list); 428 if (up == NULL) 429 break; 430 if ((up->len + l) > NBITS) 431 break; 432 a++; 433 if (is_bitmap(uh, up)) 434 a++; 435 l += up->len; 436 } 437 if (a > ba) { 438 ba = a; 439 us = uf; 440 } 441 } 442 if (ba < 3) 443 return (0); 444 445 /* 446 * If the first element is not a bitmap, make it one. 447 * Trying to do so without allocating more memory complicates things 448 * a bit 449 */ 450 if (!is_bitmap(uh, us)) { 451 uf = TAILQ_NEXT(us, list); 452 TAILQ_REMOVE(&uh->head, us, list); 453 a = us->len; 454 l = us->ptr == uh ? 1 : 0; 455 ub = (void *)us; 456 bit_nclear(ub->map, 0, NBITS - 1); 457 if (l) 458 bit_nset(ub->map, 0, a); 459 if (!is_bitmap(uh, uf)) { 460 if (uf->ptr == NULL) 461 bit_nclear(ub->map, a, a + uf->len - 1); 462 else 463 bit_nset(ub->map, a, a + uf->len - 1); 464 uf->ptr = ub; 465 uf->len += a; 466 us = uf; 467 } else { 468 ubf = uf->ptr; 469 for (l = 0; l < uf->len; l++, a++) { 470 if (bit_test(ubf->map, l)) 471 bit_set(ub->map, a); 472 else 473 bit_clear(ub->map, a); 474 } 475 uf->len = a; 476 delete_unr(uh, uf->ptr); 477 uf->ptr = ub; 478 us = uf; 479 } 480 } 481 ub = us->ptr; 482 while (1) { 483 uf = TAILQ_NEXT(us, list); 484 if (uf == NULL) 485 return (1); 486 if (uf->len + us->len > NBITS) 487 return (1); 488 if (uf->ptr == NULL) { 489 bit_nclear(ub->map, us->len, us->len + uf->len - 1); 490 us->len += uf->len; 491 TAILQ_REMOVE(&uh->head, uf, list); 492 delete_unr(uh, uf); 493 } else if (uf->ptr == uh) { 494 bit_nset(ub->map, us->len, us->len + uf->len - 1); 495 us->len += uf->len; 496 TAILQ_REMOVE(&uh->head, uf, list); 497 delete_unr(uh, uf); 498 } else { 499 ubf = uf->ptr; 500 for (l = 0; l < uf->len; l++, us->len++) { 501 if (bit_test(ubf->map, l)) 502 bit_set(ub->map, us->len); 503 else 504 bit_clear(ub->map, us->len); 505 } 506 TAILQ_REMOVE(&uh->head, uf, list); 507 delete_unr(uh, ubf); 508 delete_unr(uh, uf); 509 } 510 } 511 } 512 513 /* 514 * See if a given unr should be collapsed with a neighbor. 515 * 516 * NB: called from alloc_unr(), no new memory allocation allowed. 517 */ 518 static void 519 collapse_unr(struct unrhdr *uh, struct unr *up) 520 { 521 struct unr *upp; 522 struct unrb *ub; 523 524 /* If bitmap is all set or clear, change it to runlength */ 525 if (is_bitmap(uh, up)) { 526 ub = up->ptr; 527 if (ub_full(ub, up->len)) { 528 delete_unr(uh, up->ptr); 529 up->ptr = uh; 530 } else if (ub_empty(ub, up->len)) { 531 delete_unr(uh, up->ptr); 532 up->ptr = NULL; 533 } 534 } 535 536 /* If nothing left in runlength, delete it */ 537 if (up->len == 0) { 538 upp = TAILQ_PREV(up, unrhd, list); 539 if (upp == NULL) 540 upp = TAILQ_NEXT(up, list); 541 TAILQ_REMOVE(&uh->head, up, list); 542 delete_unr(uh, up); 543 up = upp; 544 } 545 546 /* If we have "hot-spot" still, merge with neighbor if possible */ 547 if (up != NULL) { 548 upp = TAILQ_PREV(up, unrhd, list); 549 if (upp != NULL && up->ptr == upp->ptr) { 550 up->len += upp->len; 551 TAILQ_REMOVE(&uh->head, upp, list); 552 delete_unr(uh, upp); 553 } 554 upp = TAILQ_NEXT(up, list); 555 if (upp != NULL && up->ptr == upp->ptr) { 556 up->len += upp->len; 557 TAILQ_REMOVE(&uh->head, upp, list); 558 delete_unr(uh, upp); 559 } 560 } 561 562 /* Merge into ->first if possible */ 563 upp = TAILQ_FIRST(&uh->head); 564 if (upp != NULL && upp->ptr == uh) { 565 uh->first += upp->len; 566 TAILQ_REMOVE(&uh->head, upp, list); 567 delete_unr(uh, upp); 568 if (up == upp) 569 up = NULL; 570 } 571 572 /* Merge into ->last if possible */ 573 upp = TAILQ_LAST(&uh->head, unrhd); 574 if (upp != NULL && upp->ptr == NULL) { 575 uh->last += upp->len; 576 TAILQ_REMOVE(&uh->head, upp, list); 577 delete_unr(uh, upp); 578 if (up == upp) 579 up = NULL; 580 } 581 582 /* Try to make bitmaps */ 583 while (optimize_unr(uh)) 584 continue; 585 } 586 587 /* 588 * Allocate a free unr. 589 */ 590 int 591 alloc_unrl(struct unrhdr *uh) 592 { 593 struct unr *up; 594 struct unrb *ub; 595 u_int x; 596 int y; 597 598 mtx_assert(uh->mtx, MA_OWNED); 599 check_unrhdr(uh, __LINE__); 600 x = uh->low + uh->first; 601 602 up = TAILQ_FIRST(&uh->head); 603 604 /* 605 * If we have an ideal split, just adjust the first+last 606 */ 607 if (up == NULL && uh->last > 0) { 608 uh->first++; 609 uh->last--; 610 uh->busy++; 611 return (x); 612 } 613 614 /* 615 * We can always allocate from the first list element, so if we have 616 * nothing on the list, we must have run out of unit numbers. 617 */ 618 if (up == NULL) 619 return (-1); 620 621 KASSERT(up->ptr != uh, ("UNR first element is allocated")); 622 623 if (up->ptr == NULL) { /* free run */ 624 uh->first++; 625 up->len--; 626 } else { /* bitmap */ 627 ub = up->ptr; 628 bit_ffc(ub->map, up->len, &y); 629 KASSERT(y != -1, ("UNR corruption: No clear bit in bitmap.")); 630 bit_set(ub->map, y); 631 x += y; 632 } 633 uh->busy++; 634 collapse_unr(uh, up); 635 return (x); 636 } 637 638 int 639 alloc_unr(struct unrhdr *uh) 640 { 641 int i; 642 643 mtx_lock(uh->mtx); 644 i = alloc_unrl(uh); 645 clean_unrhdrl(uh); 646 mtx_unlock(uh->mtx); 647 return (i); 648 } 649 650 static int 651 alloc_unr_specificl(struct unrhdr *uh, u_int item, void **p1, void **p2) 652 { 653 struct unr *up, *upn; 654 struct unrb *ub; 655 u_int i, last, tl; 656 657 mtx_assert(uh->mtx, MA_OWNED); 658 659 if (item < uh->low + uh->first || item > uh->high) 660 return (-1); 661 662 up = TAILQ_FIRST(&uh->head); 663 /* Ideal split. */ 664 if (up == NULL && item - uh->low == uh->first) { 665 uh->first++; 666 uh->last--; 667 uh->busy++; 668 check_unrhdr(uh, __LINE__); 669 return (item); 670 } 671 672 i = item - uh->low - uh->first; 673 674 if (up == NULL) { 675 up = new_unr(uh, p1, p2); 676 up->ptr = NULL; 677 up->len = i; 678 TAILQ_INSERT_TAIL(&uh->head, up, list); 679 up = new_unr(uh, p1, p2); 680 up->ptr = uh; 681 up->len = 1; 682 TAILQ_INSERT_TAIL(&uh->head, up, list); 683 uh->last = uh->high - uh->low - i; 684 uh->busy++; 685 check_unrhdr(uh, __LINE__); 686 return (item); 687 } else { 688 /* Find the item which contains the unit we want to allocate. */ 689 TAILQ_FOREACH(up, &uh->head, list) { 690 if (up->len > i) 691 break; 692 i -= up->len; 693 } 694 } 695 696 if (up == NULL) { 697 if (i > 0) { 698 up = new_unr(uh, p1, p2); 699 up->ptr = NULL; 700 up->len = i; 701 TAILQ_INSERT_TAIL(&uh->head, up, list); 702 } 703 up = new_unr(uh, p1, p2); 704 up->ptr = uh; 705 up->len = 1; 706 TAILQ_INSERT_TAIL(&uh->head, up, list); 707 goto done; 708 } 709 710 if (is_bitmap(uh, up)) { 711 ub = up->ptr; 712 if (bit_test(ub->map, i) == 0) { 713 bit_set(ub->map, i); 714 goto done; 715 } else 716 return (-1); 717 } else if (up->ptr == uh) 718 return (-1); 719 720 KASSERT(up->ptr == NULL, 721 ("alloc_unr_specificl: up->ptr != NULL (up=%p)", up)); 722 723 /* Split off the tail end, if any. */ 724 tl = up->len - (1 + i); 725 if (tl > 0) { 726 upn = new_unr(uh, p1, p2); 727 upn->ptr = NULL; 728 upn->len = tl; 729 TAILQ_INSERT_AFTER(&uh->head, up, upn, list); 730 } 731 732 /* Split off head end, if any */ 733 if (i > 0) { 734 upn = new_unr(uh, p1, p2); 735 upn->len = i; 736 upn->ptr = NULL; 737 TAILQ_INSERT_BEFORE(up, upn, list); 738 } 739 up->len = 1; 740 up->ptr = uh; 741 742 done: 743 last = uh->high - uh->low - (item - uh->low); 744 if (uh->last > last) 745 uh->last = last; 746 uh->busy++; 747 collapse_unr(uh, up); 748 check_unrhdr(uh, __LINE__); 749 return (item); 750 } 751 752 int 753 alloc_unr_specific(struct unrhdr *uh, u_int item) 754 { 755 void *p1, *p2; 756 int i; 757 758 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "alloc_unr_specific"); 759 760 p1 = Malloc(sizeof(struct unr)); 761 p2 = Malloc(sizeof(struct unr)); 762 763 mtx_lock(uh->mtx); 764 i = alloc_unr_specificl(uh, item, &p1, &p2); 765 mtx_unlock(uh->mtx); 766 767 if (p1 != NULL) 768 Free(p1); 769 if (p2 != NULL) 770 Free(p2); 771 772 return (i); 773 } 774 775 /* 776 * Free a unr. 777 * 778 * If we can save unrs by using a bitmap, do so. 779 */ 780 static void 781 free_unrl(struct unrhdr *uh, u_int item, void **p1, void **p2) 782 { 783 struct unr *up, *upp, *upn; 784 struct unrb *ub; 785 u_int pl; 786 787 KASSERT(item >= uh->low && item <= uh->high, 788 ("UNR: free_unr(%u) out of range [%u...%u]", 789 item, uh->low, uh->high)); 790 check_unrhdr(uh, __LINE__); 791 item -= uh->low; 792 upp = TAILQ_FIRST(&uh->head); 793 /* 794 * Freeing in the ideal split case 795 */ 796 if (item + 1 == uh->first && upp == NULL) { 797 uh->last++; 798 uh->first--; 799 uh->busy--; 800 check_unrhdr(uh, __LINE__); 801 return; 802 } 803 /* 804 * Freeing in the ->first section. Create a run starting at the 805 * freed item. The code below will subdivide it. 806 */ 807 if (item < uh->first) { 808 up = new_unr(uh, p1, p2); 809 up->ptr = uh; 810 up->len = uh->first - item; 811 TAILQ_INSERT_HEAD(&uh->head, up, list); 812 uh->first -= up->len; 813 } 814 815 item -= uh->first; 816 817 /* Find the item which contains the unit we want to free */ 818 TAILQ_FOREACH(up, &uh->head, list) { 819 if (up->len > item) 820 break; 821 item -= up->len; 822 } 823 824 /* Handle bitmap items */ 825 if (is_bitmap(uh, up)) { 826 ub = up->ptr; 827 828 KASSERT(bit_test(ub->map, item) != 0, 829 ("UNR: Freeing free item %d (bitmap)\n", item)); 830 bit_clear(ub->map, item); 831 uh->busy--; 832 collapse_unr(uh, up); 833 return; 834 } 835 836 KASSERT(up->ptr == uh, ("UNR Freeing free item %d (run))\n", item)); 837 838 /* Just this one left, reap it */ 839 if (up->len == 1) { 840 up->ptr = NULL; 841 uh->busy--; 842 collapse_unr(uh, up); 843 return; 844 } 845 846 /* Check if we can shift the item into the previous 'free' run */ 847 upp = TAILQ_PREV(up, unrhd, list); 848 if (item == 0 && upp != NULL && upp->ptr == NULL) { 849 upp->len++; 850 up->len--; 851 uh->busy--; 852 collapse_unr(uh, up); 853 return; 854 } 855 856 /* Check if we can shift the item to the next 'free' run */ 857 upn = TAILQ_NEXT(up, list); 858 if (item == up->len - 1 && upn != NULL && upn->ptr == NULL) { 859 upn->len++; 860 up->len--; 861 uh->busy--; 862 collapse_unr(uh, up); 863 return; 864 } 865 866 /* Split off the tail end, if any. */ 867 pl = up->len - (1 + item); 868 if (pl > 0) { 869 upp = new_unr(uh, p1, p2); 870 upp->ptr = uh; 871 upp->len = pl; 872 TAILQ_INSERT_AFTER(&uh->head, up, upp, list); 873 } 874 875 /* Split off head end, if any */ 876 if (item > 0) { 877 upp = new_unr(uh, p1, p2); 878 upp->len = item; 879 upp->ptr = uh; 880 TAILQ_INSERT_BEFORE(up, upp, list); 881 } 882 up->len = 1; 883 up->ptr = NULL; 884 uh->busy--; 885 collapse_unr(uh, up); 886 } 887 888 void 889 free_unr(struct unrhdr *uh, u_int item) 890 { 891 void *p1, *p2; 892 893 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "free_unr"); 894 p1 = Malloc(sizeof(struct unr)); 895 p2 = Malloc(sizeof(struct unr)); 896 mtx_lock(uh->mtx); 897 free_unrl(uh, item, &p1, &p2); 898 clean_unrhdrl(uh); 899 mtx_unlock(uh->mtx); 900 if (p1 != NULL) 901 Free(p1); 902 if (p2 != NULL) 903 Free(p2); 904 } 905 906 #ifndef _KERNEL /* USERLAND test driver */ 907 908 /* 909 * Simple stochastic test driver for the above functions. The code resides 910 * here so that it can access static functions and structures. 911 */ 912 913 static bool verbose; 914 #define VPRINTF(...) {if (verbose) printf(__VA_ARGS__);} 915 916 static void 917 print_unr(struct unrhdr *uh, struct unr *up) 918 { 919 u_int x; 920 struct unrb *ub; 921 922 printf(" %p len = %5u ", up, up->len); 923 if (up->ptr == NULL) 924 printf("free\n"); 925 else if (up->ptr == uh) 926 printf("alloc\n"); 927 else { 928 ub = up->ptr; 929 printf("bitmap ["); 930 for (x = 0; x < up->len; x++) { 931 if (bit_test(ub->map, x)) 932 printf("#"); 933 else 934 printf(" "); 935 } 936 printf("]\n"); 937 } 938 } 939 940 static void 941 print_unrhdr(struct unrhdr *uh) 942 { 943 struct unr *up; 944 u_int x; 945 946 printf( 947 "%p low = %u high = %u first = %u last = %u busy %u chunks = %u\n", 948 uh, uh->low, uh->high, uh->first, uh->last, uh->busy, uh->alloc); 949 x = uh->low + uh->first; 950 TAILQ_FOREACH(up, &uh->head, list) { 951 printf(" from = %5u", x); 952 print_unr(uh, up); 953 if (up->ptr == NULL || up->ptr == uh) 954 x += up->len; 955 else 956 x += NBITS; 957 } 958 } 959 960 static void 961 test_alloc_unr(struct unrhdr *uh, u_int i, char a[]) 962 { 963 int j; 964 965 if (a[i]) { 966 VPRINTF("F %u\n", i); 967 free_unr(uh, i); 968 a[i] = 0; 969 } else { 970 no_alloc = 1; 971 j = alloc_unr(uh); 972 if (j != -1) { 973 a[j] = 1; 974 VPRINTF("A %d\n", j); 975 } 976 no_alloc = 0; 977 } 978 } 979 980 static void 981 test_alloc_unr_specific(struct unrhdr *uh, u_int i, char a[]) 982 { 983 int j; 984 985 j = alloc_unr_specific(uh, i); 986 if (j == -1) { 987 VPRINTF("F %u\n", i); 988 a[i] = 0; 989 free_unr(uh, i); 990 } else { 991 a[i] = 1; 992 VPRINTF("A %d\n", j); 993 } 994 } 995 996 static void 997 usage(char** argv) 998 { 999 printf("%s [-h] [-r REPETITIONS] [-v]\n", argv[0]); 1000 } 1001 1002 int 1003 main(int argc, char **argv) 1004 { 1005 struct unrhdr *uh; 1006 char *a; 1007 long count = 10000; /* Number of unrs to test */ 1008 long reps = 1, m; 1009 int ch; 1010 u_int i, j; 1011 1012 verbose = false; 1013 1014 while ((ch = getopt(argc, argv, "hr:v")) != -1) { 1015 switch (ch) { 1016 case 'r': 1017 errno = 0; 1018 reps = strtol(optarg, NULL, 0); 1019 if (errno == ERANGE || errno == EINVAL) { 1020 usage(argv); 1021 exit(2); 1022 } 1023 1024 break; 1025 case 'v': 1026 verbose = true; 1027 break; 1028 case 'h': 1029 default: 1030 usage(argv); 1031 exit(2); 1032 } 1033 1034 1035 } 1036 1037 setbuf(stdout, NULL); 1038 uh = new_unrhdr(0, count - 1, NULL); 1039 print_unrhdr(uh); 1040 1041 a = calloc(count, sizeof(char)); 1042 if (a == NULL) 1043 err(1, "calloc failed"); 1044 srandomdev(); 1045 1046 printf("sizeof(struct unr) %zu\n", sizeof(struct unr)); 1047 printf("sizeof(struct unrb) %zu\n", sizeof(struct unrb)); 1048 printf("sizeof(struct unrhdr) %zu\n", sizeof(struct unrhdr)); 1049 printf("NBITS %lu\n", (unsigned long)NBITS); 1050 for (m = 0; m < count * reps; m++) { 1051 j = random(); 1052 i = (j >> 1) % count; 1053 #if 0 1054 if (a[i] && (j & 1)) 1055 continue; 1056 #endif 1057 if ((random() & 1) != 0) 1058 test_alloc_unr(uh, i, a); 1059 else 1060 test_alloc_unr_specific(uh, i, a); 1061 1062 if (verbose) 1063 print_unrhdr(uh); 1064 check_unrhdr(uh, __LINE__); 1065 } 1066 for (i = 0; i < (u_int)count; i++) { 1067 if (a[i]) { 1068 if (verbose) { 1069 printf("C %u\n", i); 1070 print_unrhdr(uh); 1071 } 1072 free_unr(uh, i); 1073 } 1074 } 1075 print_unrhdr(uh); 1076 delete_unrhdr(uh); 1077 free(a); 1078 return (0); 1079 } 1080 #endif 1081