1 /* $NetBSD: kern_malloc.c,v 1.71 2002/04/03 09:45:22 fvdl Exp $ */ 2 3 /* 4 * Copyright (c) 1996 Christopher G. Demetriou. All rights reserved. 5 * Copyright (c) 1987, 1991, 1993 6 * The Regents of the University of California. 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 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 * 36 * @(#)kern_malloc.c 8.4 (Berkeley) 5/20/95 37 */ 38 39 #include <sys/cdefs.h> 40 __KERNEL_RCSID(0, "$NetBSD: kern_malloc.c,v 1.71 2002/04/03 09:45:22 fvdl Exp $"); 41 42 #include "opt_lockdebug.h" 43 44 #include <sys/param.h> 45 #include <sys/proc.h> 46 #include <sys/map.h> 47 #include <sys/kernel.h> 48 #include <sys/malloc.h> 49 #include <sys/systm.h> 50 51 #include <uvm/uvm_extern.h> 52 53 static struct vm_map kmem_map_store; 54 struct vm_map *kmem_map = NULL; 55 56 #include "opt_kmempages.h" 57 58 #ifdef NKMEMCLUSTERS 59 #error NKMEMCLUSTERS is obsolete; remove it from your kernel config file and use NKMEMPAGES instead or let the kernel auto-size 60 #endif 61 62 /* 63 * Default number of pages in kmem_map. We attempt to calculate this 64 * at run-time, but allow it to be either patched or set in the kernel 65 * config file. 66 */ 67 #ifndef NKMEMPAGES 68 #define NKMEMPAGES 0 69 #endif 70 int nkmempages = NKMEMPAGES; 71 72 /* 73 * Defaults for lower- and upper-bounds for the kmem_map page count. 74 * Can be overridden by kernel config options. 75 */ 76 #ifndef NKMEMPAGES_MIN 77 #define NKMEMPAGES_MIN NKMEMPAGES_MIN_DEFAULT 78 #endif 79 80 #ifndef NKMEMPAGES_MAX 81 #define NKMEMPAGES_MAX NKMEMPAGES_MAX_DEFAULT 82 #endif 83 84 #include "opt_kmemstats.h" 85 #include "opt_malloclog.h" 86 #include "opt_malloc_debug.h" 87 88 struct kmembuckets bucket[MINBUCKET + 16]; 89 struct kmemstats kmemstats[M_LAST]; 90 struct kmemusage *kmemusage; 91 char *kmembase, *kmemlimit; 92 const char * const memname[] = INITKMEMNAMES; 93 94 #ifdef MALLOCLOG 95 #ifndef MALLOCLOGSIZE 96 #define MALLOCLOGSIZE 100000 97 #endif 98 99 struct malloclog { 100 void *addr; 101 long size; 102 int type; 103 int action; 104 const char *file; 105 long line; 106 } malloclog[MALLOCLOGSIZE]; 107 108 long malloclogptr; 109 110 static void domlog(void *, long, int, int, const char *, long); 111 static void hitmlog(void *); 112 113 static void 114 domlog(void *a, long size, int type, int action, const char *file, long line) 115 { 116 117 malloclog[malloclogptr].addr = a; 118 malloclog[malloclogptr].size = size; 119 malloclog[malloclogptr].type = type; 120 malloclog[malloclogptr].action = action; 121 malloclog[malloclogptr].file = file; 122 malloclog[malloclogptr].line = line; 123 malloclogptr++; 124 if (malloclogptr >= MALLOCLOGSIZE) 125 malloclogptr = 0; 126 } 127 128 static void 129 hitmlog(void *a) 130 { 131 struct malloclog *lp; 132 long l; 133 134 #define PRT do { \ 135 if (malloclog[l].addr == a && malloclog[l].action) { \ 136 lp = &malloclog[l]; \ 137 printf("malloc log entry %ld:\n", l); \ 138 printf("\taddr = %p\n", lp->addr); \ 139 printf("\tsize = %ld\n", lp->size); \ 140 printf("\ttype = %s\n", memname[lp->type]); \ 141 printf("\taction = %s\n", lp->action == 1 ? "alloc" : "free"); \ 142 printf("\tfile = %s\n", lp->file); \ 143 printf("\tline = %ld\n", lp->line); \ 144 } \ 145 } while (/* CONSTCOND */0) 146 147 for (l = malloclogptr; l < MALLOCLOGSIZE; l++) 148 PRT; 149 150 for (l = 0; l < malloclogptr; l++) 151 PRT; 152 } 153 #endif /* MALLOCLOG */ 154 155 #ifdef DIAGNOSTIC 156 /* 157 * This structure provides a set of masks to catch unaligned frees. 158 */ 159 const long addrmask[] = { 0, 160 0x00000001, 0x00000003, 0x00000007, 0x0000000f, 161 0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff, 162 0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff, 163 0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff, 164 }; 165 166 /* 167 * The WEIRD_ADDR is used as known text to copy into free objects so 168 * that modifications after frees can be detected. 169 */ 170 #define WEIRD_ADDR ((unsigned) 0xdeadbeef) 171 #ifdef DEBUG 172 #define MAX_COPY PAGE_SIZE 173 #else 174 #define MAX_COPY 32 175 #endif 176 177 /* 178 * Normally the freelist structure is used only to hold the list pointer 179 * for free objects. However, when running with diagnostics, the first 180 * 8 bytes of the structure is unused except for diagnostic information, 181 * and the free list pointer is at offst 8 in the structure. Since the 182 * first 8 bytes is the portion of the structure most often modified, this 183 * helps to detect memory reuse problems and avoid free list corruption. 184 */ 185 struct freelist { 186 int32_t spare0; 187 int16_t type; 188 int16_t spare1; 189 caddr_t next; 190 }; 191 #else /* !DIAGNOSTIC */ 192 struct freelist { 193 caddr_t next; 194 }; 195 #endif /* DIAGNOSTIC */ 196 197 /* 198 * Allocate a block of memory 199 */ 200 #ifdef MALLOCLOG 201 void * 202 _malloc(unsigned long size, int type, int flags, const char *file, long line) 203 #else 204 void * 205 malloc(unsigned long size, int type, int flags) 206 #endif /* MALLOCLOG */ 207 { 208 struct kmembuckets *kbp; 209 struct kmemusage *kup; 210 struct freelist *freep; 211 long indx, npg, allocsize; 212 int s; 213 caddr_t va, cp, savedlist; 214 #ifdef DIAGNOSTIC 215 int32_t *end, *lp; 216 int copysize; 217 const char *savedtype; 218 #endif 219 #ifdef KMEMSTATS 220 struct kmemstats *ksp = &kmemstats[type]; 221 222 if (__predict_false(((unsigned long)type) > M_LAST)) 223 panic("malloc - bogus type"); 224 #endif 225 #ifdef LOCKDEBUG 226 if ((flags & M_NOWAIT) == 0) 227 simple_lock_only_held(NULL, "malloc"); 228 #endif 229 #ifdef MALLOC_DEBUG 230 if (debug_malloc(size, type, flags, (void **) &va)) 231 return ((void *) va); 232 #endif 233 indx = BUCKETINDX(size); 234 kbp = &bucket[indx]; 235 s = splvm(); 236 #ifdef KMEMSTATS 237 while (ksp->ks_memuse >= ksp->ks_limit) { 238 if (flags & M_NOWAIT) { 239 splx(s); 240 return ((void *) NULL); 241 } 242 if (ksp->ks_limblocks < 65535) 243 ksp->ks_limblocks++; 244 tsleep((caddr_t)ksp, PSWP+2, memname[type], 0); 245 } 246 ksp->ks_size |= 1 << indx; 247 #endif 248 #ifdef DIAGNOSTIC 249 copysize = 1 << indx < MAX_COPY ? 1 << indx : MAX_COPY; 250 #endif 251 if (kbp->kb_next == NULL) { 252 kbp->kb_last = NULL; 253 if (size > MAXALLOCSAVE) 254 allocsize = round_page(size); 255 else 256 allocsize = 1 << indx; 257 npg = btoc(allocsize); 258 va = (caddr_t) uvm_km_kmemalloc(kmem_map, NULL, 259 (vsize_t)ctob(npg), 260 (flags & M_NOWAIT) ? UVM_KMF_NOWAIT : 0); 261 if (__predict_false(va == NULL)) { 262 /* 263 * Kmem_malloc() can return NULL, even if it can 264 * wait, if there is no map space avaiable, because 265 * it can't fix that problem. Neither can we, 266 * right now. (We should release pages which 267 * are completely free and which are in buckets 268 * with too many free elements.) 269 */ 270 if ((flags & (M_NOWAIT|M_CANFAIL)) == 0) 271 panic("malloc: out of space in kmem_map"); 272 splx(s); 273 return ((void *) NULL); 274 } 275 #ifdef KMEMSTATS 276 kbp->kb_total += kbp->kb_elmpercl; 277 #endif 278 kup = btokup(va); 279 kup->ku_indx = indx; 280 if (allocsize > MAXALLOCSAVE) { 281 if (npg > 65535) 282 panic("malloc: allocation too large"); 283 kup->ku_pagecnt = npg; 284 #ifdef KMEMSTATS 285 ksp->ks_memuse += allocsize; 286 #endif 287 goto out; 288 } 289 #ifdef KMEMSTATS 290 kup->ku_freecnt = kbp->kb_elmpercl; 291 kbp->kb_totalfree += kbp->kb_elmpercl; 292 #endif 293 /* 294 * Just in case we blocked while allocating memory, 295 * and someone else also allocated memory for this 296 * bucket, don't assume the list is still empty. 297 */ 298 savedlist = kbp->kb_next; 299 kbp->kb_next = cp = va + (npg << PAGE_SHIFT) - allocsize; 300 for (;;) { 301 freep = (struct freelist *)cp; 302 #ifdef DIAGNOSTIC 303 /* 304 * Copy in known text to detect modification 305 * after freeing. 306 */ 307 end = (int32_t *)&cp[copysize]; 308 for (lp = (int32_t *)cp; lp < end; lp++) 309 *lp = WEIRD_ADDR; 310 freep->type = M_FREE; 311 #endif /* DIAGNOSTIC */ 312 if (cp <= va) 313 break; 314 cp -= allocsize; 315 freep->next = cp; 316 } 317 freep->next = savedlist; 318 if (kbp->kb_last == NULL) 319 kbp->kb_last = (caddr_t)freep; 320 } 321 va = kbp->kb_next; 322 kbp->kb_next = ((struct freelist *)va)->next; 323 #ifdef DIAGNOSTIC 324 freep = (struct freelist *)va; 325 savedtype = (unsigned)freep->type < M_LAST ? 326 memname[freep->type] : "???"; 327 if (kbp->kb_next) { 328 int rv; 329 vaddr_t addr = (vaddr_t)kbp->kb_next; 330 331 vm_map_lock(kmem_map); 332 rv = uvm_map_checkprot(kmem_map, addr, 333 addr + sizeof(struct freelist), VM_PROT_WRITE); 334 vm_map_unlock(kmem_map); 335 336 if (__predict_false(rv == 0)) { 337 printf("Data modified on freelist: " 338 "word %ld of object %p size %ld previous type %s " 339 "(invalid addr %p)\n", 340 (long)((int32_t *)&kbp->kb_next - (int32_t *)kbp), 341 va, size, savedtype, kbp->kb_next); 342 #ifdef MALLOCLOG 343 hitmlog(va); 344 #endif 345 kbp->kb_next = NULL; 346 } 347 } 348 349 /* Fill the fields that we've used with WEIRD_ADDR */ 350 #if BYTE_ORDER == BIG_ENDIAN 351 freep->type = WEIRD_ADDR >> 16; 352 #endif 353 #if BYTE_ORDER == LITTLE_ENDIAN 354 freep->type = (short)WEIRD_ADDR; 355 #endif 356 end = (int32_t *)&freep->next + 357 (sizeof(freep->next) / sizeof(int32_t)); 358 for (lp = (int32_t *)&freep->next; lp < end; lp++) 359 *lp = WEIRD_ADDR; 360 361 /* and check that the data hasn't been modified. */ 362 end = (int32_t *)&va[copysize]; 363 for (lp = (int32_t *)va; lp < end; lp++) { 364 if (__predict_true(*lp == WEIRD_ADDR)) 365 continue; 366 printf("Data modified on freelist: " 367 "word %ld of object %p size %ld previous type %s " 368 "(0x%x != 0x%x)\n", 369 (long)(lp - (int32_t *)va), va, size, 370 savedtype, *lp, WEIRD_ADDR); 371 #ifdef MALLOCLOG 372 hitmlog(va); 373 #endif 374 break; 375 } 376 377 freep->spare0 = 0; 378 #endif /* DIAGNOSTIC */ 379 #ifdef KMEMSTATS 380 kup = btokup(va); 381 if (kup->ku_indx != indx) 382 panic("malloc: wrong bucket"); 383 if (kup->ku_freecnt == 0) 384 panic("malloc: lost data"); 385 kup->ku_freecnt--; 386 kbp->kb_totalfree--; 387 ksp->ks_memuse += 1 << indx; 388 out: 389 kbp->kb_calls++; 390 ksp->ks_inuse++; 391 ksp->ks_calls++; 392 if (ksp->ks_memuse > ksp->ks_maxused) 393 ksp->ks_maxused = ksp->ks_memuse; 394 #else 395 out: 396 #endif 397 #ifdef MALLOCLOG 398 domlog(va, size, type, 1, file, line); 399 #endif 400 splx(s); 401 if ((flags & M_ZERO) != 0) 402 memset(va, 0, size); 403 return ((void *) va); 404 } 405 406 /* 407 * Free a block of memory allocated by malloc. 408 */ 409 #ifdef MALLOCLOG 410 void 411 _free(void *addr, int type, const char *file, long line) 412 #else 413 void 414 free(void *addr, int type) 415 #endif /* MALLOCLOG */ 416 { 417 struct kmembuckets *kbp; 418 struct kmemusage *kup; 419 struct freelist *freep; 420 long size; 421 int s; 422 #ifdef DIAGNOSTIC 423 caddr_t cp; 424 int32_t *end, *lp; 425 long alloc, copysize; 426 #endif 427 #ifdef KMEMSTATS 428 struct kmemstats *ksp = &kmemstats[type]; 429 #endif 430 431 #ifdef MALLOC_DEBUG 432 if (debug_free(addr, type)) 433 return; 434 #endif 435 436 #ifdef DIAGNOSTIC 437 /* 438 * Ensure that we're free'ing something that we could 439 * have allocated in the first place. That is, check 440 * to see that the address is within kmem_map. 441 */ 442 if (__predict_false((vaddr_t)addr < kmem_map->header.start || 443 (vaddr_t)addr >= kmem_map->header.end)) 444 panic("free: addr %p not within kmem_map", addr); 445 #endif 446 447 kup = btokup(addr); 448 size = 1 << kup->ku_indx; 449 kbp = &bucket[kup->ku_indx]; 450 s = splvm(); 451 #ifdef MALLOCLOG 452 domlog(addr, 0, type, 2, file, line); 453 #endif 454 #ifdef DIAGNOSTIC 455 /* 456 * Check for returns of data that do not point to the 457 * beginning of the allocation. 458 */ 459 if (size > PAGE_SIZE) 460 alloc = addrmask[BUCKETINDX(PAGE_SIZE)]; 461 else 462 alloc = addrmask[kup->ku_indx]; 463 if (((u_long)addr & alloc) != 0) 464 panic("free: unaligned addr %p, size %ld, type %s, mask %ld\n", 465 addr, size, memname[type], alloc); 466 #endif /* DIAGNOSTIC */ 467 if (size > MAXALLOCSAVE) { 468 uvm_km_free(kmem_map, (vaddr_t)addr, ctob(kup->ku_pagecnt)); 469 #ifdef KMEMSTATS 470 size = kup->ku_pagecnt << PGSHIFT; 471 ksp->ks_memuse -= size; 472 kup->ku_indx = 0; 473 kup->ku_pagecnt = 0; 474 if (ksp->ks_memuse + size >= ksp->ks_limit && 475 ksp->ks_memuse < ksp->ks_limit) 476 wakeup((caddr_t)ksp); 477 ksp->ks_inuse--; 478 kbp->kb_total -= 1; 479 #endif 480 splx(s); 481 return; 482 } 483 freep = (struct freelist *)addr; 484 #ifdef DIAGNOSTIC 485 /* 486 * Check for multiple frees. Use a quick check to see if 487 * it looks free before laboriously searching the freelist. 488 */ 489 if (__predict_false(freep->spare0 == WEIRD_ADDR)) { 490 for (cp = kbp->kb_next; cp; 491 cp = ((struct freelist *)cp)->next) { 492 if (addr != cp) 493 continue; 494 printf("multiply freed item %p\n", addr); 495 #ifdef MALLOCLOG 496 hitmlog(addr); 497 #endif 498 panic("free: duplicated free"); 499 } 500 } 501 #ifdef LOCKDEBUG 502 /* 503 * Check if we're freeing a locked simple lock. 504 */ 505 simple_lock_freecheck(addr, (char *)addr + size); 506 #endif 507 /* 508 * Copy in known text to detect modification after freeing 509 * and to make it look free. Also, save the type being freed 510 * so we can list likely culprit if modification is detected 511 * when the object is reallocated. 512 */ 513 copysize = size < MAX_COPY ? size : MAX_COPY; 514 end = (int32_t *)&((caddr_t)addr)[copysize]; 515 for (lp = (int32_t *)addr; lp < end; lp++) 516 *lp = WEIRD_ADDR; 517 freep->type = type; 518 #endif /* DIAGNOSTIC */ 519 #ifdef KMEMSTATS 520 kup->ku_freecnt++; 521 if (kup->ku_freecnt >= kbp->kb_elmpercl) { 522 if (kup->ku_freecnt > kbp->kb_elmpercl) 523 panic("free: multiple frees"); 524 else if (kbp->kb_totalfree > kbp->kb_highwat) 525 kbp->kb_couldfree++; 526 } 527 kbp->kb_totalfree++; 528 ksp->ks_memuse -= size; 529 if (ksp->ks_memuse + size >= ksp->ks_limit && 530 ksp->ks_memuse < ksp->ks_limit) 531 wakeup((caddr_t)ksp); 532 ksp->ks_inuse--; 533 #endif 534 if (kbp->kb_next == NULL) 535 kbp->kb_next = addr; 536 else 537 ((struct freelist *)kbp->kb_last)->next = addr; 538 freep->next = NULL; 539 kbp->kb_last = addr; 540 splx(s); 541 } 542 543 /* 544 * Change the size of a block of memory. 545 */ 546 void * 547 realloc(void *curaddr, unsigned long newsize, int type, int flags) 548 { 549 struct kmemusage *kup; 550 long cursize; 551 void *newaddr; 552 #ifdef DIAGNOSTIC 553 long alloc; 554 #endif 555 556 /* 557 * realloc() with a NULL pointer is the same as malloc(). 558 */ 559 if (curaddr == NULL) 560 return (malloc(newsize, type, flags)); 561 562 /* 563 * realloc() with zero size is the same as free(). 564 */ 565 if (newsize == 0) { 566 free(curaddr, type); 567 return (NULL); 568 } 569 570 #ifdef LOCKDEBUG 571 if ((flags & M_NOWAIT) == 0) 572 simple_lock_only_held(NULL, "realloc"); 573 #endif 574 575 /* 576 * Find out how large the old allocation was (and do some 577 * sanity checking). 578 */ 579 kup = btokup(curaddr); 580 cursize = 1 << kup->ku_indx; 581 582 #ifdef DIAGNOSTIC 583 /* 584 * Check for returns of data that do not point to the 585 * beginning of the allocation. 586 */ 587 if (cursize > PAGE_SIZE) 588 alloc = addrmask[BUCKETINDX(PAGE_SIZE)]; 589 else 590 alloc = addrmask[kup->ku_indx]; 591 if (((u_long)curaddr & alloc) != 0) 592 panic("realloc: " 593 "unaligned addr %p, size %ld, type %s, mask %ld\n", 594 curaddr, cursize, memname[type], alloc); 595 #endif /* DIAGNOSTIC */ 596 597 if (cursize > MAXALLOCSAVE) 598 cursize = ctob(kup->ku_pagecnt); 599 600 /* 601 * If we already actually have as much as they want, we're done. 602 */ 603 if (newsize <= cursize) 604 return (curaddr); 605 606 /* 607 * Can't satisfy the allocation with the existing block. 608 * Allocate a new one and copy the data. 609 */ 610 newaddr = malloc(newsize, type, flags); 611 if (__predict_false(newaddr == NULL)) { 612 /* 613 * malloc() failed, because flags included M_NOWAIT. 614 * Return NULL to indicate that failure. The old 615 * pointer is still valid. 616 */ 617 return (NULL); 618 } 619 memcpy(newaddr, curaddr, cursize); 620 621 /* 622 * We were successful: free the old allocation and return 623 * the new one. 624 */ 625 free(curaddr, type); 626 return (newaddr); 627 } 628 629 /* 630 * Roundup size to the actual allocation size. 631 */ 632 unsigned long 633 malloc_roundup(unsigned long size) 634 { 635 636 if (size > MAXALLOCSAVE) 637 return (roundup(size, PAGE_SIZE)); 638 else 639 return (1 << BUCKETINDX(size)); 640 } 641 642 /* 643 * Compute the number of pages that kmem_map will map, that is, 644 * the size of the kernel malloc arena. 645 */ 646 void 647 kmeminit_nkmempages(void) 648 { 649 int npages; 650 651 if (nkmempages != 0) { 652 /* 653 * It's already been set (by us being here before, or 654 * by patching or kernel config options), bail out now. 655 */ 656 return; 657 } 658 659 /* 660 * We use the following (simple) formula: 661 * 662 * - Starting point is physical memory / 4. 663 * 664 * - Clamp it down to NKMEMPAGES_MAX. 665 * 666 * - Round it up to NKMEMPAGES_MIN. 667 */ 668 npages = physmem / 4; 669 670 if (npages > NKMEMPAGES_MAX) 671 npages = NKMEMPAGES_MAX; 672 673 if (npages < NKMEMPAGES_MIN) 674 npages = NKMEMPAGES_MIN; 675 676 nkmempages = npages; 677 } 678 679 /* 680 * Initialize the kernel memory allocator 681 */ 682 void 683 kmeminit(void) 684 { 685 #ifdef KMEMSTATS 686 long indx; 687 #endif 688 689 #if ((MAXALLOCSAVE & (MAXALLOCSAVE - 1)) != 0) 690 ERROR!_kmeminit:_MAXALLOCSAVE_not_power_of_2 691 #endif 692 #if (MAXALLOCSAVE > MINALLOCSIZE * 32768) 693 ERROR!_kmeminit:_MAXALLOCSAVE_too_big 694 #endif 695 #if (MAXALLOCSAVE < NBPG) 696 ERROR!_kmeminit:_MAXALLOCSAVE_too_small 697 #endif 698 699 if (sizeof(struct freelist) > (1 << MINBUCKET)) 700 panic("minbucket too small/struct freelist too big"); 701 702 /* 703 * Compute the number of kmem_map pages, if we have not 704 * done so already. 705 */ 706 kmeminit_nkmempages(); 707 708 kmemusage = (struct kmemusage *) uvm_km_zalloc(kernel_map, 709 (vsize_t)(nkmempages * sizeof(struct kmemusage))); 710 kmem_map = uvm_km_suballoc(kernel_map, (vaddr_t *)&kmembase, 711 (vaddr_t *)&kmemlimit, (vsize_t)(nkmempages << PAGE_SHIFT), 712 VM_MAP_INTRSAFE, FALSE, &kmem_map_store); 713 #ifdef KMEMSTATS 714 for (indx = 0; indx < MINBUCKET + 16; indx++) { 715 if (1 << indx >= PAGE_SIZE) 716 bucket[indx].kb_elmpercl = 1; 717 else 718 bucket[indx].kb_elmpercl = PAGE_SIZE / (1 << indx); 719 bucket[indx].kb_highwat = 5 * bucket[indx].kb_elmpercl; 720 } 721 for (indx = 0; indx < M_LAST; indx++) 722 kmemstats[indx].ks_limit = 723 ((u_long)nkmempages << PAGE_SHIFT) * 6U / 10U; 724 #endif 725 #ifdef MALLOC_DEBUG 726 debug_malloc_init(); 727 #endif 728 } 729 730 #ifdef DDB 731 #include <ddb/db_output.h> 732 733 /* 734 * Dump kmem statistics from ddb. 735 * 736 * usage: call dump_kmemstats 737 */ 738 void dump_kmemstats(void); 739 740 void 741 dump_kmemstats(void) 742 { 743 #ifdef KMEMSTATS 744 const char *name; 745 int i; 746 747 for (i = 0; i < M_LAST; i++) { 748 name = memname[i] ? memname[i] : ""; 749 750 db_printf("%2d %s%.*s %ld\n", i, name, 751 (int)(20 - strlen(name)), " ", 752 kmemstats[i].ks_memuse); 753 } 754 #else 755 db_printf("Kmem stats are not being collected.\n"); 756 #endif /* KMEMSTATS */ 757 } 758 #endif /* DDB */ 759