1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c)2006,2007,2008,2009 YAMAMOTO Takashi, 5 * Copyright (c) 2013 EMC Corp. 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 /* 31 * From: 32 * $NetBSD: vmem_impl.h,v 1.2 2013/01/29 21:26:24 para Exp $ 33 * $NetBSD: subr_vmem.c,v 1.83 2013/03/06 11:20:10 yamt Exp $ 34 */ 35 36 /* 37 * reference: 38 * - Magazines and Vmem: Extending the Slab Allocator 39 * to Many CPUs and Arbitrary Resources 40 * http://www.usenix.org/event/usenix01/bonwick.html 41 */ 42 43 #include <sys/cdefs.h> 44 __FBSDID("$FreeBSD$"); 45 46 #include "opt_ddb.h" 47 48 #include <sys/param.h> 49 #include <sys/systm.h> 50 #include <sys/kernel.h> 51 #include <sys/queue.h> 52 #include <sys/callout.h> 53 #include <sys/hash.h> 54 #include <sys/lock.h> 55 #include <sys/malloc.h> 56 #include <sys/mutex.h> 57 #include <sys/smp.h> 58 #include <sys/condvar.h> 59 #include <sys/sysctl.h> 60 #include <sys/taskqueue.h> 61 #include <sys/vmem.h> 62 #include <sys/vmmeter.h> 63 64 #include "opt_vm.h" 65 66 #include <vm/uma.h> 67 #include <vm/vm.h> 68 #include <vm/pmap.h> 69 #include <vm/vm_map.h> 70 #include <vm/vm_object.h> 71 #include <vm/vm_kern.h> 72 #include <vm/vm_extern.h> 73 #include <vm/vm_param.h> 74 #include <vm/vm_page.h> 75 #include <vm/vm_pageout.h> 76 #include <vm/vm_phys.h> 77 #include <vm/vm_pagequeue.h> 78 #include <vm/uma_int.h> 79 80 int vmem_startup_count(void); 81 82 #define VMEM_OPTORDER 5 83 #define VMEM_OPTVALUE (1 << VMEM_OPTORDER) 84 #define VMEM_MAXORDER \ 85 (VMEM_OPTVALUE - 1 + sizeof(vmem_size_t) * NBBY - VMEM_OPTORDER) 86 87 #define VMEM_HASHSIZE_MIN 16 88 #define VMEM_HASHSIZE_MAX 131072 89 90 #define VMEM_QCACHE_IDX_MAX 16 91 92 #define VMEM_FITMASK (M_BESTFIT | M_FIRSTFIT) 93 94 #define VMEM_FLAGS \ 95 (M_NOWAIT | M_WAITOK | M_USE_RESERVE | M_NOVM | M_BESTFIT | M_FIRSTFIT) 96 97 #define BT_FLAGS (M_NOWAIT | M_WAITOK | M_USE_RESERVE | M_NOVM) 98 99 #define QC_NAME_MAX 16 100 101 /* 102 * Data structures private to vmem. 103 */ 104 MALLOC_DEFINE(M_VMEM, "vmem", "vmem internal structures"); 105 106 typedef struct vmem_btag bt_t; 107 108 TAILQ_HEAD(vmem_seglist, vmem_btag); 109 LIST_HEAD(vmem_freelist, vmem_btag); 110 LIST_HEAD(vmem_hashlist, vmem_btag); 111 112 struct qcache { 113 uma_zone_t qc_cache; 114 vmem_t *qc_vmem; 115 vmem_size_t qc_size; 116 char qc_name[QC_NAME_MAX]; 117 }; 118 typedef struct qcache qcache_t; 119 #define QC_POOL_TO_QCACHE(pool) ((qcache_t *)(pool->pr_qcache)) 120 121 #define VMEM_NAME_MAX 16 122 123 /* vmem arena */ 124 struct vmem { 125 struct mtx_padalign vm_lock; 126 struct cv vm_cv; 127 char vm_name[VMEM_NAME_MAX+1]; 128 LIST_ENTRY(vmem) vm_alllist; 129 struct vmem_hashlist vm_hash0[VMEM_HASHSIZE_MIN]; 130 struct vmem_freelist vm_freelist[VMEM_MAXORDER]; 131 struct vmem_seglist vm_seglist; 132 struct vmem_hashlist *vm_hashlist; 133 vmem_size_t vm_hashsize; 134 135 /* Constant after init */ 136 vmem_size_t vm_qcache_max; 137 vmem_size_t vm_quantum_mask; 138 vmem_size_t vm_import_quantum; 139 int vm_quantum_shift; 140 141 /* Written on alloc/free */ 142 LIST_HEAD(, vmem_btag) vm_freetags; 143 int vm_nfreetags; 144 int vm_nbusytag; 145 vmem_size_t vm_inuse; 146 vmem_size_t vm_size; 147 vmem_size_t vm_limit; 148 149 /* Used on import. */ 150 vmem_import_t *vm_importfn; 151 vmem_release_t *vm_releasefn; 152 void *vm_arg; 153 154 /* Space exhaustion callback. */ 155 vmem_reclaim_t *vm_reclaimfn; 156 157 /* quantum cache */ 158 qcache_t vm_qcache[VMEM_QCACHE_IDX_MAX]; 159 }; 160 161 /* boundary tag */ 162 struct vmem_btag { 163 TAILQ_ENTRY(vmem_btag) bt_seglist; 164 union { 165 LIST_ENTRY(vmem_btag) u_freelist; /* BT_TYPE_FREE */ 166 LIST_ENTRY(vmem_btag) u_hashlist; /* BT_TYPE_BUSY */ 167 } bt_u; 168 #define bt_hashlist bt_u.u_hashlist 169 #define bt_freelist bt_u.u_freelist 170 vmem_addr_t bt_start; 171 vmem_size_t bt_size; 172 int bt_type; 173 }; 174 175 #define BT_TYPE_SPAN 1 /* Allocated from importfn */ 176 #define BT_TYPE_SPAN_STATIC 2 /* vmem_add() or create. */ 177 #define BT_TYPE_FREE 3 /* Available space. */ 178 #define BT_TYPE_BUSY 4 /* Used space. */ 179 #define BT_ISSPAN_P(bt) ((bt)->bt_type <= BT_TYPE_SPAN_STATIC) 180 181 #define BT_END(bt) ((bt)->bt_start + (bt)->bt_size - 1) 182 183 #if defined(DIAGNOSTIC) 184 static int enable_vmem_check = 1; 185 SYSCTL_INT(_debug, OID_AUTO, vmem_check, CTLFLAG_RWTUN, 186 &enable_vmem_check, 0, "Enable vmem check"); 187 static void vmem_check(vmem_t *); 188 #endif 189 190 static struct callout vmem_periodic_ch; 191 static int vmem_periodic_interval; 192 static struct task vmem_periodic_wk; 193 194 static struct mtx_padalign __exclusive_cache_line vmem_list_lock; 195 static LIST_HEAD(, vmem) vmem_list = LIST_HEAD_INITIALIZER(vmem_list); 196 static uma_zone_t vmem_zone; 197 198 /* ---- misc */ 199 #define VMEM_CONDVAR_INIT(vm, wchan) cv_init(&vm->vm_cv, wchan) 200 #define VMEM_CONDVAR_DESTROY(vm) cv_destroy(&vm->vm_cv) 201 #define VMEM_CONDVAR_WAIT(vm) cv_wait(&vm->vm_cv, &vm->vm_lock) 202 #define VMEM_CONDVAR_BROADCAST(vm) cv_broadcast(&vm->vm_cv) 203 204 205 #define VMEM_LOCK(vm) mtx_lock(&vm->vm_lock) 206 #define VMEM_TRYLOCK(vm) mtx_trylock(&vm->vm_lock) 207 #define VMEM_UNLOCK(vm) mtx_unlock(&vm->vm_lock) 208 #define VMEM_LOCK_INIT(vm, name) mtx_init(&vm->vm_lock, (name), NULL, MTX_DEF) 209 #define VMEM_LOCK_DESTROY(vm) mtx_destroy(&vm->vm_lock) 210 #define VMEM_ASSERT_LOCKED(vm) mtx_assert(&vm->vm_lock, MA_OWNED); 211 212 #define VMEM_ALIGNUP(addr, align) (-(-(addr) & -(align))) 213 214 #define VMEM_CROSS_P(addr1, addr2, boundary) \ 215 ((((addr1) ^ (addr2)) & -(boundary)) != 0) 216 217 #define ORDER2SIZE(order) ((order) < VMEM_OPTVALUE ? ((order) + 1) : \ 218 (vmem_size_t)1 << ((order) - (VMEM_OPTVALUE - VMEM_OPTORDER - 1))) 219 #define SIZE2ORDER(size) ((size) <= VMEM_OPTVALUE ? ((size) - 1) : \ 220 (flsl(size) + (VMEM_OPTVALUE - VMEM_OPTORDER - 2))) 221 222 /* 223 * Maximum number of boundary tags that may be required to satisfy an 224 * allocation. Two may be required to import. Another two may be 225 * required to clip edges. 226 */ 227 #define BT_MAXALLOC 4 228 229 /* 230 * Max free limits the number of locally cached boundary tags. We 231 * just want to avoid hitting the zone allocator for every call. 232 */ 233 #define BT_MAXFREE (BT_MAXALLOC * 8) 234 235 /* Allocator for boundary tags. */ 236 static uma_zone_t vmem_bt_zone; 237 238 /* boot time arena storage. */ 239 static struct vmem kernel_arena_storage; 240 static struct vmem buffer_arena_storage; 241 static struct vmem transient_arena_storage; 242 /* kernel and kmem arenas are aliased for backwards KPI compat. */ 243 vmem_t *kernel_arena = &kernel_arena_storage; 244 vmem_t *kmem_arena = &kernel_arena_storage; 245 vmem_t *buffer_arena = &buffer_arena_storage; 246 vmem_t *transient_arena = &transient_arena_storage; 247 248 #ifdef DEBUG_MEMGUARD 249 static struct vmem memguard_arena_storage; 250 vmem_t *memguard_arena = &memguard_arena_storage; 251 #endif 252 253 /* 254 * Fill the vmem's boundary tag cache. We guarantee that boundary tag 255 * allocation will not fail once bt_fill() passes. To do so we cache 256 * at least the maximum possible tag allocations in the arena. 257 */ 258 static int 259 bt_fill(vmem_t *vm, int flags) 260 { 261 bt_t *bt; 262 263 VMEM_ASSERT_LOCKED(vm); 264 265 /* 266 * Only allow the kernel arena and arenas derived from kernel arena to 267 * dip into reserve tags. They are where new tags come from. 268 */ 269 flags &= BT_FLAGS; 270 if (vm != kernel_arena && vm->vm_arg != kernel_arena) 271 flags &= ~M_USE_RESERVE; 272 273 /* 274 * Loop until we meet the reserve. To minimize the lock shuffle 275 * and prevent simultaneous fills we first try a NOWAIT regardless 276 * of the caller's flags. Specify M_NOVM so we don't recurse while 277 * holding a vmem lock. 278 */ 279 while (vm->vm_nfreetags < BT_MAXALLOC) { 280 bt = uma_zalloc(vmem_bt_zone, 281 (flags & M_USE_RESERVE) | M_NOWAIT | M_NOVM); 282 if (bt == NULL) { 283 VMEM_UNLOCK(vm); 284 bt = uma_zalloc(vmem_bt_zone, flags); 285 VMEM_LOCK(vm); 286 if (bt == NULL) 287 break; 288 } 289 LIST_INSERT_HEAD(&vm->vm_freetags, bt, bt_freelist); 290 vm->vm_nfreetags++; 291 } 292 293 if (vm->vm_nfreetags < BT_MAXALLOC) 294 return ENOMEM; 295 296 return 0; 297 } 298 299 /* 300 * Pop a tag off of the freetag stack. 301 */ 302 static bt_t * 303 bt_alloc(vmem_t *vm) 304 { 305 bt_t *bt; 306 307 VMEM_ASSERT_LOCKED(vm); 308 bt = LIST_FIRST(&vm->vm_freetags); 309 MPASS(bt != NULL); 310 LIST_REMOVE(bt, bt_freelist); 311 vm->vm_nfreetags--; 312 313 return bt; 314 } 315 316 /* 317 * Trim the per-vmem free list. Returns with the lock released to 318 * avoid allocator recursions. 319 */ 320 static void 321 bt_freetrim(vmem_t *vm, int freelimit) 322 { 323 LIST_HEAD(, vmem_btag) freetags; 324 bt_t *bt; 325 326 LIST_INIT(&freetags); 327 VMEM_ASSERT_LOCKED(vm); 328 while (vm->vm_nfreetags > freelimit) { 329 bt = LIST_FIRST(&vm->vm_freetags); 330 LIST_REMOVE(bt, bt_freelist); 331 vm->vm_nfreetags--; 332 LIST_INSERT_HEAD(&freetags, bt, bt_freelist); 333 } 334 VMEM_UNLOCK(vm); 335 while ((bt = LIST_FIRST(&freetags)) != NULL) { 336 LIST_REMOVE(bt, bt_freelist); 337 uma_zfree(vmem_bt_zone, bt); 338 } 339 } 340 341 static inline void 342 bt_free(vmem_t *vm, bt_t *bt) 343 { 344 345 VMEM_ASSERT_LOCKED(vm); 346 MPASS(LIST_FIRST(&vm->vm_freetags) != bt); 347 LIST_INSERT_HEAD(&vm->vm_freetags, bt, bt_freelist); 348 vm->vm_nfreetags++; 349 } 350 351 /* 352 * freelist[0] ... [1, 1] 353 * freelist[1] ... [2, 2] 354 * : 355 * freelist[29] ... [30, 30] 356 * freelist[30] ... [31, 31] 357 * freelist[31] ... [32, 63] 358 * freelist[33] ... [64, 127] 359 * : 360 * freelist[n] ... [(1 << (n - 26)), (1 << (n - 25)) - 1] 361 * : 362 */ 363 364 static struct vmem_freelist * 365 bt_freehead_tofree(vmem_t *vm, vmem_size_t size) 366 { 367 const vmem_size_t qsize = size >> vm->vm_quantum_shift; 368 const int idx = SIZE2ORDER(qsize); 369 370 MPASS(size != 0 && qsize != 0); 371 MPASS((size & vm->vm_quantum_mask) == 0); 372 MPASS(idx >= 0); 373 MPASS(idx < VMEM_MAXORDER); 374 375 return &vm->vm_freelist[idx]; 376 } 377 378 /* 379 * bt_freehead_toalloc: return the freelist for the given size and allocation 380 * strategy. 381 * 382 * For M_FIRSTFIT, return the list in which any blocks are large enough 383 * for the requested size. otherwise, return the list which can have blocks 384 * large enough for the requested size. 385 */ 386 static struct vmem_freelist * 387 bt_freehead_toalloc(vmem_t *vm, vmem_size_t size, int strat) 388 { 389 const vmem_size_t qsize = size >> vm->vm_quantum_shift; 390 int idx = SIZE2ORDER(qsize); 391 392 MPASS(size != 0 && qsize != 0); 393 MPASS((size & vm->vm_quantum_mask) == 0); 394 395 if (strat == M_FIRSTFIT && ORDER2SIZE(idx) != qsize) { 396 idx++; 397 /* check too large request? */ 398 } 399 MPASS(idx >= 0); 400 MPASS(idx < VMEM_MAXORDER); 401 402 return &vm->vm_freelist[idx]; 403 } 404 405 /* ---- boundary tag hash */ 406 407 static struct vmem_hashlist * 408 bt_hashhead(vmem_t *vm, vmem_addr_t addr) 409 { 410 struct vmem_hashlist *list; 411 unsigned int hash; 412 413 hash = hash32_buf(&addr, sizeof(addr), 0); 414 list = &vm->vm_hashlist[hash % vm->vm_hashsize]; 415 416 return list; 417 } 418 419 static bt_t * 420 bt_lookupbusy(vmem_t *vm, vmem_addr_t addr) 421 { 422 struct vmem_hashlist *list; 423 bt_t *bt; 424 425 VMEM_ASSERT_LOCKED(vm); 426 list = bt_hashhead(vm, addr); 427 LIST_FOREACH(bt, list, bt_hashlist) { 428 if (bt->bt_start == addr) { 429 break; 430 } 431 } 432 433 return bt; 434 } 435 436 static void 437 bt_rembusy(vmem_t *vm, bt_t *bt) 438 { 439 440 VMEM_ASSERT_LOCKED(vm); 441 MPASS(vm->vm_nbusytag > 0); 442 vm->vm_inuse -= bt->bt_size; 443 vm->vm_nbusytag--; 444 LIST_REMOVE(bt, bt_hashlist); 445 } 446 447 static void 448 bt_insbusy(vmem_t *vm, bt_t *bt) 449 { 450 struct vmem_hashlist *list; 451 452 VMEM_ASSERT_LOCKED(vm); 453 MPASS(bt->bt_type == BT_TYPE_BUSY); 454 455 list = bt_hashhead(vm, bt->bt_start); 456 LIST_INSERT_HEAD(list, bt, bt_hashlist); 457 vm->vm_nbusytag++; 458 vm->vm_inuse += bt->bt_size; 459 } 460 461 /* ---- boundary tag list */ 462 463 static void 464 bt_remseg(vmem_t *vm, bt_t *bt) 465 { 466 467 TAILQ_REMOVE(&vm->vm_seglist, bt, bt_seglist); 468 bt_free(vm, bt); 469 } 470 471 static void 472 bt_insseg(vmem_t *vm, bt_t *bt, bt_t *prev) 473 { 474 475 TAILQ_INSERT_AFTER(&vm->vm_seglist, prev, bt, bt_seglist); 476 } 477 478 static void 479 bt_insseg_tail(vmem_t *vm, bt_t *bt) 480 { 481 482 TAILQ_INSERT_TAIL(&vm->vm_seglist, bt, bt_seglist); 483 } 484 485 static void 486 bt_remfree(vmem_t *vm, bt_t *bt) 487 { 488 489 MPASS(bt->bt_type == BT_TYPE_FREE); 490 491 LIST_REMOVE(bt, bt_freelist); 492 } 493 494 static void 495 bt_insfree(vmem_t *vm, bt_t *bt) 496 { 497 struct vmem_freelist *list; 498 499 list = bt_freehead_tofree(vm, bt->bt_size); 500 LIST_INSERT_HEAD(list, bt, bt_freelist); 501 } 502 503 /* ---- vmem internal functions */ 504 505 /* 506 * Import from the arena into the quantum cache in UMA. 507 * 508 * We use VMEM_ADDR_QCACHE_MIN instead of 0: uma_zalloc() returns 0 to indicate 509 * failure, so UMA can't be used to cache a resource with value 0. 510 */ 511 static int 512 qc_import(void *arg, void **store, int cnt, int domain, int flags) 513 { 514 qcache_t *qc; 515 vmem_addr_t addr; 516 int i; 517 518 KASSERT((flags & M_WAITOK) == 0, ("blocking allocation")); 519 520 qc = arg; 521 for (i = 0; i < cnt; i++) { 522 if (vmem_xalloc(qc->qc_vmem, qc->qc_size, 0, 0, 0, 523 VMEM_ADDR_QCACHE_MIN, VMEM_ADDR_MAX, flags, &addr) != 0) 524 break; 525 store[i] = (void *)addr; 526 } 527 return (i); 528 } 529 530 /* 531 * Release memory from the UMA cache to the arena. 532 */ 533 static void 534 qc_release(void *arg, void **store, int cnt) 535 { 536 qcache_t *qc; 537 int i; 538 539 qc = arg; 540 for (i = 0; i < cnt; i++) 541 vmem_xfree(qc->qc_vmem, (vmem_addr_t)store[i], qc->qc_size); 542 } 543 544 static void 545 qc_init(vmem_t *vm, vmem_size_t qcache_max) 546 { 547 qcache_t *qc; 548 vmem_size_t size; 549 int qcache_idx_max; 550 int i; 551 552 MPASS((qcache_max & vm->vm_quantum_mask) == 0); 553 qcache_idx_max = MIN(qcache_max >> vm->vm_quantum_shift, 554 VMEM_QCACHE_IDX_MAX); 555 vm->vm_qcache_max = qcache_idx_max << vm->vm_quantum_shift; 556 for (i = 0; i < qcache_idx_max; i++) { 557 qc = &vm->vm_qcache[i]; 558 size = (i + 1) << vm->vm_quantum_shift; 559 snprintf(qc->qc_name, sizeof(qc->qc_name), "%s-%zu", 560 vm->vm_name, size); 561 qc->qc_vmem = vm; 562 qc->qc_size = size; 563 qc->qc_cache = uma_zcache_create(qc->qc_name, size, 564 NULL, NULL, NULL, NULL, qc_import, qc_release, qc, 565 UMA_ZONE_VM); 566 MPASS(qc->qc_cache); 567 } 568 } 569 570 static void 571 qc_destroy(vmem_t *vm) 572 { 573 int qcache_idx_max; 574 int i; 575 576 qcache_idx_max = vm->vm_qcache_max >> vm->vm_quantum_shift; 577 for (i = 0; i < qcache_idx_max; i++) 578 uma_zdestroy(vm->vm_qcache[i].qc_cache); 579 } 580 581 static void 582 qc_drain(vmem_t *vm) 583 { 584 int qcache_idx_max; 585 int i; 586 587 qcache_idx_max = vm->vm_qcache_max >> vm->vm_quantum_shift; 588 for (i = 0; i < qcache_idx_max; i++) 589 zone_drain(vm->vm_qcache[i].qc_cache); 590 } 591 592 #ifndef UMA_MD_SMALL_ALLOC 593 594 static struct mtx_padalign __exclusive_cache_line vmem_bt_lock; 595 596 /* 597 * vmem_bt_alloc: Allocate a new page of boundary tags. 598 * 599 * On architectures with uma_small_alloc there is no recursion; no address 600 * space need be allocated to allocate boundary tags. For the others, we 601 * must handle recursion. Boundary tags are necessary to allocate new 602 * boundary tags. 603 * 604 * UMA guarantees that enough tags are held in reserve to allocate a new 605 * page of kva. We dip into this reserve by specifying M_USE_RESERVE only 606 * when allocating the page to hold new boundary tags. In this way the 607 * reserve is automatically filled by the allocation that uses the reserve. 608 * 609 * We still have to guarantee that the new tags are allocated atomically since 610 * many threads may try concurrently. The bt_lock provides this guarantee. 611 * We convert WAITOK allocations to NOWAIT and then handle the blocking here 612 * on failure. It's ok to return NULL for a WAITOK allocation as UMA will 613 * loop again after checking to see if we lost the race to allocate. 614 * 615 * There is a small race between vmem_bt_alloc() returning the page and the 616 * zone lock being acquired to add the page to the zone. For WAITOK 617 * allocations we just pause briefly. NOWAIT may experience a transient 618 * failure. To alleviate this we permit a small number of simultaneous 619 * fills to proceed concurrently so NOWAIT is less likely to fail unless 620 * we are really out of KVA. 621 */ 622 static void * 623 vmem_bt_alloc(uma_zone_t zone, vm_size_t bytes, int domain, uint8_t *pflag, 624 int wait) 625 { 626 vmem_addr_t addr; 627 628 *pflag = UMA_SLAB_KERNEL; 629 630 /* 631 * Single thread boundary tag allocation so that the address space 632 * and memory are added in one atomic operation. 633 */ 634 mtx_lock(&vmem_bt_lock); 635 if (vmem_xalloc(vm_dom[domain].vmd_kernel_arena, bytes, 0, 0, 0, 636 VMEM_ADDR_MIN, VMEM_ADDR_MAX, 637 M_NOWAIT | M_NOVM | M_USE_RESERVE | M_BESTFIT, &addr) == 0) { 638 if (kmem_back_domain(domain, kernel_object, addr, bytes, 639 M_NOWAIT | M_USE_RESERVE) == 0) { 640 mtx_unlock(&vmem_bt_lock); 641 return ((void *)addr); 642 } 643 vmem_xfree(vm_dom[domain].vmd_kernel_arena, addr, bytes); 644 mtx_unlock(&vmem_bt_lock); 645 /* 646 * Out of memory, not address space. This may not even be 647 * possible due to M_USE_RESERVE page allocation. 648 */ 649 if (wait & M_WAITOK) 650 vm_wait_domain(domain); 651 return (NULL); 652 } 653 mtx_unlock(&vmem_bt_lock); 654 /* 655 * We're either out of address space or lost a fill race. 656 */ 657 if (wait & M_WAITOK) 658 pause("btalloc", 1); 659 660 return (NULL); 661 } 662 663 /* 664 * How many pages do we need to startup_alloc. 665 */ 666 int 667 vmem_startup_count(void) 668 { 669 670 return (howmany(BT_MAXALLOC, 671 UMA_SLAB_SPACE / sizeof(struct vmem_btag))); 672 } 673 #endif 674 675 void 676 vmem_startup(void) 677 { 678 679 mtx_init(&vmem_list_lock, "vmem list lock", NULL, MTX_DEF); 680 vmem_zone = uma_zcreate("vmem", 681 sizeof(struct vmem), NULL, NULL, NULL, NULL, 682 UMA_ALIGN_PTR, UMA_ZONE_VM); 683 vmem_bt_zone = uma_zcreate("vmem btag", 684 sizeof(struct vmem_btag), NULL, NULL, NULL, NULL, 685 UMA_ALIGN_PTR, UMA_ZONE_VM | UMA_ZONE_NOFREE); 686 #ifndef UMA_MD_SMALL_ALLOC 687 mtx_init(&vmem_bt_lock, "btag lock", NULL, MTX_DEF); 688 uma_prealloc(vmem_bt_zone, BT_MAXALLOC); 689 /* 690 * Reserve enough tags to allocate new tags. We allow multiple 691 * CPUs to attempt to allocate new tags concurrently to limit 692 * false restarts in UMA. vmem_bt_alloc() allocates from a per-domain 693 * arena, which may involve importing a range from the kernel arena, 694 * so we need to keep at least 2 * BT_MAXALLOC tags reserved. 695 */ 696 uma_zone_reserve(vmem_bt_zone, 2 * BT_MAXALLOC * mp_ncpus); 697 uma_zone_set_allocf(vmem_bt_zone, vmem_bt_alloc); 698 #endif 699 } 700 701 /* ---- rehash */ 702 703 static int 704 vmem_rehash(vmem_t *vm, vmem_size_t newhashsize) 705 { 706 bt_t *bt; 707 int i; 708 struct vmem_hashlist *newhashlist; 709 struct vmem_hashlist *oldhashlist; 710 vmem_size_t oldhashsize; 711 712 MPASS(newhashsize > 0); 713 714 newhashlist = malloc(sizeof(struct vmem_hashlist) * newhashsize, 715 M_VMEM, M_NOWAIT); 716 if (newhashlist == NULL) 717 return ENOMEM; 718 for (i = 0; i < newhashsize; i++) { 719 LIST_INIT(&newhashlist[i]); 720 } 721 722 VMEM_LOCK(vm); 723 oldhashlist = vm->vm_hashlist; 724 oldhashsize = vm->vm_hashsize; 725 vm->vm_hashlist = newhashlist; 726 vm->vm_hashsize = newhashsize; 727 if (oldhashlist == NULL) { 728 VMEM_UNLOCK(vm); 729 return 0; 730 } 731 for (i = 0; i < oldhashsize; i++) { 732 while ((bt = LIST_FIRST(&oldhashlist[i])) != NULL) { 733 bt_rembusy(vm, bt); 734 bt_insbusy(vm, bt); 735 } 736 } 737 VMEM_UNLOCK(vm); 738 739 if (oldhashlist != vm->vm_hash0) { 740 free(oldhashlist, M_VMEM); 741 } 742 743 return 0; 744 } 745 746 static void 747 vmem_periodic_kick(void *dummy) 748 { 749 750 taskqueue_enqueue(taskqueue_thread, &vmem_periodic_wk); 751 } 752 753 static void 754 vmem_periodic(void *unused, int pending) 755 { 756 vmem_t *vm; 757 vmem_size_t desired; 758 vmem_size_t current; 759 760 mtx_lock(&vmem_list_lock); 761 LIST_FOREACH(vm, &vmem_list, vm_alllist) { 762 #ifdef DIAGNOSTIC 763 /* Convenient time to verify vmem state. */ 764 if (enable_vmem_check == 1) { 765 VMEM_LOCK(vm); 766 vmem_check(vm); 767 VMEM_UNLOCK(vm); 768 } 769 #endif 770 desired = 1 << flsl(vm->vm_nbusytag); 771 desired = MIN(MAX(desired, VMEM_HASHSIZE_MIN), 772 VMEM_HASHSIZE_MAX); 773 current = vm->vm_hashsize; 774 775 /* Grow in powers of two. Shrink less aggressively. */ 776 if (desired >= current * 2 || desired * 4 <= current) 777 vmem_rehash(vm, desired); 778 779 /* 780 * Periodically wake up threads waiting for resources, 781 * so they could ask for reclamation again. 782 */ 783 VMEM_CONDVAR_BROADCAST(vm); 784 } 785 mtx_unlock(&vmem_list_lock); 786 787 callout_reset(&vmem_periodic_ch, vmem_periodic_interval, 788 vmem_periodic_kick, NULL); 789 } 790 791 static void 792 vmem_start_callout(void *unused) 793 { 794 795 TASK_INIT(&vmem_periodic_wk, 0, vmem_periodic, NULL); 796 vmem_periodic_interval = hz * 10; 797 callout_init(&vmem_periodic_ch, 1); 798 callout_reset(&vmem_periodic_ch, vmem_periodic_interval, 799 vmem_periodic_kick, NULL); 800 } 801 SYSINIT(vfs, SI_SUB_CONFIGURE, SI_ORDER_ANY, vmem_start_callout, NULL); 802 803 static void 804 vmem_add1(vmem_t *vm, vmem_addr_t addr, vmem_size_t size, int type) 805 { 806 bt_t *btspan; 807 bt_t *btfree; 808 809 MPASS(type == BT_TYPE_SPAN || type == BT_TYPE_SPAN_STATIC); 810 MPASS((size & vm->vm_quantum_mask) == 0); 811 812 btspan = bt_alloc(vm); 813 btspan->bt_type = type; 814 btspan->bt_start = addr; 815 btspan->bt_size = size; 816 bt_insseg_tail(vm, btspan); 817 818 btfree = bt_alloc(vm); 819 btfree->bt_type = BT_TYPE_FREE; 820 btfree->bt_start = addr; 821 btfree->bt_size = size; 822 bt_insseg(vm, btfree, btspan); 823 bt_insfree(vm, btfree); 824 825 vm->vm_size += size; 826 } 827 828 static void 829 vmem_destroy1(vmem_t *vm) 830 { 831 bt_t *bt; 832 833 /* 834 * Drain per-cpu quantum caches. 835 */ 836 qc_destroy(vm); 837 838 /* 839 * The vmem should now only contain empty segments. 840 */ 841 VMEM_LOCK(vm); 842 MPASS(vm->vm_nbusytag == 0); 843 844 while ((bt = TAILQ_FIRST(&vm->vm_seglist)) != NULL) 845 bt_remseg(vm, bt); 846 847 if (vm->vm_hashlist != NULL && vm->vm_hashlist != vm->vm_hash0) 848 free(vm->vm_hashlist, M_VMEM); 849 850 bt_freetrim(vm, 0); 851 852 VMEM_CONDVAR_DESTROY(vm); 853 VMEM_LOCK_DESTROY(vm); 854 uma_zfree(vmem_zone, vm); 855 } 856 857 static int 858 vmem_import(vmem_t *vm, vmem_size_t size, vmem_size_t align, int flags) 859 { 860 vmem_addr_t addr; 861 int error; 862 863 if (vm->vm_importfn == NULL) 864 return (EINVAL); 865 866 /* 867 * To make sure we get a span that meets the alignment we double it 868 * and add the size to the tail. This slightly overestimates. 869 */ 870 if (align != vm->vm_quantum_mask + 1) 871 size = (align * 2) + size; 872 size = roundup(size, vm->vm_import_quantum); 873 874 if (vm->vm_limit != 0 && vm->vm_limit < vm->vm_size + size) 875 return (ENOMEM); 876 877 /* 878 * Hide MAXALLOC tags so we're guaranteed to be able to add this 879 * span and the tag we want to allocate from it. 880 */ 881 MPASS(vm->vm_nfreetags >= BT_MAXALLOC); 882 vm->vm_nfreetags -= BT_MAXALLOC; 883 VMEM_UNLOCK(vm); 884 error = (vm->vm_importfn)(vm->vm_arg, size, flags, &addr); 885 VMEM_LOCK(vm); 886 vm->vm_nfreetags += BT_MAXALLOC; 887 if (error) 888 return (ENOMEM); 889 890 vmem_add1(vm, addr, size, BT_TYPE_SPAN); 891 892 return 0; 893 } 894 895 /* 896 * vmem_fit: check if a bt can satisfy the given restrictions. 897 * 898 * it's a caller's responsibility to ensure the region is big enough 899 * before calling us. 900 */ 901 static int 902 vmem_fit(const bt_t *bt, vmem_size_t size, vmem_size_t align, 903 vmem_size_t phase, vmem_size_t nocross, vmem_addr_t minaddr, 904 vmem_addr_t maxaddr, vmem_addr_t *addrp) 905 { 906 vmem_addr_t start; 907 vmem_addr_t end; 908 909 MPASS(size > 0); 910 MPASS(bt->bt_size >= size); /* caller's responsibility */ 911 912 /* 913 * XXX assumption: vmem_addr_t and vmem_size_t are 914 * unsigned integer of the same size. 915 */ 916 917 start = bt->bt_start; 918 if (start < minaddr) { 919 start = minaddr; 920 } 921 end = BT_END(bt); 922 if (end > maxaddr) 923 end = maxaddr; 924 if (start > end) 925 return (ENOMEM); 926 927 start = VMEM_ALIGNUP(start - phase, align) + phase; 928 if (start < bt->bt_start) 929 start += align; 930 if (VMEM_CROSS_P(start, start + size - 1, nocross)) { 931 MPASS(align < nocross); 932 start = VMEM_ALIGNUP(start - phase, nocross) + phase; 933 } 934 if (start <= end && end - start >= size - 1) { 935 MPASS((start & (align - 1)) == phase); 936 MPASS(!VMEM_CROSS_P(start, start + size - 1, nocross)); 937 MPASS(minaddr <= start); 938 MPASS(maxaddr == 0 || start + size - 1 <= maxaddr); 939 MPASS(bt->bt_start <= start); 940 MPASS(BT_END(bt) - start >= size - 1); 941 *addrp = start; 942 943 return (0); 944 } 945 return (ENOMEM); 946 } 947 948 /* 949 * vmem_clip: Trim the boundary tag edges to the requested start and size. 950 */ 951 static void 952 vmem_clip(vmem_t *vm, bt_t *bt, vmem_addr_t start, vmem_size_t size) 953 { 954 bt_t *btnew; 955 bt_t *btprev; 956 957 VMEM_ASSERT_LOCKED(vm); 958 MPASS(bt->bt_type == BT_TYPE_FREE); 959 MPASS(bt->bt_size >= size); 960 bt_remfree(vm, bt); 961 if (bt->bt_start != start) { 962 btprev = bt_alloc(vm); 963 btprev->bt_type = BT_TYPE_FREE; 964 btprev->bt_start = bt->bt_start; 965 btprev->bt_size = start - bt->bt_start; 966 bt->bt_start = start; 967 bt->bt_size -= btprev->bt_size; 968 bt_insfree(vm, btprev); 969 bt_insseg(vm, btprev, 970 TAILQ_PREV(bt, vmem_seglist, bt_seglist)); 971 } 972 MPASS(bt->bt_start == start); 973 if (bt->bt_size != size && bt->bt_size - size > vm->vm_quantum_mask) { 974 /* split */ 975 btnew = bt_alloc(vm); 976 btnew->bt_type = BT_TYPE_BUSY; 977 btnew->bt_start = bt->bt_start; 978 btnew->bt_size = size; 979 bt->bt_start = bt->bt_start + size; 980 bt->bt_size -= size; 981 bt_insfree(vm, bt); 982 bt_insseg(vm, btnew, 983 TAILQ_PREV(bt, vmem_seglist, bt_seglist)); 984 bt_insbusy(vm, btnew); 985 bt = btnew; 986 } else { 987 bt->bt_type = BT_TYPE_BUSY; 988 bt_insbusy(vm, bt); 989 } 990 MPASS(bt->bt_size >= size); 991 } 992 993 /* ---- vmem API */ 994 995 void 996 vmem_set_import(vmem_t *vm, vmem_import_t *importfn, 997 vmem_release_t *releasefn, void *arg, vmem_size_t import_quantum) 998 { 999 1000 VMEM_LOCK(vm); 1001 vm->vm_importfn = importfn; 1002 vm->vm_releasefn = releasefn; 1003 vm->vm_arg = arg; 1004 vm->vm_import_quantum = import_quantum; 1005 VMEM_UNLOCK(vm); 1006 } 1007 1008 void 1009 vmem_set_limit(vmem_t *vm, vmem_size_t limit) 1010 { 1011 1012 VMEM_LOCK(vm); 1013 vm->vm_limit = limit; 1014 VMEM_UNLOCK(vm); 1015 } 1016 1017 void 1018 vmem_set_reclaim(vmem_t *vm, vmem_reclaim_t *reclaimfn) 1019 { 1020 1021 VMEM_LOCK(vm); 1022 vm->vm_reclaimfn = reclaimfn; 1023 VMEM_UNLOCK(vm); 1024 } 1025 1026 /* 1027 * vmem_init: Initializes vmem arena. 1028 */ 1029 vmem_t * 1030 vmem_init(vmem_t *vm, const char *name, vmem_addr_t base, vmem_size_t size, 1031 vmem_size_t quantum, vmem_size_t qcache_max, int flags) 1032 { 1033 int i; 1034 1035 MPASS(quantum > 0); 1036 MPASS((quantum & (quantum - 1)) == 0); 1037 1038 bzero(vm, sizeof(*vm)); 1039 1040 VMEM_CONDVAR_INIT(vm, name); 1041 VMEM_LOCK_INIT(vm, name); 1042 vm->vm_nfreetags = 0; 1043 LIST_INIT(&vm->vm_freetags); 1044 strlcpy(vm->vm_name, name, sizeof(vm->vm_name)); 1045 vm->vm_quantum_mask = quantum - 1; 1046 vm->vm_quantum_shift = flsl(quantum) - 1; 1047 vm->vm_nbusytag = 0; 1048 vm->vm_size = 0; 1049 vm->vm_limit = 0; 1050 vm->vm_inuse = 0; 1051 qc_init(vm, qcache_max); 1052 1053 TAILQ_INIT(&vm->vm_seglist); 1054 for (i = 0; i < VMEM_MAXORDER; i++) { 1055 LIST_INIT(&vm->vm_freelist[i]); 1056 } 1057 memset(&vm->vm_hash0, 0, sizeof(vm->vm_hash0)); 1058 vm->vm_hashsize = VMEM_HASHSIZE_MIN; 1059 vm->vm_hashlist = vm->vm_hash0; 1060 1061 if (size != 0) { 1062 if (vmem_add(vm, base, size, flags) != 0) { 1063 vmem_destroy1(vm); 1064 return NULL; 1065 } 1066 } 1067 1068 mtx_lock(&vmem_list_lock); 1069 LIST_INSERT_HEAD(&vmem_list, vm, vm_alllist); 1070 mtx_unlock(&vmem_list_lock); 1071 1072 return vm; 1073 } 1074 1075 /* 1076 * vmem_create: create an arena. 1077 */ 1078 vmem_t * 1079 vmem_create(const char *name, vmem_addr_t base, vmem_size_t size, 1080 vmem_size_t quantum, vmem_size_t qcache_max, int flags) 1081 { 1082 1083 vmem_t *vm; 1084 1085 vm = uma_zalloc(vmem_zone, flags & (M_WAITOK|M_NOWAIT)); 1086 if (vm == NULL) 1087 return (NULL); 1088 if (vmem_init(vm, name, base, size, quantum, qcache_max, 1089 flags) == NULL) 1090 return (NULL); 1091 return (vm); 1092 } 1093 1094 void 1095 vmem_destroy(vmem_t *vm) 1096 { 1097 1098 mtx_lock(&vmem_list_lock); 1099 LIST_REMOVE(vm, vm_alllist); 1100 mtx_unlock(&vmem_list_lock); 1101 1102 vmem_destroy1(vm); 1103 } 1104 1105 vmem_size_t 1106 vmem_roundup_size(vmem_t *vm, vmem_size_t size) 1107 { 1108 1109 return (size + vm->vm_quantum_mask) & ~vm->vm_quantum_mask; 1110 } 1111 1112 /* 1113 * vmem_alloc: allocate resource from the arena. 1114 */ 1115 int 1116 vmem_alloc(vmem_t *vm, vmem_size_t size, int flags, vmem_addr_t *addrp) 1117 { 1118 const int strat __unused = flags & VMEM_FITMASK; 1119 qcache_t *qc; 1120 1121 flags &= VMEM_FLAGS; 1122 MPASS(size > 0); 1123 MPASS(strat == M_BESTFIT || strat == M_FIRSTFIT); 1124 if ((flags & M_NOWAIT) == 0) 1125 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "vmem_alloc"); 1126 1127 if (size <= vm->vm_qcache_max) { 1128 /* 1129 * Resource 0 cannot be cached, so avoid a blocking allocation 1130 * in qc_import() and give the vmem_xalloc() call below a chance 1131 * to return 0. 1132 */ 1133 qc = &vm->vm_qcache[(size - 1) >> vm->vm_quantum_shift]; 1134 *addrp = (vmem_addr_t)uma_zalloc(qc->qc_cache, 1135 (flags & ~M_WAITOK) | M_NOWAIT); 1136 if (__predict_true(*addrp != 0)) 1137 return (0); 1138 } 1139 1140 return (vmem_xalloc(vm, size, 0, 0, 0, VMEM_ADDR_MIN, VMEM_ADDR_MAX, 1141 flags, addrp)); 1142 } 1143 1144 int 1145 vmem_xalloc(vmem_t *vm, const vmem_size_t size0, vmem_size_t align, 1146 const vmem_size_t phase, const vmem_size_t nocross, 1147 const vmem_addr_t minaddr, const vmem_addr_t maxaddr, int flags, 1148 vmem_addr_t *addrp) 1149 { 1150 const vmem_size_t size = vmem_roundup_size(vm, size0); 1151 struct vmem_freelist *list; 1152 struct vmem_freelist *first; 1153 struct vmem_freelist *end; 1154 vmem_size_t avail; 1155 bt_t *bt; 1156 int error; 1157 int strat; 1158 1159 flags &= VMEM_FLAGS; 1160 strat = flags & VMEM_FITMASK; 1161 MPASS(size0 > 0); 1162 MPASS(size > 0); 1163 MPASS(strat == M_BESTFIT || strat == M_FIRSTFIT); 1164 MPASS((flags & (M_NOWAIT|M_WAITOK)) != (M_NOWAIT|M_WAITOK)); 1165 if ((flags & M_NOWAIT) == 0) 1166 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "vmem_xalloc"); 1167 MPASS((align & vm->vm_quantum_mask) == 0); 1168 MPASS((align & (align - 1)) == 0); 1169 MPASS((phase & vm->vm_quantum_mask) == 0); 1170 MPASS((nocross & vm->vm_quantum_mask) == 0); 1171 MPASS((nocross & (nocross - 1)) == 0); 1172 MPASS((align == 0 && phase == 0) || phase < align); 1173 MPASS(nocross == 0 || nocross >= size); 1174 MPASS(minaddr <= maxaddr); 1175 MPASS(!VMEM_CROSS_P(phase, phase + size - 1, nocross)); 1176 1177 if (align == 0) 1178 align = vm->vm_quantum_mask + 1; 1179 1180 *addrp = 0; 1181 end = &vm->vm_freelist[VMEM_MAXORDER]; 1182 /* 1183 * choose a free block from which we allocate. 1184 */ 1185 first = bt_freehead_toalloc(vm, size, strat); 1186 VMEM_LOCK(vm); 1187 for (;;) { 1188 /* 1189 * Make sure we have enough tags to complete the 1190 * operation. 1191 */ 1192 if (vm->vm_nfreetags < BT_MAXALLOC && 1193 bt_fill(vm, flags) != 0) { 1194 error = ENOMEM; 1195 break; 1196 } 1197 /* 1198 * Scan freelists looking for a tag that satisfies the 1199 * allocation. If we're doing BESTFIT we may encounter 1200 * sizes below the request. If we're doing FIRSTFIT we 1201 * inspect only the first element from each list. 1202 */ 1203 for (list = first; list < end; list++) { 1204 LIST_FOREACH(bt, list, bt_freelist) { 1205 if (bt->bt_size >= size) { 1206 error = vmem_fit(bt, size, align, phase, 1207 nocross, minaddr, maxaddr, addrp); 1208 if (error == 0) { 1209 vmem_clip(vm, bt, *addrp, size); 1210 goto out; 1211 } 1212 } 1213 /* FIRST skips to the next list. */ 1214 if (strat == M_FIRSTFIT) 1215 break; 1216 } 1217 } 1218 /* 1219 * Retry if the fast algorithm failed. 1220 */ 1221 if (strat == M_FIRSTFIT) { 1222 strat = M_BESTFIT; 1223 first = bt_freehead_toalloc(vm, size, strat); 1224 continue; 1225 } 1226 /* 1227 * XXX it is possible to fail to meet restrictions with the 1228 * imported region. It is up to the user to specify the 1229 * import quantum such that it can satisfy any allocation. 1230 */ 1231 if (vmem_import(vm, size, align, flags) == 0) 1232 continue; 1233 1234 /* 1235 * Try to free some space from the quantum cache or reclaim 1236 * functions if available. 1237 */ 1238 if (vm->vm_qcache_max != 0 || vm->vm_reclaimfn != NULL) { 1239 avail = vm->vm_size - vm->vm_inuse; 1240 VMEM_UNLOCK(vm); 1241 if (vm->vm_qcache_max != 0) 1242 qc_drain(vm); 1243 if (vm->vm_reclaimfn != NULL) 1244 vm->vm_reclaimfn(vm, flags); 1245 VMEM_LOCK(vm); 1246 /* If we were successful retry even NOWAIT. */ 1247 if (vm->vm_size - vm->vm_inuse > avail) 1248 continue; 1249 } 1250 if ((flags & M_NOWAIT) != 0) { 1251 error = ENOMEM; 1252 break; 1253 } 1254 VMEM_CONDVAR_WAIT(vm); 1255 } 1256 out: 1257 VMEM_UNLOCK(vm); 1258 if (error != 0 && (flags & M_NOWAIT) == 0) 1259 panic("failed to allocate waiting allocation\n"); 1260 1261 return (error); 1262 } 1263 1264 /* 1265 * vmem_free: free the resource to the arena. 1266 */ 1267 void 1268 vmem_free(vmem_t *vm, vmem_addr_t addr, vmem_size_t size) 1269 { 1270 qcache_t *qc; 1271 MPASS(size > 0); 1272 1273 if (size <= vm->vm_qcache_max && 1274 __predict_true(addr >= VMEM_ADDR_QCACHE_MIN)) { 1275 qc = &vm->vm_qcache[(size - 1) >> vm->vm_quantum_shift]; 1276 uma_zfree(qc->qc_cache, (void *)addr); 1277 } else 1278 vmem_xfree(vm, addr, size); 1279 } 1280 1281 void 1282 vmem_xfree(vmem_t *vm, vmem_addr_t addr, vmem_size_t size) 1283 { 1284 bt_t *bt; 1285 bt_t *t; 1286 1287 MPASS(size > 0); 1288 1289 VMEM_LOCK(vm); 1290 bt = bt_lookupbusy(vm, addr); 1291 MPASS(bt != NULL); 1292 MPASS(bt->bt_start == addr); 1293 MPASS(bt->bt_size == vmem_roundup_size(vm, size) || 1294 bt->bt_size - vmem_roundup_size(vm, size) <= vm->vm_quantum_mask); 1295 MPASS(bt->bt_type == BT_TYPE_BUSY); 1296 bt_rembusy(vm, bt); 1297 bt->bt_type = BT_TYPE_FREE; 1298 1299 /* coalesce */ 1300 t = TAILQ_NEXT(bt, bt_seglist); 1301 if (t != NULL && t->bt_type == BT_TYPE_FREE) { 1302 MPASS(BT_END(bt) < t->bt_start); /* YYY */ 1303 bt->bt_size += t->bt_size; 1304 bt_remfree(vm, t); 1305 bt_remseg(vm, t); 1306 } 1307 t = TAILQ_PREV(bt, vmem_seglist, bt_seglist); 1308 if (t != NULL && t->bt_type == BT_TYPE_FREE) { 1309 MPASS(BT_END(t) < bt->bt_start); /* YYY */ 1310 bt->bt_size += t->bt_size; 1311 bt->bt_start = t->bt_start; 1312 bt_remfree(vm, t); 1313 bt_remseg(vm, t); 1314 } 1315 1316 t = TAILQ_PREV(bt, vmem_seglist, bt_seglist); 1317 MPASS(t != NULL); 1318 MPASS(BT_ISSPAN_P(t) || t->bt_type == BT_TYPE_BUSY); 1319 if (vm->vm_releasefn != NULL && t->bt_type == BT_TYPE_SPAN && 1320 t->bt_size == bt->bt_size) { 1321 vmem_addr_t spanaddr; 1322 vmem_size_t spansize; 1323 1324 MPASS(t->bt_start == bt->bt_start); 1325 spanaddr = bt->bt_start; 1326 spansize = bt->bt_size; 1327 bt_remseg(vm, bt); 1328 bt_remseg(vm, t); 1329 vm->vm_size -= spansize; 1330 VMEM_CONDVAR_BROADCAST(vm); 1331 bt_freetrim(vm, BT_MAXFREE); 1332 (*vm->vm_releasefn)(vm->vm_arg, spanaddr, spansize); 1333 } else { 1334 bt_insfree(vm, bt); 1335 VMEM_CONDVAR_BROADCAST(vm); 1336 bt_freetrim(vm, BT_MAXFREE); 1337 } 1338 } 1339 1340 /* 1341 * vmem_add: 1342 * 1343 */ 1344 int 1345 vmem_add(vmem_t *vm, vmem_addr_t addr, vmem_size_t size, int flags) 1346 { 1347 int error; 1348 1349 error = 0; 1350 flags &= VMEM_FLAGS; 1351 VMEM_LOCK(vm); 1352 if (vm->vm_nfreetags >= BT_MAXALLOC || bt_fill(vm, flags) == 0) 1353 vmem_add1(vm, addr, size, BT_TYPE_SPAN_STATIC); 1354 else 1355 error = ENOMEM; 1356 VMEM_UNLOCK(vm); 1357 1358 return (error); 1359 } 1360 1361 /* 1362 * vmem_size: information about arenas size 1363 */ 1364 vmem_size_t 1365 vmem_size(vmem_t *vm, int typemask) 1366 { 1367 int i; 1368 1369 switch (typemask) { 1370 case VMEM_ALLOC: 1371 return vm->vm_inuse; 1372 case VMEM_FREE: 1373 return vm->vm_size - vm->vm_inuse; 1374 case VMEM_FREE|VMEM_ALLOC: 1375 return vm->vm_size; 1376 case VMEM_MAXFREE: 1377 VMEM_LOCK(vm); 1378 for (i = VMEM_MAXORDER - 1; i >= 0; i--) { 1379 if (LIST_EMPTY(&vm->vm_freelist[i])) 1380 continue; 1381 VMEM_UNLOCK(vm); 1382 return ((vmem_size_t)ORDER2SIZE(i) << 1383 vm->vm_quantum_shift); 1384 } 1385 VMEM_UNLOCK(vm); 1386 return (0); 1387 default: 1388 panic("vmem_size"); 1389 } 1390 } 1391 1392 /* ---- debug */ 1393 1394 #if defined(DDB) || defined(DIAGNOSTIC) 1395 1396 static void bt_dump(const bt_t *, int (*)(const char *, ...) 1397 __printflike(1, 2)); 1398 1399 static const char * 1400 bt_type_string(int type) 1401 { 1402 1403 switch (type) { 1404 case BT_TYPE_BUSY: 1405 return "busy"; 1406 case BT_TYPE_FREE: 1407 return "free"; 1408 case BT_TYPE_SPAN: 1409 return "span"; 1410 case BT_TYPE_SPAN_STATIC: 1411 return "static span"; 1412 default: 1413 break; 1414 } 1415 return "BOGUS"; 1416 } 1417 1418 static void 1419 bt_dump(const bt_t *bt, int (*pr)(const char *, ...)) 1420 { 1421 1422 (*pr)("\t%p: %jx %jx, %d(%s)\n", 1423 bt, (intmax_t)bt->bt_start, (intmax_t)bt->bt_size, 1424 bt->bt_type, bt_type_string(bt->bt_type)); 1425 } 1426 1427 static void 1428 vmem_dump(const vmem_t *vm , int (*pr)(const char *, ...) __printflike(1, 2)) 1429 { 1430 const bt_t *bt; 1431 int i; 1432 1433 (*pr)("vmem %p '%s'\n", vm, vm->vm_name); 1434 TAILQ_FOREACH(bt, &vm->vm_seglist, bt_seglist) { 1435 bt_dump(bt, pr); 1436 } 1437 1438 for (i = 0; i < VMEM_MAXORDER; i++) { 1439 const struct vmem_freelist *fl = &vm->vm_freelist[i]; 1440 1441 if (LIST_EMPTY(fl)) { 1442 continue; 1443 } 1444 1445 (*pr)("freelist[%d]\n", i); 1446 LIST_FOREACH(bt, fl, bt_freelist) { 1447 bt_dump(bt, pr); 1448 } 1449 } 1450 } 1451 1452 #endif /* defined(DDB) || defined(DIAGNOSTIC) */ 1453 1454 #if defined(DDB) 1455 #include <ddb/ddb.h> 1456 1457 static bt_t * 1458 vmem_whatis_lookup(vmem_t *vm, vmem_addr_t addr) 1459 { 1460 bt_t *bt; 1461 1462 TAILQ_FOREACH(bt, &vm->vm_seglist, bt_seglist) { 1463 if (BT_ISSPAN_P(bt)) { 1464 continue; 1465 } 1466 if (bt->bt_start <= addr && addr <= BT_END(bt)) { 1467 return bt; 1468 } 1469 } 1470 1471 return NULL; 1472 } 1473 1474 void 1475 vmem_whatis(vmem_addr_t addr, int (*pr)(const char *, ...)) 1476 { 1477 vmem_t *vm; 1478 1479 LIST_FOREACH(vm, &vmem_list, vm_alllist) { 1480 bt_t *bt; 1481 1482 bt = vmem_whatis_lookup(vm, addr); 1483 if (bt == NULL) { 1484 continue; 1485 } 1486 (*pr)("%p is %p+%zu in VMEM '%s' (%s)\n", 1487 (void *)addr, (void *)bt->bt_start, 1488 (vmem_size_t)(addr - bt->bt_start), vm->vm_name, 1489 (bt->bt_type == BT_TYPE_BUSY) ? "allocated" : "free"); 1490 } 1491 } 1492 1493 void 1494 vmem_printall(const char *modif, int (*pr)(const char *, ...)) 1495 { 1496 const vmem_t *vm; 1497 1498 LIST_FOREACH(vm, &vmem_list, vm_alllist) { 1499 vmem_dump(vm, pr); 1500 } 1501 } 1502 1503 void 1504 vmem_print(vmem_addr_t addr, const char *modif, int (*pr)(const char *, ...)) 1505 { 1506 const vmem_t *vm = (const void *)addr; 1507 1508 vmem_dump(vm, pr); 1509 } 1510 1511 DB_SHOW_COMMAND(vmemdump, vmemdump) 1512 { 1513 1514 if (!have_addr) { 1515 db_printf("usage: show vmemdump <addr>\n"); 1516 return; 1517 } 1518 1519 vmem_dump((const vmem_t *)addr, db_printf); 1520 } 1521 1522 DB_SHOW_ALL_COMMAND(vmemdump, vmemdumpall) 1523 { 1524 const vmem_t *vm; 1525 1526 LIST_FOREACH(vm, &vmem_list, vm_alllist) 1527 vmem_dump(vm, db_printf); 1528 } 1529 1530 DB_SHOW_COMMAND(vmem, vmem_summ) 1531 { 1532 const vmem_t *vm = (const void *)addr; 1533 const bt_t *bt; 1534 size_t ft[VMEM_MAXORDER], ut[VMEM_MAXORDER]; 1535 size_t fs[VMEM_MAXORDER], us[VMEM_MAXORDER]; 1536 int ord; 1537 1538 if (!have_addr) { 1539 db_printf("usage: show vmem <addr>\n"); 1540 return; 1541 } 1542 1543 db_printf("vmem %p '%s'\n", vm, vm->vm_name); 1544 db_printf("\tquantum:\t%zu\n", vm->vm_quantum_mask + 1); 1545 db_printf("\tsize:\t%zu\n", vm->vm_size); 1546 db_printf("\tinuse:\t%zu\n", vm->vm_inuse); 1547 db_printf("\tfree:\t%zu\n", vm->vm_size - vm->vm_inuse); 1548 db_printf("\tbusy tags:\t%d\n", vm->vm_nbusytag); 1549 db_printf("\tfree tags:\t%d\n", vm->vm_nfreetags); 1550 1551 memset(&ft, 0, sizeof(ft)); 1552 memset(&ut, 0, sizeof(ut)); 1553 memset(&fs, 0, sizeof(fs)); 1554 memset(&us, 0, sizeof(us)); 1555 TAILQ_FOREACH(bt, &vm->vm_seglist, bt_seglist) { 1556 ord = SIZE2ORDER(bt->bt_size >> vm->vm_quantum_shift); 1557 if (bt->bt_type == BT_TYPE_BUSY) { 1558 ut[ord]++; 1559 us[ord] += bt->bt_size; 1560 } else if (bt->bt_type == BT_TYPE_FREE) { 1561 ft[ord]++; 1562 fs[ord] += bt->bt_size; 1563 } 1564 } 1565 db_printf("\t\t\tinuse\tsize\t\tfree\tsize\n"); 1566 for (ord = 0; ord < VMEM_MAXORDER; ord++) { 1567 if (ut[ord] == 0 && ft[ord] == 0) 1568 continue; 1569 db_printf("\t%-15zu %zu\t%-15zu %zu\t%-16zu\n", 1570 ORDER2SIZE(ord) << vm->vm_quantum_shift, 1571 ut[ord], us[ord], ft[ord], fs[ord]); 1572 } 1573 } 1574 1575 DB_SHOW_ALL_COMMAND(vmem, vmem_summall) 1576 { 1577 const vmem_t *vm; 1578 1579 LIST_FOREACH(vm, &vmem_list, vm_alllist) 1580 vmem_summ((db_expr_t)vm, TRUE, count, modif); 1581 } 1582 #endif /* defined(DDB) */ 1583 1584 #define vmem_printf printf 1585 1586 #if defined(DIAGNOSTIC) 1587 1588 static bool 1589 vmem_check_sanity(vmem_t *vm) 1590 { 1591 const bt_t *bt, *bt2; 1592 1593 MPASS(vm != NULL); 1594 1595 TAILQ_FOREACH(bt, &vm->vm_seglist, bt_seglist) { 1596 if (bt->bt_start > BT_END(bt)) { 1597 printf("corrupted tag\n"); 1598 bt_dump(bt, vmem_printf); 1599 return false; 1600 } 1601 } 1602 TAILQ_FOREACH(bt, &vm->vm_seglist, bt_seglist) { 1603 TAILQ_FOREACH(bt2, &vm->vm_seglist, bt_seglist) { 1604 if (bt == bt2) { 1605 continue; 1606 } 1607 if (BT_ISSPAN_P(bt) != BT_ISSPAN_P(bt2)) { 1608 continue; 1609 } 1610 if (bt->bt_start <= BT_END(bt2) && 1611 bt2->bt_start <= BT_END(bt)) { 1612 printf("overwrapped tags\n"); 1613 bt_dump(bt, vmem_printf); 1614 bt_dump(bt2, vmem_printf); 1615 return false; 1616 } 1617 } 1618 } 1619 1620 return true; 1621 } 1622 1623 static void 1624 vmem_check(vmem_t *vm) 1625 { 1626 1627 if (!vmem_check_sanity(vm)) { 1628 panic("insanity vmem %p", vm); 1629 } 1630 } 1631 1632 #endif /* defined(DIAGNOSTIC) */ 1633