kern_malloc.c revision 65649
1/* 2 * Copyright (c) 1987, 1991, 1993 3 * The Regents of the University of California. 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 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)kern_malloc.c 8.3 (Berkeley) 1/4/94 34 * $FreeBSD: head/sys/kern/kern_malloc.c 65649 2000-09-09 22:27:35Z jasone $ 35 */ 36 37#include "opt_vm.h" 38 39#include <sys/param.h> 40#include <sys/systm.h> 41#include <sys/kernel.h> 42#include <sys/malloc.h> 43#include <sys/mbuf.h> 44#include <sys/vmmeter.h> 45#include <sys/lock.h> 46#include <sys/proc.h> 47#include <machine/mutex.h> 48 49#include <vm/vm.h> 50#include <vm/vm_param.h> 51#include <vm/vm_kern.h> 52#include <vm/vm_extern.h> 53#include <vm/pmap.h> 54#include <vm/vm_map.h> 55 56#if defined(INVARIANTS) && defined(__i386__) 57#include <machine/cpu.h> 58#endif 59 60MALLOC_DEFINE(M_CACHE, "cache", "Various Dynamically allocated caches"); 61MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory"); 62MALLOC_DEFINE(M_TEMP, "temp", "misc temporary data buffers"); 63 64MALLOC_DEFINE(M_IP6OPT, "ip6opt", "IPv6 options"); 65MALLOC_DEFINE(M_IP6NDP, "ip6ndp", "IPv6 Neighbor Discovery"); 66 67static void kmeminit __P((void *)); 68SYSINIT(kmem, SI_SUB_KMEM, SI_ORDER_FIRST, kmeminit, NULL) 69 70static MALLOC_DEFINE(M_FREE, "free", "should be on free list"); 71 72static struct malloc_type *kmemstatistics; 73static struct kmembuckets bucket[MINBUCKET + 16]; 74static struct kmemusage *kmemusage; 75static char *kmembase; 76static char *kmemlimit; 77 78static mtx_t malloc_mtx; 79 80u_int vm_kmem_size; 81 82#ifdef INVARIANTS 83/* 84 * This structure provides a set of masks to catch unaligned frees. 85 */ 86static long addrmask[] = { 0, 87 0x00000001, 0x00000003, 0x00000007, 0x0000000f, 88 0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff, 89 0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff, 90 0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff, 91}; 92 93/* 94 * The WEIRD_ADDR is used as known text to copy into free objects so 95 * that modifications after frees can be detected. 96 */ 97#define WEIRD_ADDR 0xdeadc0de 98#define MAX_COPY 64 99 100/* 101 * Normally the first word of the structure is used to hold the list 102 * pointer for free objects. However, when running with diagnostics, 103 * we use the third and fourth fields, so as to catch modifications 104 * in the most commonly trashed first two words. 105 */ 106struct freelist { 107 long spare0; 108 struct malloc_type *type; 109 long spare1; 110 caddr_t next; 111}; 112#else /* !INVARIANTS */ 113struct freelist { 114 caddr_t next; 115}; 116#endif /* INVARIANTS */ 117 118/* 119 * malloc: 120 * 121 * Allocate a block of memory. 122 * 123 * If M_NOWAIT is set, this routine will not block and return NULL if 124 * the allocation fails. 125 * 126 * If M_ASLEEP is set (M_NOWAIT must also be set), this routine 127 * will have the side effect of calling asleep() if it returns NULL, 128 * allowing the parent to await() at some future time. 129 */ 130void * 131malloc(size, type, flags) 132 unsigned long size; 133 struct malloc_type *type; 134 int flags; 135{ 136 register struct kmembuckets *kbp; 137 register struct kmemusage *kup; 138 register struct freelist *freep; 139 long indx, npg, allocsize; 140 int s; 141 caddr_t va, cp, savedlist; 142#ifdef INVARIANTS 143 long *end, *lp; 144 int copysize; 145 const char *savedtype; 146#endif 147 register struct malloc_type *ksp = type; 148 149#if defined(INVARIANTS) && defined(__i386__) 150 if (flags == M_WAITOK) 151 KASSERT(intr_nesting_level == 0, 152 ("malloc(M_WAITOK) in interrupt context")); 153#endif 154 indx = BUCKETINDX(size); 155 kbp = &bucket[indx]; 156 s = splmem(); 157 mtx_enter(&malloc_mtx, MTX_DEF); 158 while (ksp->ks_memuse >= ksp->ks_limit) { 159 if (flags & M_ASLEEP) { 160 if (ksp->ks_limblocks < 65535) 161 ksp->ks_limblocks++; 162 asleep((caddr_t)ksp, PSWP+2, type->ks_shortdesc, 0); 163 } 164 if (flags & M_NOWAIT) { 165 splx(s); 166 mtx_exit(&malloc_mtx, MTX_DEF); 167 return ((void *) NULL); 168 } 169 if (ksp->ks_limblocks < 65535) 170 ksp->ks_limblocks++; 171 tsleep((caddr_t)ksp, PSWP+2, type->ks_shortdesc, 0); 172 } 173 ksp->ks_size |= 1 << indx; 174#ifdef INVARIANTS 175 copysize = 1 << indx < MAX_COPY ? 1 << indx : MAX_COPY; 176#endif 177 if (kbp->kb_next == NULL) { 178 kbp->kb_last = NULL; 179 if (size > MAXALLOCSAVE) 180 allocsize = roundup(size, PAGE_SIZE); 181 else 182 allocsize = 1 << indx; 183 npg = btoc(allocsize); 184 va = (caddr_t) kmem_malloc(kmem_map, (vm_size_t)ctob(npg), flags); 185 if (va == NULL) { 186 splx(s); 187 mtx_exit(&malloc_mtx, MTX_DEF); 188 return ((void *) NULL); 189 } 190 kbp->kb_total += kbp->kb_elmpercl; 191 kup = btokup(va); 192 kup->ku_indx = indx; 193 if (allocsize > MAXALLOCSAVE) { 194 if (npg > 65535) 195 panic("malloc: allocation too large"); 196 kup->ku_pagecnt = npg; 197 ksp->ks_memuse += allocsize; 198 goto out; 199 } 200 kup->ku_freecnt = kbp->kb_elmpercl; 201 kbp->kb_totalfree += kbp->kb_elmpercl; 202 /* 203 * Just in case we blocked while allocating memory, 204 * and someone else also allocated memory for this 205 * bucket, don't assume the list is still empty. 206 */ 207 savedlist = kbp->kb_next; 208 kbp->kb_next = cp = va + (npg * PAGE_SIZE) - allocsize; 209 for (;;) { 210 freep = (struct freelist *)cp; 211#ifdef INVARIANTS 212 /* 213 * Copy in known text to detect modification 214 * after freeing. 215 */ 216 end = (long *)&cp[copysize]; 217 for (lp = (long *)cp; lp < end; lp++) 218 *lp = WEIRD_ADDR; 219 freep->type = M_FREE; 220#endif /* INVARIANTS */ 221 if (cp <= va) 222 break; 223 cp -= allocsize; 224 freep->next = cp; 225 } 226 freep->next = savedlist; 227 if (kbp->kb_last == NULL) 228 kbp->kb_last = (caddr_t)freep; 229 } 230 va = kbp->kb_next; 231 kbp->kb_next = ((struct freelist *)va)->next; 232#ifdef INVARIANTS 233 freep = (struct freelist *)va; 234 savedtype = (const char *) freep->type->ks_shortdesc; 235#if BYTE_ORDER == BIG_ENDIAN 236 freep->type = (struct malloc_type *)WEIRD_ADDR >> 16; 237#endif 238#if BYTE_ORDER == LITTLE_ENDIAN 239 freep->type = (struct malloc_type *)WEIRD_ADDR; 240#endif 241 if ((intptr_t)(void *)&freep->next & 0x2) 242 freep->next = (caddr_t)((WEIRD_ADDR >> 16)|(WEIRD_ADDR << 16)); 243 else 244 freep->next = (caddr_t)WEIRD_ADDR; 245 end = (long *)&va[copysize]; 246 for (lp = (long *)va; lp < end; lp++) { 247 if (*lp == WEIRD_ADDR) 248 continue; 249 printf("%s %ld of object %p size %lu %s %s (0x%lx != 0x%lx)\n", 250 "Data modified on freelist: word", 251 (long)(lp - (long *)va), (void *)va, size, 252 "previous type", savedtype, *lp, (u_long)WEIRD_ADDR); 253 break; 254 } 255 freep->spare0 = 0; 256#endif /* INVARIANTS */ 257 kup = btokup(va); 258 if (kup->ku_indx != indx) 259 panic("malloc: wrong bucket"); 260 if (kup->ku_freecnt == 0) 261 panic("malloc: lost data"); 262 kup->ku_freecnt--; 263 kbp->kb_totalfree--; 264 ksp->ks_memuse += 1 << indx; 265out: 266 kbp->kb_calls++; 267 ksp->ks_inuse++; 268 ksp->ks_calls++; 269 if (ksp->ks_memuse > ksp->ks_maxused) 270 ksp->ks_maxused = ksp->ks_memuse; 271 splx(s); 272 mtx_exit(&malloc_mtx, MTX_DEF); 273 return ((void *) va); 274} 275 276/* 277 * free: 278 * 279 * Free a block of memory allocated by malloc. 280 * 281 * This routine may not block. 282 */ 283void 284free(addr, type) 285 void *addr; 286 struct malloc_type *type; 287{ 288 register struct kmembuckets *kbp; 289 register struct kmemusage *kup; 290 register struct freelist *freep; 291 long size; 292 int s; 293#ifdef INVARIANTS 294 struct freelist *fp; 295 long *end, *lp, alloc, copysize; 296#endif 297 register struct malloc_type *ksp = type; 298 299 KASSERT(kmembase <= (char *)addr && (char *)addr < kmemlimit, 300 ("free: address %p out of range", (void *)addr)); 301 kup = btokup(addr); 302 size = 1 << kup->ku_indx; 303 kbp = &bucket[kup->ku_indx]; 304 s = splmem(); 305 mtx_enter(&malloc_mtx, MTX_DEF); 306#ifdef INVARIANTS 307 /* 308 * Check for returns of data that do not point to the 309 * beginning of the allocation. 310 */ 311 if (size > PAGE_SIZE) 312 alloc = addrmask[BUCKETINDX(PAGE_SIZE)]; 313 else 314 alloc = addrmask[kup->ku_indx]; 315 if (((uintptr_t)(void *)addr & alloc) != 0) 316 panic("free: unaligned addr %p, size %ld, type %s, mask %ld", 317 (void *)addr, size, type->ks_shortdesc, alloc); 318#endif /* INVARIANTS */ 319 if (size > MAXALLOCSAVE) { 320 kmem_free(kmem_map, (vm_offset_t)addr, ctob(kup->ku_pagecnt)); 321 size = kup->ku_pagecnt << PAGE_SHIFT; 322 ksp->ks_memuse -= size; 323 kup->ku_indx = 0; 324 kup->ku_pagecnt = 0; 325 if (ksp->ks_memuse + size >= ksp->ks_limit && 326 ksp->ks_memuse < ksp->ks_limit) 327 wakeup((caddr_t)ksp); 328 ksp->ks_inuse--; 329 kbp->kb_total -= 1; 330 splx(s); 331 mtx_exit(&malloc_mtx, MTX_DEF); 332 return; 333 } 334 freep = (struct freelist *)addr; 335#ifdef INVARIANTS 336 /* 337 * Check for multiple frees. Use a quick check to see if 338 * it looks free before laboriously searching the freelist. 339 */ 340 if (freep->spare0 == WEIRD_ADDR) { 341 fp = (struct freelist *)kbp->kb_next; 342 while (fp) { 343 if (fp->spare0 != WEIRD_ADDR) 344 panic("free: free item %p modified", fp); 345 else if (addr == (caddr_t)fp) 346 panic("free: multiple freed item %p", addr); 347 fp = (struct freelist *)fp->next; 348 } 349 } 350 /* 351 * Copy in known text to detect modification after freeing 352 * and to make it look free. Also, save the type being freed 353 * so we can list likely culprit if modification is detected 354 * when the object is reallocated. 355 */ 356 copysize = size < MAX_COPY ? size : MAX_COPY; 357 end = (long *)&((caddr_t)addr)[copysize]; 358 for (lp = (long *)addr; lp < end; lp++) 359 *lp = WEIRD_ADDR; 360 freep->type = type; 361#endif /* INVARIANTS */ 362 kup->ku_freecnt++; 363 if (kup->ku_freecnt >= kbp->kb_elmpercl) { 364 if (kup->ku_freecnt > kbp->kb_elmpercl) 365 panic("free: multiple frees"); 366 else if (kbp->kb_totalfree > kbp->kb_highwat) 367 kbp->kb_couldfree++; 368 } 369 kbp->kb_totalfree++; 370 ksp->ks_memuse -= size; 371 if (ksp->ks_memuse + size >= ksp->ks_limit && 372 ksp->ks_memuse < ksp->ks_limit) 373 wakeup((caddr_t)ksp); 374 ksp->ks_inuse--; 375#ifdef OLD_MALLOC_MEMORY_POLICY 376 if (kbp->kb_next == NULL) 377 kbp->kb_next = addr; 378 else 379 ((struct freelist *)kbp->kb_last)->next = addr; 380 freep->next = NULL; 381 kbp->kb_last = addr; 382#else 383 /* 384 * Return memory to the head of the queue for quick reuse. This 385 * can improve performance by improving the probability of the 386 * item being in the cache when it is reused. 387 */ 388 if (kbp->kb_next == NULL) { 389 kbp->kb_next = addr; 390 kbp->kb_last = addr; 391 freep->next = NULL; 392 } else { 393 freep->next = kbp->kb_next; 394 kbp->kb_next = addr; 395 } 396#endif 397 splx(s); 398 mtx_exit(&malloc_mtx, MTX_DEF); 399} 400 401/* 402 * Initialize the kernel memory allocator 403 */ 404/* ARGSUSED*/ 405static void 406kmeminit(dummy) 407 void *dummy; 408{ 409 register long indx; 410 u_long npg; 411 u_long mem_size; 412 u_long xvm_kmem_size; 413 414#if ((MAXALLOCSAVE & (MAXALLOCSAVE - 1)) != 0) 415#error "kmeminit: MAXALLOCSAVE not power of 2" 416#endif 417#if (MAXALLOCSAVE > MINALLOCSIZE * 32768) 418#error "kmeminit: MAXALLOCSAVE too big" 419#endif 420#if (MAXALLOCSAVE < PAGE_SIZE) 421#error "kmeminit: MAXALLOCSAVE too small" 422#endif 423 424 mtx_init(&malloc_mtx, "malloc", MTX_DEF); 425 426 /* 427 * Try to auto-tune the kernel memory size, so that it is 428 * more applicable for a wider range of machine sizes. 429 * On an X86, a VM_KMEM_SIZE_SCALE value of 4 is good, while 430 * a VM_KMEM_SIZE of 12MB is a fair compromise. The 431 * VM_KMEM_SIZE_MAX is dependent on the maximum KVA space 432 * available, and on an X86 with a total KVA space of 256MB, 433 * try to keep VM_KMEM_SIZE_MAX at 80MB or below. 434 * 435 * Note that the kmem_map is also used by the zone allocator, 436 * so make sure that there is enough space. 437 */ 438 xvm_kmem_size = VM_KMEM_SIZE; 439 mem_size = cnt.v_page_count * PAGE_SIZE; 440 441#if defined(VM_KMEM_SIZE_SCALE) 442 if ((mem_size / VM_KMEM_SIZE_SCALE) > xvm_kmem_size) 443 xvm_kmem_size = mem_size / VM_KMEM_SIZE_SCALE; 444#endif 445 446#if defined(VM_KMEM_SIZE_MAX) 447 if (xvm_kmem_size >= VM_KMEM_SIZE_MAX) 448 xvm_kmem_size = VM_KMEM_SIZE_MAX; 449#endif 450 451 /* Allow final override from the kernel environment */ 452 TUNABLE_INT_FETCH("kern.vm.kmem.size", xvm_kmem_size, vm_kmem_size); 453 454 /* 455 * Limit kmem virtual size to twice the physical memory. 456 * This allows for kmem map sparseness, but limits the size 457 * to something sane. Be careful to not overflow the 32bit 458 * ints while doing the check. 459 */ 460 if ((vm_kmem_size / 2) > (cnt.v_page_count * PAGE_SIZE)) 461 vm_kmem_size = 2 * cnt.v_page_count * PAGE_SIZE; 462 463 npg = (nmbufs * MSIZE + nmbclusters * MCLBYTES + vm_kmem_size) 464 / PAGE_SIZE; 465 466 kmemusage = (struct kmemusage *) kmem_alloc(kernel_map, 467 (vm_size_t)(npg * sizeof(struct kmemusage))); 468 kmem_map = kmem_suballoc(kernel_map, (vm_offset_t *)&kmembase, 469 (vm_offset_t *)&kmemlimit, (vm_size_t)(npg * PAGE_SIZE)); 470 kmem_map->system_map = 1; 471 for (indx = 0; indx < MINBUCKET + 16; indx++) { 472 if (1 << indx >= PAGE_SIZE) 473 bucket[indx].kb_elmpercl = 1; 474 else 475 bucket[indx].kb_elmpercl = PAGE_SIZE / (1 << indx); 476 bucket[indx].kb_highwat = 5 * bucket[indx].kb_elmpercl; 477 } 478} 479 480void 481malloc_init(data) 482 void *data; 483{ 484 struct malloc_type *type = (struct malloc_type *)data; 485 486 if (type->ks_magic != M_MAGIC) 487 panic("malloc type lacks magic"); 488 489 if (type->ks_limit != 0) 490 return; 491 492 if (cnt.v_page_count == 0) 493 panic("malloc_init not allowed before vm init"); 494 495 /* 496 * The default limits for each malloc region is 1/2 of the 497 * malloc portion of the kmem map size. 498 */ 499 type->ks_limit = vm_kmem_size / 2; 500 type->ks_next = kmemstatistics; 501 kmemstatistics = type; 502} 503 504void 505malloc_uninit(data) 506 void *data; 507{ 508 struct malloc_type *type = (struct malloc_type *)data; 509 struct malloc_type *t; 510#ifdef INVARIANTS 511 struct kmembuckets *kbp; 512 struct freelist *freep; 513 long indx; 514 int s; 515#endif 516 517 if (type->ks_magic != M_MAGIC) 518 panic("malloc type lacks magic"); 519 520 if (cnt.v_page_count == 0) 521 panic("malloc_uninit not allowed before vm init"); 522 523 if (type->ks_limit == 0) 524 panic("malloc_uninit on uninitialized type"); 525 526#ifdef INVARIANTS 527 s = splmem(); 528 mtx_enter(&malloc_mtx, MTX_DEF); 529 for (indx = 0; indx < MINBUCKET + 16; indx++) { 530 kbp = bucket + indx; 531 freep = (struct freelist*)kbp->kb_next; 532 while (freep) { 533 if (freep->type == type) 534 freep->type = M_FREE; 535 freep = (struct freelist*)freep->next; 536 } 537 } 538 splx(s); 539 mtx_exit(&malloc_mtx, MTX_DEF); 540 541 if (type->ks_memuse != 0) 542 printf("malloc_uninit: %ld bytes of '%s' still allocated\n", 543 type->ks_memuse, type->ks_shortdesc); 544#endif 545 546 if (type == kmemstatistics) 547 kmemstatistics = type->ks_next; 548 else { 549 for (t = kmemstatistics; t->ks_next != NULL; t = t->ks_next) { 550 if (t->ks_next == type) { 551 t->ks_next = type->ks_next; 552 break; 553 } 554 } 555 } 556 type->ks_next = NULL; 557 type->ks_limit = 0; 558} 559