1 /* $OpenBSD: kern_malloc.c,v 1.133 2018/01/18 18:08:51 bluhm Exp $ */ 2 /* $NetBSD: kern_malloc.c,v 1.15.4.2 1996/06/13 17:10:56 cgd Exp $ */ 3 4 /* 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. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * @(#)kern_malloc.c 8.3 (Berkeley) 1/4/94 33 */ 34 35 #include <sys/param.h> 36 #include <sys/kernel.h> 37 #include <sys/malloc.h> 38 #include <sys/proc.h> 39 #include <sys/stdint.h> 40 #include <sys/systm.h> 41 #include <sys/sysctl.h> 42 #include <sys/time.h> 43 #include <sys/mutex.h> 44 #include <sys/rwlock.h> 45 46 #include <uvm/uvm_extern.h> 47 48 static 49 #ifndef SMALL_KERNEL 50 __inline__ 51 #endif 52 long BUCKETINDX(size_t sz) 53 { 54 long b, d; 55 56 /* note that this relies upon MINALLOCSIZE being 1 << MINBUCKET */ 57 b = 7 + MINBUCKET; d = 4; 58 while (d != 0) { 59 if (sz <= (1 << b)) 60 b -= d; 61 else 62 b += d; 63 d >>= 1; 64 } 65 if (sz <= (1 << b)) 66 b += 0; 67 else 68 b += 1; 69 return b; 70 } 71 72 static struct vm_map kmem_map_store; 73 struct vm_map *kmem_map = NULL; 74 75 /* 76 * Default number of pages in kmem_map. We attempt to calculate this 77 * at run-time, but allow it to be either patched or set in the kernel 78 * config file. 79 */ 80 #ifndef NKMEMPAGES 81 #define NKMEMPAGES 0 82 #endif 83 u_int nkmempages = NKMEMPAGES; 84 85 /* 86 * Defaults for lower- and upper-bounds for the kmem_map page count. 87 * Can be overridden by kernel config options. 88 */ 89 #ifndef NKMEMPAGES_MIN 90 #define NKMEMPAGES_MIN 0 91 #endif 92 u_int nkmempages_min = 0; 93 94 #ifndef NKMEMPAGES_MAX 95 #define NKMEMPAGES_MAX NKMEMPAGES_MAX_DEFAULT 96 #endif 97 u_int nkmempages_max = 0; 98 99 struct mutex malloc_mtx = MUTEX_INITIALIZER(IPL_VM); 100 struct kmembuckets bucket[MINBUCKET + 16]; 101 #ifdef KMEMSTATS 102 struct kmemstats kmemstats[M_LAST]; 103 #endif 104 struct kmemusage *kmemusage; 105 char *kmembase, *kmemlimit; 106 char buckstring[16 * sizeof("123456,")]; 107 int buckstring_init = 0; 108 #if defined(KMEMSTATS) || defined(DIAGNOSTIC) || defined(FFS_SOFTUPDATES) 109 char *memname[] = INITKMEMNAMES; 110 char *memall = NULL; 111 struct rwlock sysctl_kmemlock = RWLOCK_INITIALIZER("sysctlklk"); 112 #endif 113 114 /* 115 * Normally the freelist structure is used only to hold the list pointer 116 * for free objects. However, when running with diagnostics, the first 117 * 8 bytes of the structure is unused except for diagnostic information, 118 * and the free list pointer is at offset 8 in the structure. Since the 119 * first 8 bytes is the portion of the structure most often modified, this 120 * helps to detect memory reuse problems and avoid free list corruption. 121 */ 122 struct kmem_freelist { 123 int32_t kf_spare0; 124 int16_t kf_type; 125 int16_t kf_spare1; 126 XSIMPLEQ_ENTRY(kmem_freelist) kf_flist; 127 }; 128 129 #ifdef DIAGNOSTIC 130 /* 131 * This structure provides a set of masks to catch unaligned frees. 132 */ 133 const long addrmask[] = { 0, 134 0x00000001, 0x00000003, 0x00000007, 0x0000000f, 135 0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff, 136 0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff, 137 0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff, 138 }; 139 140 #endif /* DIAGNOSTIC */ 141 142 #ifndef SMALL_KERNEL 143 struct timeval malloc_errintvl = { 5, 0 }; 144 struct timeval malloc_lasterr; 145 #endif 146 147 /* 148 * Allocate a block of memory 149 */ 150 void * 151 malloc(size_t size, int type, int flags) 152 { 153 struct kmembuckets *kbp; 154 struct kmemusage *kup; 155 struct kmem_freelist *freep; 156 long indx, npg, allocsize; 157 caddr_t va, cp; 158 int s; 159 #ifdef DIAGNOSTIC 160 int freshalloc; 161 char *savedtype; 162 #endif 163 #ifdef KMEMSTATS 164 struct kmemstats *ksp = &kmemstats[type]; 165 int wake; 166 167 if (((unsigned long)type) <= 1 || ((unsigned long)type) >= M_LAST) 168 panic("malloc: bogus type %d", type); 169 #endif 170 171 KASSERT(flags & (M_WAITOK | M_NOWAIT)); 172 173 #ifdef DIAGNOSTIC 174 if ((flags & M_NOWAIT) == 0) { 175 extern int pool_debug; 176 assertwaitok(); 177 if (pool_debug == 2) 178 yield(); 179 } 180 #endif 181 182 if (size > 65535 * PAGE_SIZE) { 183 if (flags & M_CANFAIL) { 184 #ifndef SMALL_KERNEL 185 /* XXX lock */ 186 if (ratecheck(&malloc_lasterr, &malloc_errintvl)) 187 printf("malloc(): allocation too large, " 188 "type = %d, size = %lu\n", type, size); 189 #endif 190 return (NULL); 191 } else 192 panic("malloc: allocation too large, " 193 "type = %d, size = %lu\n", type, size); 194 } 195 196 indx = BUCKETINDX(size); 197 if (size > MAXALLOCSAVE) 198 allocsize = round_page(size); 199 else 200 allocsize = 1 << indx; 201 kbp = &bucket[indx]; 202 mtx_enter(&malloc_mtx); 203 #ifdef KMEMSTATS 204 while (ksp->ks_memuse >= ksp->ks_limit) { 205 if (flags & M_NOWAIT) { 206 mtx_leave(&malloc_mtx); 207 return (NULL); 208 } 209 #ifdef DIAGNOSTIC 210 if (ISSET(flags, M_WAITOK) && curproc == &proc0) 211 panic("%s: cannot sleep for memory during boot", 212 __func__); 213 #endif 214 if (ksp->ks_limblocks < 65535) 215 ksp->ks_limblocks++; 216 msleep(ksp, &malloc_mtx, PSWP+2, memname[type], 0); 217 } 218 ksp->ks_memuse += allocsize; /* account for this early */ 219 ksp->ks_size |= 1 << indx; 220 #endif 221 if (XSIMPLEQ_FIRST(&kbp->kb_freelist) == NULL) { 222 mtx_leave(&malloc_mtx); 223 npg = atop(round_page(allocsize)); 224 s = splvm(); 225 va = (caddr_t)uvm_km_kmemalloc_pla(kmem_map, NULL, 226 (vsize_t)ptoa(npg), 0, 227 ((flags & M_NOWAIT) ? UVM_KMF_NOWAIT : 0) | 228 ((flags & M_CANFAIL) ? UVM_KMF_CANFAIL : 0), 229 no_constraint.ucr_low, no_constraint.ucr_high, 230 0, 0, 0); 231 splx(s); 232 if (va == NULL) { 233 /* 234 * Kmem_malloc() can return NULL, even if it can 235 * wait, if there is no map space available, because 236 * it can't fix that problem. Neither can we, 237 * right now. (We should release pages which 238 * are completely free and which are in buckets 239 * with too many free elements.) 240 */ 241 if ((flags & (M_NOWAIT|M_CANFAIL)) == 0) 242 panic("malloc: out of space in kmem_map"); 243 244 #ifdef KMEMSTATS 245 mtx_enter(&malloc_mtx); 246 ksp->ks_memuse -= allocsize; 247 wake = ksp->ks_memuse + allocsize >= ksp->ks_limit && 248 ksp->ks_memuse < ksp->ks_limit; 249 mtx_leave(&malloc_mtx); 250 if (wake) 251 wakeup(ksp); 252 #endif 253 254 return (NULL); 255 } 256 mtx_enter(&malloc_mtx); 257 #ifdef KMEMSTATS 258 kbp->kb_total += kbp->kb_elmpercl; 259 #endif 260 kup = btokup(va); 261 kup->ku_indx = indx; 262 #ifdef DIAGNOSTIC 263 freshalloc = 1; 264 #endif 265 if (allocsize > MAXALLOCSAVE) { 266 kup->ku_pagecnt = npg; 267 goto out; 268 } 269 #ifdef KMEMSTATS 270 kup->ku_freecnt = kbp->kb_elmpercl; 271 kbp->kb_totalfree += kbp->kb_elmpercl; 272 #endif 273 cp = va + (npg * PAGE_SIZE) - allocsize; 274 for (;;) { 275 freep = (struct kmem_freelist *)cp; 276 #ifdef DIAGNOSTIC 277 /* 278 * Copy in known text to detect modification 279 * after freeing. 280 */ 281 poison_mem(cp, allocsize); 282 freep->kf_type = M_FREE; 283 #endif /* DIAGNOSTIC */ 284 XSIMPLEQ_INSERT_HEAD(&kbp->kb_freelist, freep, kf_flist); 285 if (cp <= va) 286 break; 287 cp -= allocsize; 288 } 289 } else { 290 #ifdef DIAGNOSTIC 291 freshalloc = 0; 292 #endif 293 } 294 freep = XSIMPLEQ_FIRST(&kbp->kb_freelist); 295 XSIMPLEQ_REMOVE_HEAD(&kbp->kb_freelist, kf_flist); 296 va = (caddr_t)freep; 297 #ifdef DIAGNOSTIC 298 savedtype = (unsigned)freep->kf_type < M_LAST ? 299 memname[freep->kf_type] : "???"; 300 if (freshalloc == 0 && XSIMPLEQ_FIRST(&kbp->kb_freelist)) { 301 int rv; 302 vaddr_t addr = (vaddr_t)XSIMPLEQ_FIRST(&kbp->kb_freelist); 303 304 vm_map_lock(kmem_map); 305 rv = uvm_map_checkprot(kmem_map, addr, 306 addr + sizeof(struct kmem_freelist), PROT_WRITE); 307 vm_map_unlock(kmem_map); 308 309 if (!rv) { 310 printf("%s %zd of object %p size 0x%lx %s %s" 311 " (invalid addr %p)\n", 312 "Data modified on freelist: word", 313 (int32_t *)&addr - (int32_t *)kbp, va, size, 314 "previous type", savedtype, (void *)addr); 315 } 316 } 317 318 /* Fill the fields that we've used with poison */ 319 poison_mem(freep, sizeof(*freep)); 320 321 /* and check that the data hasn't been modified. */ 322 if (freshalloc == 0) { 323 size_t pidx; 324 uint32_t pval; 325 if (poison_check(va, allocsize, &pidx, &pval)) { 326 panic("%s %zd of object %p size 0x%lx %s %s" 327 " (0x%x != 0x%x)\n", 328 "Data modified on freelist: word", 329 pidx, va, size, "previous type", 330 savedtype, ((int32_t*)va)[pidx], pval); 331 } 332 } 333 334 freep->kf_spare0 = 0; 335 #endif /* DIAGNOSTIC */ 336 #ifdef KMEMSTATS 337 kup = btokup(va); 338 if (kup->ku_indx != indx) 339 panic("malloc: wrong bucket"); 340 if (kup->ku_freecnt == 0) 341 panic("malloc: lost data"); 342 kup->ku_freecnt--; 343 kbp->kb_totalfree--; 344 out: 345 kbp->kb_calls++; 346 ksp->ks_inuse++; 347 ksp->ks_calls++; 348 if (ksp->ks_memuse > ksp->ks_maxused) 349 ksp->ks_maxused = ksp->ks_memuse; 350 #else 351 out: 352 #endif 353 mtx_leave(&malloc_mtx); 354 355 if ((flags & M_ZERO) && va != NULL) 356 memset(va, 0, size); 357 return (va); 358 } 359 360 /* 361 * Free a block of memory allocated by malloc. 362 */ 363 void 364 free(void *addr, int type, size_t freedsize) 365 { 366 struct kmembuckets *kbp; 367 struct kmemusage *kup; 368 struct kmem_freelist *freep; 369 long size; 370 int s; 371 #ifdef DIAGNOSTIC 372 long alloc; 373 #endif 374 #ifdef KMEMSTATS 375 struct kmemstats *ksp = &kmemstats[type]; 376 #endif 377 378 if (addr == NULL) 379 return; 380 381 #ifdef DIAGNOSTIC 382 if (addr < (void *)kmembase || addr >= (void *)kmemlimit) 383 panic("free: non-malloced addr %p type %s", addr, 384 memname[type]); 385 #endif 386 387 kup = btokup(addr); 388 size = 1 << kup->ku_indx; 389 kbp = &bucket[kup->ku_indx]; 390 if (size > MAXALLOCSAVE) 391 size = kup->ku_pagecnt << PAGE_SHIFT; 392 #ifdef DIAGNOSTIC 393 if (freedsize != 0 && freedsize > size) 394 panic("free: size too large %zu > %ld (%p) type %s", 395 freedsize, size, addr, memname[type]); 396 if (freedsize != 0 && size > MINALLOCSIZE && freedsize <= size / 2) 397 panic("free: size too small %zu <= %ld / 2 (%p) type %s", 398 freedsize, size, addr, memname[type]); 399 /* 400 * Check for returns of data that do not point to the 401 * beginning of the allocation. 402 */ 403 if (size > PAGE_SIZE) 404 alloc = addrmask[BUCKETINDX(PAGE_SIZE)]; 405 else 406 alloc = addrmask[kup->ku_indx]; 407 if (((u_long)addr & alloc) != 0) 408 panic("free: unaligned addr %p, size %ld, type %s, mask %ld", 409 addr, size, memname[type], alloc); 410 #endif /* DIAGNOSTIC */ 411 if (size > MAXALLOCSAVE) { 412 s = splvm(); 413 uvm_km_free(kmem_map, (vaddr_t)addr, ptoa(kup->ku_pagecnt)); 414 splx(s); 415 #ifdef KMEMSTATS 416 mtx_enter(&malloc_mtx); 417 ksp->ks_memuse -= size; 418 kup->ku_indx = 0; 419 kup->ku_pagecnt = 0; 420 if (ksp->ks_memuse + size >= ksp->ks_limit && 421 ksp->ks_memuse < ksp->ks_limit) 422 wakeup(ksp); 423 ksp->ks_inuse--; 424 kbp->kb_total -= 1; 425 mtx_leave(&malloc_mtx); 426 #endif 427 return; 428 } 429 freep = (struct kmem_freelist *)addr; 430 mtx_enter(&malloc_mtx); 431 #ifdef DIAGNOSTIC 432 /* 433 * Check for multiple frees. Use a quick check to see if 434 * it looks free before laboriously searching the freelist. 435 */ 436 if (freep->kf_spare0 == poison_value(freep)) { 437 struct kmem_freelist *fp; 438 XSIMPLEQ_FOREACH(fp, &kbp->kb_freelist, kf_flist) { 439 if (addr != fp) 440 continue; 441 printf("multiply freed item %p\n", addr); 442 panic("free: duplicated free"); 443 } 444 } 445 /* 446 * Copy in known text to detect modification after freeing 447 * and to make it look free. Also, save the type being freed 448 * so we can list likely culprit if modification is detected 449 * when the object is reallocated. 450 */ 451 poison_mem(addr, size); 452 freep->kf_spare0 = poison_value(freep); 453 454 freep->kf_type = type; 455 #endif /* DIAGNOSTIC */ 456 #ifdef KMEMSTATS 457 kup->ku_freecnt++; 458 if (kup->ku_freecnt >= kbp->kb_elmpercl) { 459 if (kup->ku_freecnt > kbp->kb_elmpercl) 460 panic("free: multiple frees"); 461 else if (kbp->kb_totalfree > kbp->kb_highwat) 462 kbp->kb_couldfree++; 463 } 464 kbp->kb_totalfree++; 465 ksp->ks_memuse -= size; 466 if (ksp->ks_memuse + size >= ksp->ks_limit && 467 ksp->ks_memuse < ksp->ks_limit) 468 wakeup(ksp); 469 ksp->ks_inuse--; 470 #endif 471 XSIMPLEQ_INSERT_TAIL(&kbp->kb_freelist, freep, kf_flist); 472 mtx_leave(&malloc_mtx); 473 } 474 475 /* 476 * Compute the number of pages that kmem_map will map, that is, 477 * the size of the kernel malloc arena. 478 */ 479 void 480 kmeminit_nkmempages(void) 481 { 482 u_int npages; 483 484 if (nkmempages != 0) { 485 /* 486 * It's already been set (by us being here before, or 487 * by patching or kernel config options), bail out now. 488 */ 489 return; 490 } 491 492 /* 493 * We can't initialize these variables at compilation time, since 494 * the page size may not be known (on sparc GENERIC kernels, for 495 * example). But we still want the MD code to be able to provide 496 * better values. 497 */ 498 if (nkmempages_min == 0) 499 nkmempages_min = NKMEMPAGES_MIN; 500 if (nkmempages_max == 0) 501 nkmempages_max = NKMEMPAGES_MAX; 502 503 /* 504 * We use the following (simple) formula: 505 * 506 * - Starting point is physical memory / 4. 507 * 508 * - Clamp it down to nkmempages_max. 509 * 510 * - Round it up to nkmempages_min. 511 */ 512 npages = physmem / 4; 513 514 if (npages > nkmempages_max) 515 npages = nkmempages_max; 516 517 if (npages < nkmempages_min) 518 npages = nkmempages_min; 519 520 nkmempages = npages; 521 } 522 523 /* 524 * Initialize the kernel memory allocator 525 */ 526 void 527 kmeminit(void) 528 { 529 vaddr_t base, limit; 530 long indx; 531 532 #ifdef DIAGNOSTIC 533 if (sizeof(struct kmem_freelist) > (1 << MINBUCKET)) 534 panic("kmeminit: minbucket too small/struct freelist too big"); 535 #endif 536 537 /* 538 * Compute the number of kmem_map pages, if we have not 539 * done so already. 540 */ 541 kmeminit_nkmempages(); 542 base = vm_map_min(kernel_map); 543 kmem_map = uvm_km_suballoc(kernel_map, &base, &limit, 544 (vsize_t)nkmempages << PAGE_SHIFT, 545 #ifdef KVA_GUARDPAGES 546 VM_MAP_INTRSAFE | VM_MAP_GUARDPAGES, 547 #else 548 VM_MAP_INTRSAFE, 549 #endif 550 FALSE, &kmem_map_store); 551 kmembase = (char *)base; 552 kmemlimit = (char *)limit; 553 kmemusage = (struct kmemusage *) uvm_km_zalloc(kernel_map, 554 (vsize_t)(nkmempages * sizeof(struct kmemusage))); 555 for (indx = 0; indx < MINBUCKET + 16; indx++) { 556 XSIMPLEQ_INIT(&bucket[indx].kb_freelist); 557 } 558 #ifdef KMEMSTATS 559 for (indx = 0; indx < MINBUCKET + 16; indx++) { 560 if (1 << indx >= PAGE_SIZE) 561 bucket[indx].kb_elmpercl = 1; 562 else 563 bucket[indx].kb_elmpercl = PAGE_SIZE / (1 << indx); 564 bucket[indx].kb_highwat = 5 * bucket[indx].kb_elmpercl; 565 } 566 for (indx = 0; indx < M_LAST; indx++) 567 kmemstats[indx].ks_limit = nkmempages * PAGE_SIZE * 6 / 10; 568 #endif 569 } 570 571 /* 572 * Return kernel malloc statistics information. 573 */ 574 int 575 sysctl_malloc(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp, 576 size_t newlen, struct proc *p) 577 { 578 struct kmembuckets kb; 579 #ifdef KMEMSTATS 580 struct kmemstats km; 581 #endif 582 #if defined(KMEMSTATS) || defined(DIAGNOSTIC) || defined(FFS_SOFTUPDATES) 583 int error; 584 #endif 585 int i, siz; 586 587 if (namelen != 2 && name[0] != KERN_MALLOC_BUCKETS && 588 name[0] != KERN_MALLOC_KMEMNAMES) 589 return (ENOTDIR); /* overloaded */ 590 591 switch (name[0]) { 592 case KERN_MALLOC_BUCKETS: 593 /* Initialize the first time */ 594 if (buckstring_init == 0) { 595 buckstring_init = 1; 596 memset(buckstring, 0, sizeof(buckstring)); 597 for (siz = 0, i = MINBUCKET; i < MINBUCKET + 16; i++) { 598 snprintf(buckstring + siz, 599 sizeof buckstring - siz, 600 "%d,", (u_int)(1<<i)); 601 siz += strlen(buckstring + siz); 602 } 603 /* Remove trailing comma */ 604 if (siz) 605 buckstring[siz - 1] = '\0'; 606 } 607 return (sysctl_rdstring(oldp, oldlenp, newp, buckstring)); 608 609 case KERN_MALLOC_BUCKET: 610 mtx_enter(&malloc_mtx); 611 memcpy(&kb, &bucket[BUCKETINDX(name[1])], sizeof(kb)); 612 mtx_leave(&malloc_mtx); 613 memset(&kb.kb_freelist, 0, sizeof(kb.kb_freelist)); 614 return (sysctl_rdstruct(oldp, oldlenp, newp, &kb, sizeof(kb))); 615 case KERN_MALLOC_KMEMSTATS: 616 #ifdef KMEMSTATS 617 if ((name[1] < 0) || (name[1] >= M_LAST)) 618 return (EINVAL); 619 mtx_enter(&malloc_mtx); 620 memcpy(&km, &kmemstats[name[1]], sizeof(km)); 621 mtx_leave(&malloc_mtx); 622 return (sysctl_rdstruct(oldp, oldlenp, newp, &km, sizeof(km))); 623 #else 624 return (EOPNOTSUPP); 625 #endif 626 case KERN_MALLOC_KMEMNAMES: 627 #if defined(KMEMSTATS) || defined(DIAGNOSTIC) || defined(FFS_SOFTUPDATES) 628 error = rw_enter(&sysctl_kmemlock, RW_WRITE|RW_INTR); 629 if (error) 630 return (error); 631 if (memall == NULL) { 632 int totlen; 633 634 /* Figure out how large a buffer we need */ 635 for (totlen = 0, i = 0; i < M_LAST; i++) { 636 if (memname[i]) 637 totlen += strlen(memname[i]); 638 totlen++; 639 } 640 memall = malloc(totlen + M_LAST, M_SYSCTL, 641 M_WAITOK|M_ZERO); 642 for (siz = 0, i = 0; i < M_LAST; i++) { 643 snprintf(memall + siz, 644 totlen + M_LAST - siz, 645 "%s,", memname[i] ? memname[i] : ""); 646 siz += strlen(memall + siz); 647 } 648 /* Remove trailing comma */ 649 if (siz) 650 memall[siz - 1] = '\0'; 651 652 /* Now, convert all spaces to underscores */ 653 for (i = 0; i < totlen; i++) 654 if (memall[i] == ' ') 655 memall[i] = '_'; 656 } 657 rw_exit_write(&sysctl_kmemlock); 658 return (sysctl_rdstring(oldp, oldlenp, newp, memall)); 659 #else 660 return (EOPNOTSUPP); 661 #endif 662 default: 663 return (EOPNOTSUPP); 664 } 665 /* NOTREACHED */ 666 } 667 668 /* 669 * Round up a size to how much malloc would actually allocate. 670 */ 671 size_t 672 malloc_roundup(size_t sz) 673 { 674 if (sz > MAXALLOCSAVE) 675 return round_page(sz); 676 677 return (1 << BUCKETINDX(sz)); 678 } 679 680 #if defined(DDB) 681 #include <machine/db_machdep.h> 682 #include <ddb/db_output.h> 683 684 void 685 malloc_printit( 686 int (*pr)(const char *, ...) __attribute__((__format__(__kprintf__,1,2)))) 687 { 688 #ifdef KMEMSTATS 689 struct kmemstats *km; 690 int i; 691 692 (*pr)("%15s %5s %6s %7s %6s %9s %8s %8s\n", 693 "Type", "InUse", "MemUse", "HighUse", "Limit", "Requests", 694 "Type Lim", "Kern Lim"); 695 for (i = 0, km = kmemstats; i < M_LAST; i++, km++) { 696 if (!km->ks_calls || !memname[i]) 697 continue; 698 699 (*pr)("%15s %5ld %6ldK %7ldK %6ldK %9ld %8d %8d\n", 700 memname[i], km->ks_inuse, km->ks_memuse / 1024, 701 km->ks_maxused / 1024, km->ks_limit / 1024, 702 km->ks_calls, km->ks_limblocks, km->ks_mapblocks); 703 } 704 #else 705 (*pr)("No KMEMSTATS compiled in\n"); 706 #endif 707 } 708 #endif /* DDB */ 709 710 /* 711 * Copyright (c) 2008 Otto Moerbeek <otto@drijf.net> 712 * 713 * Permission to use, copy, modify, and distribute this software for any 714 * purpose with or without fee is hereby granted, provided that the above 715 * copyright notice and this permission notice appear in all copies. 716 * 717 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 718 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 719 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 720 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 721 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 722 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 723 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 724 */ 725 726 /* 727 * This is sqrt(SIZE_MAX+1), as s1*s2 <= SIZE_MAX 728 * if both s1 < MUL_NO_OVERFLOW and s2 < MUL_NO_OVERFLOW 729 */ 730 #define MUL_NO_OVERFLOW (1UL << (sizeof(size_t) * 4)) 731 732 void * 733 mallocarray(size_t nmemb, size_t size, int type, int flags) 734 { 735 if ((nmemb >= MUL_NO_OVERFLOW || size >= MUL_NO_OVERFLOW) && 736 nmemb > 0 && SIZE_MAX / nmemb < size) { 737 if (flags & M_CANFAIL) 738 return (NULL); 739 panic("mallocarray: overflow %zu * %zu", nmemb, size); 740 } 741 return (malloc(size * nmemb, type, flags)); 742 } 743