Home | History | Annotate | Line # | Download | only in kern
kern_malloc.c revision 1.102.4.2
      1 /*	$NetBSD: kern_malloc.c,v 1.102.4.2 2007/01/19 20:49:54 ad Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1987, 1991, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  *
     31  *	@(#)kern_malloc.c	8.4 (Berkeley) 5/20/95
     32  */
     33 
     34 /*
     35  * Copyright (c) 1996 Christopher G. Demetriou.  All rights reserved.
     36  *
     37  * Redistribution and use in source and binary forms, with or without
     38  * modification, are permitted provided that the following conditions
     39  * are met:
     40  * 1. Redistributions of source code must retain the above copyright
     41  *    notice, this list of conditions and the following disclaimer.
     42  * 2. Redistributions in binary form must reproduce the above copyright
     43  *    notice, this list of conditions and the following disclaimer in the
     44  *    documentation and/or other materials provided with the distribution.
     45  * 3. All advertising materials mentioning features or use of this software
     46  *    must display the following acknowledgement:
     47  *	This product includes software developed by the University of
     48  *	California, Berkeley and its contributors.
     49  * 4. Neither the name of the University nor the names of its contributors
     50  *    may be used to endorse or promote products derived from this software
     51  *    without specific prior written permission.
     52  *
     53  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     54  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     55  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     56  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     57  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     58  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     59  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     60  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     61  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     62  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     63  * SUCH DAMAGE.
     64  *
     65  *	@(#)kern_malloc.c	8.4 (Berkeley) 5/20/95
     66  */
     67 
     68 #include <sys/cdefs.h>
     69 __KERNEL_RCSID(0, "$NetBSD: kern_malloc.c,v 1.102.4.2 2007/01/19 20:49:54 ad Exp $");
     70 
     71 #include "opt_lockdebug.h"
     72 
     73 #include <sys/param.h>
     74 #include <sys/proc.h>
     75 #include <sys/kernel.h>
     76 #include <sys/malloc.h>
     77 #include <sys/systm.h>
     78 #include <sys/debug.h>
     79 
     80 #include <uvm/uvm_extern.h>
     81 
     82 static struct vm_map_kernel kmem_map_store;
     83 struct vm_map *kmem_map = NULL;
     84 
     85 #include "opt_kmempages.h"
     86 
     87 #ifdef NKMEMCLUSTERS
     88 #error NKMEMCLUSTERS is obsolete; remove it from your kernel config file and use NKMEMPAGES instead or let the kernel auto-size
     89 #endif
     90 
     91 /*
     92  * Default number of pages in kmem_map.  We attempt to calculate this
     93  * at run-time, but allow it to be either patched or set in the kernel
     94  * config file.
     95  */
     96 #ifndef NKMEMPAGES
     97 #define	NKMEMPAGES	0
     98 #endif
     99 int	nkmempages = NKMEMPAGES;
    100 
    101 /*
    102  * Defaults for lower- and upper-bounds for the kmem_map page count.
    103  * Can be overridden by kernel config options.
    104  */
    105 #ifndef	NKMEMPAGES_MIN
    106 #define	NKMEMPAGES_MIN	NKMEMPAGES_MIN_DEFAULT
    107 #endif
    108 
    109 #ifndef NKMEMPAGES_MAX
    110 #define	NKMEMPAGES_MAX	NKMEMPAGES_MAX_DEFAULT
    111 #endif
    112 
    113 #include "opt_kmemstats.h"
    114 #include "opt_malloclog.h"
    115 #include "opt_malloc_debug.h"
    116 
    117 #define	MINALLOCSIZE	(1 << MINBUCKET)
    118 #define	BUCKETINDX(size) \
    119 	((size) <= (MINALLOCSIZE * 128) \
    120 		? (size) <= (MINALLOCSIZE * 8) \
    121 			? (size) <= (MINALLOCSIZE * 2) \
    122 				? (size) <= (MINALLOCSIZE * 1) \
    123 					? (MINBUCKET + 0) \
    124 					: (MINBUCKET + 1) \
    125 				: (size) <= (MINALLOCSIZE * 4) \
    126 					? (MINBUCKET + 2) \
    127 					: (MINBUCKET + 3) \
    128 			: (size) <= (MINALLOCSIZE* 32) \
    129 				? (size) <= (MINALLOCSIZE * 16) \
    130 					? (MINBUCKET + 4) \
    131 					: (MINBUCKET + 5) \
    132 				: (size) <= (MINALLOCSIZE * 64) \
    133 					? (MINBUCKET + 6) \
    134 					: (MINBUCKET + 7) \
    135 		: (size) <= (MINALLOCSIZE * 2048) \
    136 			? (size) <= (MINALLOCSIZE * 512) \
    137 				? (size) <= (MINALLOCSIZE * 256) \
    138 					? (MINBUCKET + 8) \
    139 					: (MINBUCKET + 9) \
    140 				: (size) <= (MINALLOCSIZE * 1024) \
    141 					? (MINBUCKET + 10) \
    142 					: (MINBUCKET + 11) \
    143 			: (size) <= (MINALLOCSIZE * 8192) \
    144 				? (size) <= (MINALLOCSIZE * 4096) \
    145 					? (MINBUCKET + 12) \
    146 					: (MINBUCKET + 13) \
    147 				: (size) <= (MINALLOCSIZE * 16384) \
    148 					? (MINBUCKET + 14) \
    149 					: (MINBUCKET + 15))
    150 
    151 /*
    152  * Array of descriptors that describe the contents of each page
    153  */
    154 struct kmemusage {
    155 	short ku_indx;		/* bucket index */
    156 	union {
    157 		u_short freecnt;/* for small allocations, free pieces in page */
    158 		u_short pagecnt;/* for large allocations, pages alloced */
    159 	} ku_un;
    160 };
    161 #define	ku_freecnt ku_un.freecnt
    162 #define	ku_pagecnt ku_un.pagecnt
    163 
    164 struct kmembuckets kmembuckets[MINBUCKET + 16];
    165 struct kmemusage *kmemusage;
    166 char *kmembase, *kmemlimit;
    167 
    168 #ifdef DEBUG
    169 static void *malloc_freecheck;
    170 #endif
    171 
    172 /*
    173  * Turn virtual addresses into kmem map indicies
    174  */
    175 #define	btokup(addr)	(&kmemusage[((caddr_t)(addr) - kmembase) >> PGSHIFT])
    176 
    177 struct malloc_type *kmemstatistics;
    178 
    179 #ifdef MALLOCLOG
    180 #ifndef MALLOCLOGSIZE
    181 #define	MALLOCLOGSIZE	100000
    182 #endif
    183 
    184 struct malloclog {
    185 	void *addr;
    186 	long size;
    187 	struct malloc_type *type;
    188 	int action;
    189 	const char *file;
    190 	long line;
    191 } malloclog[MALLOCLOGSIZE];
    192 
    193 long	malloclogptr;
    194 
    195 static void
    196 domlog(void *a, long size, struct malloc_type *type, int action,
    197     const char *file, long line)
    198 {
    199 
    200 	malloclog[malloclogptr].addr = a;
    201 	malloclog[malloclogptr].size = size;
    202 	malloclog[malloclogptr].type = type;
    203 	malloclog[malloclogptr].action = action;
    204 	malloclog[malloclogptr].file = file;
    205 	malloclog[malloclogptr].line = line;
    206 	malloclogptr++;
    207 	if (malloclogptr >= MALLOCLOGSIZE)
    208 		malloclogptr = 0;
    209 }
    210 
    211 static void
    212 hitmlog(void *a)
    213 {
    214 	struct malloclog *lp;
    215 	long l;
    216 
    217 #define	PRT do { \
    218 	lp = &malloclog[l]; \
    219 	if (lp->addr == a && lp->action) { \
    220 		printf("malloc log entry %ld:\n", l); \
    221 		printf("\taddr = %p\n", lp->addr); \
    222 		printf("\tsize = %ld\n", lp->size); \
    223 		printf("\ttype = %s\n", lp->type->ks_shortdesc); \
    224 		printf("\taction = %s\n", lp->action == 1 ? "alloc" : "free"); \
    225 		printf("\tfile = %s\n", lp->file); \
    226 		printf("\tline = %ld\n", lp->line); \
    227 	} \
    228 } while (/* CONSTCOND */0)
    229 
    230 	for (l = malloclogptr; l < MALLOCLOGSIZE; l++)
    231 		PRT;
    232 
    233 	for (l = 0; l < malloclogptr; l++)
    234 		PRT;
    235 #undef PRT
    236 }
    237 #endif /* MALLOCLOG */
    238 
    239 #ifdef DIAGNOSTIC
    240 /*
    241  * This structure provides a set of masks to catch unaligned frees.
    242  */
    243 const long addrmask[] = { 0,
    244 	0x00000001, 0x00000003, 0x00000007, 0x0000000f,
    245 	0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff,
    246 	0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff,
    247 	0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff,
    248 };
    249 
    250 /*
    251  * The WEIRD_ADDR is used as known text to copy into free objects so
    252  * that modifications after frees can be detected.
    253  */
    254 #define	WEIRD_ADDR	((uint32_t) 0xdeadbeef)
    255 #ifdef DEBUG
    256 #define	MAX_COPY	PAGE_SIZE
    257 #else
    258 #define	MAX_COPY	32
    259 #endif
    260 
    261 /*
    262  * Normally the freelist structure is used only to hold the list pointer
    263  * for free objects.  However, when running with diagnostics, the first
    264  * 8/16 bytes of the structure is unused except for diagnostic information,
    265  * and the free list pointer is at offset 8/16 in the structure.  Since the
    266  * first 8 bytes is the portion of the structure most often modified, this
    267  * helps to detect memory reuse problems and avoid free list corruption.
    268  */
    269 struct freelist {
    270 	uint32_t spare0;
    271 #ifdef _LP64
    272 	uint32_t spare1;		/* explicit padding */
    273 #endif
    274 	struct malloc_type *type;
    275 	caddr_t	next;
    276 };
    277 #else /* !DIAGNOSTIC */
    278 struct freelist {
    279 	caddr_t	next;
    280 };
    281 #endif /* DIAGNOSTIC */
    282 
    283 /*
    284  * The following are standard, built-in malloc types and are not
    285  * specific to any subsystem.
    286  */
    287 MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory");
    288 MALLOC_DEFINE(M_DMAMAP, "DMA map", "bus_dma(9) structures");
    289 MALLOC_DEFINE(M_FREE, "free", "should be on free list");
    290 MALLOC_DEFINE(M_PCB, "pcb", "protocol control block");
    291 MALLOC_DEFINE(M_SOFTINTR, "softintr", "Softinterrupt structures");
    292 MALLOC_DEFINE(M_TEMP, "temp", "misc. temporary data buffers");
    293 
    294 /* XXX These should all be elsewhere. */
    295 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
    296 MALLOC_DEFINE(M_FTABLE, "fragtbl", "fragment reassembly header");
    297 MALLOC_DEFINE(M_UFSMNT, "UFS mount", "UFS mount structure");
    298 MALLOC_DEFINE(M_NETADDR, "Export Host", "Export host address structure");
    299 MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "internet multicast options");
    300 MALLOC_DEFINE(M_IPMADDR, "in_multi", "internet multicast address");
    301 MALLOC_DEFINE(M_MRTABLE, "mrt", "multicast routing tables");
    302 MALLOC_DEFINE(M_BWMETER, "bwmeter", "multicast upcall bw meters");
    303 MALLOC_DEFINE(M_1394DATA, "1394data", "IEEE 1394 data buffers");
    304 
    305 struct simplelock malloc_slock = SIMPLELOCK_INITIALIZER;
    306 
    307 /*
    308  * Allocate a block of memory
    309  */
    310 #ifdef MALLOCLOG
    311 void *
    312 _malloc(unsigned long size, struct malloc_type *ksp, int flags,
    313     const char *file, long line)
    314 #else
    315 void *
    316 malloc(unsigned long size, struct malloc_type *ksp, int flags)
    317 #endif /* MALLOCLOG */
    318 {
    319 	struct kmembuckets *kbp;
    320 	struct kmemusage *kup;
    321 	struct freelist *freep;
    322 	long indx, npg, allocsize;
    323 	int s;
    324 	caddr_t va, cp, savedlist;
    325 #ifdef DIAGNOSTIC
    326 	uint32_t *end, *lp;
    327 	int copysize;
    328 #endif
    329 
    330 #ifdef LOCKDEBUG
    331 	if ((flags & M_NOWAIT) == 0)
    332 		ASSERT_SLEEPABLE(NULL, "malloc");
    333 #endif
    334 #ifdef MALLOC_DEBUG
    335 	if (debug_malloc(size, ksp, flags, (void *) &va)) {
    336 		if (va != 0)
    337 			FREECHECK_OUT(&malloc_freecheck, (void *)va);
    338 		return ((void *) va);
    339 	}
    340 #endif
    341 	indx = BUCKETINDX(size);
    342 	kbp = &kmembuckets[indx];
    343 	s = splvm();
    344 	simple_lock(&malloc_slock);
    345 #ifdef KMEMSTATS
    346 	while (ksp->ks_memuse >= ksp->ks_limit) {
    347 		if (flags & M_NOWAIT) {
    348 			simple_unlock(&malloc_slock);
    349 			splx(s);
    350 			return ((void *) NULL);
    351 		}
    352 		if (ksp->ks_limblocks < 65535)
    353 			ksp->ks_limblocks++;
    354 		ltsleep((caddr_t)ksp, PSWP+2, ksp->ks_shortdesc, 0,
    355 			&malloc_slock);
    356 	}
    357 	ksp->ks_size |= 1 << indx;
    358 #endif
    359 #ifdef DIAGNOSTIC
    360 	copysize = 1 << indx < MAX_COPY ? 1 << indx : MAX_COPY;
    361 #endif
    362 	if (kbp->kb_next == NULL) {
    363 		kbp->kb_last = NULL;
    364 		if (size > MAXALLOCSAVE)
    365 			allocsize = round_page(size);
    366 		else
    367 			allocsize = 1 << indx;
    368 		npg = btoc(allocsize);
    369 		simple_unlock(&malloc_slock);
    370 		va = (caddr_t) uvm_km_alloc(kmem_map,
    371 		    (vsize_t)ctob(npg), 0,
    372 		    ((flags & M_NOWAIT) ? UVM_KMF_NOWAIT : 0) |
    373 		    ((flags & M_CANFAIL) ? UVM_KMF_CANFAIL : 0) |
    374 		    UVM_KMF_WIRED);
    375 		if (__predict_false(va == NULL)) {
    376 			/*
    377 			 * Kmem_malloc() can return NULL, even if it can
    378 			 * wait, if there is no map space available, because
    379 			 * it can't fix that problem.  Neither can we,
    380 			 * right now.  (We should release pages which
    381 			 * are completely free and which are in kmembuckets
    382 			 * with too many free elements.)
    383 			 */
    384 			if ((flags & (M_NOWAIT|M_CANFAIL)) == 0)
    385 				panic("malloc: out of space in kmem_map");
    386 			splx(s);
    387 			return (NULL);
    388 		}
    389 		simple_lock(&malloc_slock);
    390 #ifdef KMEMSTATS
    391 		kbp->kb_total += kbp->kb_elmpercl;
    392 #endif
    393 		kup = btokup(va);
    394 		kup->ku_indx = indx;
    395 		if (allocsize > MAXALLOCSAVE) {
    396 			if (npg > 65535)
    397 				panic("malloc: allocation too large");
    398 			kup->ku_pagecnt = npg;
    399 #ifdef KMEMSTATS
    400 			ksp->ks_memuse += allocsize;
    401 #endif
    402 			goto out;
    403 		}
    404 #ifdef KMEMSTATS
    405 		kup->ku_freecnt = kbp->kb_elmpercl;
    406 		kbp->kb_totalfree += kbp->kb_elmpercl;
    407 #endif
    408 		/*
    409 		 * Just in case we blocked while allocating memory,
    410 		 * and someone else also allocated memory for this
    411 		 * kmembucket, don't assume the list is still empty.
    412 		 */
    413 		savedlist = kbp->kb_next;
    414 		kbp->kb_next = cp = va + (npg << PAGE_SHIFT) - allocsize;
    415 		for (;;) {
    416 			freep = (struct freelist *)cp;
    417 #ifdef DIAGNOSTIC
    418 			/*
    419 			 * Copy in known text to detect modification
    420 			 * after freeing.
    421 			 */
    422 			end = (uint32_t *)&cp[copysize];
    423 			for (lp = (uint32_t *)cp; lp < end; lp++)
    424 				*lp = WEIRD_ADDR;
    425 			freep->type = M_FREE;
    426 #endif /* DIAGNOSTIC */
    427 			if (cp <= va)
    428 				break;
    429 			cp -= allocsize;
    430 			freep->next = cp;
    431 		}
    432 		freep->next = savedlist;
    433 		if (kbp->kb_last == NULL)
    434 			kbp->kb_last = (caddr_t)freep;
    435 	}
    436 	va = kbp->kb_next;
    437 	kbp->kb_next = ((struct freelist *)va)->next;
    438 #ifdef DIAGNOSTIC
    439 	freep = (struct freelist *)va;
    440 	/* XXX potential to get garbage pointer here. */
    441 	if (kbp->kb_next) {
    442 		int rv;
    443 		vaddr_t addr = (vaddr_t)kbp->kb_next;
    444 
    445 		vm_map_lock(kmem_map);
    446 		rv = uvm_map_checkprot(kmem_map, addr,
    447 		    addr + sizeof(struct freelist), VM_PROT_WRITE);
    448 		vm_map_unlock(kmem_map);
    449 
    450 		if (__predict_false(rv == 0)) {
    451 			printf("Data modified on freelist: "
    452 			    "word %ld of object %p size %ld previous type %s "
    453 			    "(invalid addr %p)\n",
    454 			    (long)((int32_t *)&kbp->kb_next - (int32_t *)kbp),
    455 			    va, size, "foo", kbp->kb_next);
    456 #ifdef MALLOCLOG
    457 			hitmlog(va);
    458 #endif
    459 			kbp->kb_next = NULL;
    460 		}
    461 	}
    462 
    463 	/* Fill the fields that we've used with WEIRD_ADDR */
    464 #ifdef _LP64
    465 	freep->type = (struct malloc_type *)
    466 	    (WEIRD_ADDR | (((u_long) WEIRD_ADDR) << 32));
    467 #else
    468 	freep->type = (struct malloc_type *) WEIRD_ADDR;
    469 #endif
    470 	end = (uint32_t *)&freep->next +
    471 	    (sizeof(freep->next) / sizeof(int32_t));
    472 	for (lp = (uint32_t *)&freep->next; lp < end; lp++)
    473 		*lp = WEIRD_ADDR;
    474 
    475 	/* and check that the data hasn't been modified. */
    476 	end = (uint32_t *)&va[copysize];
    477 	for (lp = (uint32_t *)va; lp < end; lp++) {
    478 		if (__predict_true(*lp == WEIRD_ADDR))
    479 			continue;
    480 		printf("Data modified on freelist: "
    481 		    "word %ld of object %p size %ld previous type %s "
    482 		    "(0x%x != 0x%x)\n",
    483 		    (long)(lp - (uint32_t *)va), va, size,
    484 		    "bar", *lp, WEIRD_ADDR);
    485 #ifdef MALLOCLOG
    486 		hitmlog(va);
    487 #endif
    488 		break;
    489 	}
    490 
    491 	freep->spare0 = 0;
    492 #endif /* DIAGNOSTIC */
    493 #ifdef KMEMSTATS
    494 	kup = btokup(va);
    495 	if (kup->ku_indx != indx)
    496 		panic("malloc: wrong bucket");
    497 	if (kup->ku_freecnt == 0)
    498 		panic("malloc: lost data");
    499 	kup->ku_freecnt--;
    500 	kbp->kb_totalfree--;
    501 	ksp->ks_memuse += 1 << indx;
    502 out:
    503 	kbp->kb_calls++;
    504 	ksp->ks_inuse++;
    505 	ksp->ks_calls++;
    506 	if (ksp->ks_memuse > ksp->ks_maxused)
    507 		ksp->ks_maxused = ksp->ks_memuse;
    508 #else
    509 out:
    510 #endif
    511 #ifdef MALLOCLOG
    512 	domlog(va, size, ksp, 1, file, line);
    513 #endif
    514 	simple_unlock(&malloc_slock);
    515 	splx(s);
    516 	if ((flags & M_ZERO) != 0)
    517 		memset(va, 0, size);
    518 	FREECHECK_OUT(&malloc_freecheck, (void *)va);
    519 	return ((void *) va);
    520 }
    521 
    522 /*
    523  * Free a block of memory allocated by malloc.
    524  */
    525 #ifdef MALLOCLOG
    526 void
    527 _free(void *addr, struct malloc_type *ksp, const char *file, long line)
    528 #else
    529 void
    530 free(void *addr, struct malloc_type *ksp)
    531 #endif /* MALLOCLOG */
    532 {
    533 	struct kmembuckets *kbp;
    534 	struct kmemusage *kup;
    535 	struct freelist *freep;
    536 	long size;
    537 	int s;
    538 #ifdef DIAGNOSTIC
    539 	caddr_t cp;
    540 	int32_t *end, *lp;
    541 	long alloc, copysize;
    542 #endif
    543 
    544 	FREECHECK_IN(&malloc_freecheck, addr);
    545 
    546 #ifdef MALLOC_DEBUG
    547 	if (debug_free(addr, ksp))
    548 		return;
    549 #endif
    550 
    551 #ifdef DIAGNOSTIC
    552 	/*
    553 	 * Ensure that we're free'ing something that we could
    554 	 * have allocated in the first place.  That is, check
    555 	 * to see that the address is within kmem_map.
    556 	 */
    557 	if (__predict_false((vaddr_t)addr < vm_map_min(kmem_map) ||
    558 	    (vaddr_t)addr >= vm_map_max(kmem_map)))
    559 		panic("free: addr %p not within kmem_map", addr);
    560 #endif
    561 
    562 	kup = btokup(addr);
    563 	size = 1 << kup->ku_indx;
    564 	kbp = &kmembuckets[kup->ku_indx];
    565 	s = splvm();
    566 	simple_lock(&malloc_slock);
    567 #ifdef MALLOCLOG
    568 	domlog(addr, 0, ksp, 2, file, line);
    569 #endif
    570 #ifdef DIAGNOSTIC
    571 	/*
    572 	 * Check for returns of data that do not point to the
    573 	 * beginning of the allocation.
    574 	 */
    575 	if (size > PAGE_SIZE)
    576 		alloc = addrmask[BUCKETINDX(PAGE_SIZE)];
    577 	else
    578 		alloc = addrmask[kup->ku_indx];
    579 	if (((u_long)addr & alloc) != 0)
    580 		panic("free: unaligned addr %p, size %ld, type %s, mask %ld",
    581 		    addr, size, ksp->ks_shortdesc, alloc);
    582 #endif /* DIAGNOSTIC */
    583 	if (size > MAXALLOCSAVE) {
    584 		uvm_km_free(kmem_map, (vaddr_t)addr, ctob(kup->ku_pagecnt),
    585 		    UVM_KMF_WIRED);
    586 #ifdef KMEMSTATS
    587 		size = kup->ku_pagecnt << PGSHIFT;
    588 		ksp->ks_memuse -= size;
    589 		kup->ku_indx = 0;
    590 		kup->ku_pagecnt = 0;
    591 		if (ksp->ks_memuse + size >= ksp->ks_limit &&
    592 		    ksp->ks_memuse < ksp->ks_limit)
    593 			wakeup((caddr_t)ksp);
    594 #ifdef DIAGNOSTIC
    595 		if (ksp->ks_inuse == 0)
    596 			panic("free 1: inuse 0, probable double free");
    597 #endif
    598 		ksp->ks_inuse--;
    599 		kbp->kb_total -= 1;
    600 #endif
    601 		simple_unlock(&malloc_slock);
    602 		splx(s);
    603 		return;
    604 	}
    605 	freep = (struct freelist *)addr;
    606 #ifdef DIAGNOSTIC
    607 	/*
    608 	 * Check for multiple frees. Use a quick check to see if
    609 	 * it looks free before laboriously searching the freelist.
    610 	 */
    611 	if (__predict_false(freep->spare0 == WEIRD_ADDR)) {
    612 		for (cp = kbp->kb_next; cp;
    613 		    cp = ((struct freelist *)cp)->next) {
    614 			if (addr != cp)
    615 				continue;
    616 			printf("multiply freed item %p\n", addr);
    617 #ifdef MALLOCLOG
    618 			hitmlog(addr);
    619 #endif
    620 			panic("free: duplicated free");
    621 		}
    622 	}
    623 #ifdef LOCKDEBUG
    624 	/*
    625 	 * Check if we're freeing a locked simple lock.
    626 	 */
    627 	simple_lock_freecheck(addr, (char *)addr + size);
    628 #endif
    629 	/*
    630 	 * Copy in known text to detect modification after freeing
    631 	 * and to make it look free. Also, save the type being freed
    632 	 * so we can list likely culprit if modification is detected
    633 	 * when the object is reallocated.
    634 	 */
    635 	copysize = size < MAX_COPY ? size : MAX_COPY;
    636 	end = (int32_t *)&((caddr_t)addr)[copysize];
    637 	for (lp = (int32_t *)addr; lp < end; lp++)
    638 		*lp = WEIRD_ADDR;
    639 	freep->type = ksp;
    640 #endif /* DIAGNOSTIC */
    641 #ifdef KMEMSTATS
    642 	kup->ku_freecnt++;
    643 	if (kup->ku_freecnt >= kbp->kb_elmpercl) {
    644 		if (kup->ku_freecnt > kbp->kb_elmpercl)
    645 			panic("free: multiple frees");
    646 		else if (kbp->kb_totalfree > kbp->kb_highwat)
    647 			kbp->kb_couldfree++;
    648 	}
    649 	kbp->kb_totalfree++;
    650 	ksp->ks_memuse -= size;
    651 	if (ksp->ks_memuse + size >= ksp->ks_limit &&
    652 	    ksp->ks_memuse < ksp->ks_limit)
    653 		wakeup((caddr_t)ksp);
    654 #ifdef DIAGNOSTIC
    655 	if (ksp->ks_inuse == 0)
    656 		panic("free 2: inuse 0, probable double free");
    657 #endif
    658 	ksp->ks_inuse--;
    659 #endif
    660 	if (kbp->kb_next == NULL)
    661 		kbp->kb_next = addr;
    662 	else
    663 		((struct freelist *)kbp->kb_last)->next = addr;
    664 	freep->next = NULL;
    665 	kbp->kb_last = addr;
    666 	simple_unlock(&malloc_slock);
    667 	splx(s);
    668 }
    669 
    670 /*
    671  * Change the size of a block of memory.
    672  */
    673 void *
    674 realloc(void *curaddr, unsigned long newsize, struct malloc_type *ksp,
    675     int flags)
    676 {
    677 	struct kmemusage *kup;
    678 	unsigned long cursize;
    679 	void *newaddr;
    680 #ifdef DIAGNOSTIC
    681 	long alloc;
    682 #endif
    683 
    684 	/*
    685 	 * realloc() with a NULL pointer is the same as malloc().
    686 	 */
    687 	if (curaddr == NULL)
    688 		return (malloc(newsize, ksp, flags));
    689 
    690 	/*
    691 	 * realloc() with zero size is the same as free().
    692 	 */
    693 	if (newsize == 0) {
    694 		free(curaddr, ksp);
    695 		return (NULL);
    696 	}
    697 
    698 #ifdef LOCKDEBUG
    699 	if ((flags & M_NOWAIT) == 0)
    700 		ASSERT_SLEEPABLE(NULL, "realloc");
    701 #endif
    702 
    703 	/*
    704 	 * Find out how large the old allocation was (and do some
    705 	 * sanity checking).
    706 	 */
    707 	kup = btokup(curaddr);
    708 	cursize = 1 << kup->ku_indx;
    709 
    710 #ifdef DIAGNOSTIC
    711 	/*
    712 	 * Check for returns of data that do not point to the
    713 	 * beginning of the allocation.
    714 	 */
    715 	if (cursize > PAGE_SIZE)
    716 		alloc = addrmask[BUCKETINDX(PAGE_SIZE)];
    717 	else
    718 		alloc = addrmask[kup->ku_indx];
    719 	if (((u_long)curaddr & alloc) != 0)
    720 		panic("realloc: "
    721 		    "unaligned addr %p, size %ld, type %s, mask %ld\n",
    722 		    curaddr, cursize, ksp->ks_shortdesc, alloc);
    723 #endif /* DIAGNOSTIC */
    724 
    725 	if (cursize > MAXALLOCSAVE)
    726 		cursize = ctob(kup->ku_pagecnt);
    727 
    728 	/*
    729 	 * If we already actually have as much as they want, we're done.
    730 	 */
    731 	if (newsize <= cursize)
    732 		return (curaddr);
    733 
    734 	/*
    735 	 * Can't satisfy the allocation with the existing block.
    736 	 * Allocate a new one and copy the data.
    737 	 */
    738 	newaddr = malloc(newsize, ksp, flags);
    739 	if (__predict_false(newaddr == NULL)) {
    740 		/*
    741 		 * malloc() failed, because flags included M_NOWAIT.
    742 		 * Return NULL to indicate that failure.  The old
    743 		 * pointer is still valid.
    744 		 */
    745 		return (NULL);
    746 	}
    747 	memcpy(newaddr, curaddr, cursize);
    748 
    749 	/*
    750 	 * We were successful: free the old allocation and return
    751 	 * the new one.
    752 	 */
    753 	free(curaddr, ksp);
    754 	return (newaddr);
    755 }
    756 
    757 /*
    758  * Roundup size to the actual allocation size.
    759  */
    760 unsigned long
    761 malloc_roundup(unsigned long size)
    762 {
    763 
    764 	if (size > MAXALLOCSAVE)
    765 		return (roundup(size, PAGE_SIZE));
    766 	else
    767 		return (1 << BUCKETINDX(size));
    768 }
    769 
    770 /*
    771  * Add a malloc type to the system.
    772  */
    773 void
    774 malloc_type_attach(struct malloc_type *type)
    775 {
    776 
    777 	if (nkmempages == 0)
    778 		panic("malloc_type_attach: nkmempages == 0");
    779 
    780 	if (type->ks_magic != M_MAGIC)
    781 		panic("malloc_type_attach: bad magic");
    782 
    783 #ifdef DIAGNOSTIC
    784 	{
    785 		struct malloc_type *ksp;
    786 		for (ksp = kmemstatistics; ksp != NULL; ksp = ksp->ks_next) {
    787 			if (ksp == type)
    788 				panic("malloc_type_attach: already on list");
    789 		}
    790 	}
    791 #endif
    792 
    793 #ifdef KMEMSTATS
    794 	if (type->ks_limit == 0)
    795 		type->ks_limit = ((u_long)nkmempages << PAGE_SHIFT) * 6U / 10U;
    796 #else
    797 	type->ks_limit = 0;
    798 #endif
    799 
    800 	type->ks_next = kmemstatistics;
    801 	kmemstatistics = type;
    802 }
    803 
    804 /*
    805  * Remove a malloc type from the system..
    806  */
    807 void
    808 malloc_type_detach(struct malloc_type *type)
    809 {
    810 	struct malloc_type *ksp;
    811 
    812 #ifdef DIAGNOSTIC
    813 	if (type->ks_magic != M_MAGIC)
    814 		panic("malloc_type_detach: bad magic");
    815 #endif
    816 
    817 	if (type == kmemstatistics)
    818 		kmemstatistics = type->ks_next;
    819 	else {
    820 		for (ksp = kmemstatistics; ksp->ks_next != NULL;
    821 		     ksp = ksp->ks_next) {
    822 			if (ksp->ks_next == type) {
    823 				ksp->ks_next = type->ks_next;
    824 				break;
    825 			}
    826 		}
    827 #ifdef DIAGNOSTIC
    828 		if (ksp->ks_next == NULL)
    829 			panic("malloc_type_detach: not on list");
    830 #endif
    831 	}
    832 	type->ks_next = NULL;
    833 }
    834 
    835 /*
    836  * Set the limit on a malloc type.
    837  */
    838 void
    839 malloc_type_setlimit(struct malloc_type *type, u_long limit)
    840 {
    841 #ifdef KMEMSTATS
    842 	int s;
    843 
    844 	s = splvm();
    845 	type->ks_limit = limit;
    846 	splx(s);
    847 #endif
    848 }
    849 
    850 /*
    851  * Compute the number of pages that kmem_map will map, that is,
    852  * the size of the kernel malloc arena.
    853  */
    854 void
    855 kmeminit_nkmempages(void)
    856 {
    857 	int npages;
    858 
    859 	if (nkmempages != 0) {
    860 		/*
    861 		 * It's already been set (by us being here before, or
    862 		 * by patching or kernel config options), bail out now.
    863 		 */
    864 		return;
    865 	}
    866 
    867 	npages = physmem;
    868 
    869 	if (npages > NKMEMPAGES_MAX)
    870 		npages = NKMEMPAGES_MAX;
    871 
    872 	if (npages < NKMEMPAGES_MIN)
    873 		npages = NKMEMPAGES_MIN;
    874 
    875 	nkmempages = npages;
    876 }
    877 
    878 /*
    879  * Initialize the kernel memory allocator
    880  */
    881 void
    882 kmeminit(void)
    883 {
    884 	__link_set_decl(malloc_types, struct malloc_type);
    885 	struct malloc_type * const *ksp;
    886 	vaddr_t kmb, kml;
    887 #ifdef KMEMSTATS
    888 	long indx;
    889 #endif
    890 
    891 #if	((MAXALLOCSAVE & (MAXALLOCSAVE - 1)) != 0)
    892 		ERROR!_kmeminit:_MAXALLOCSAVE_not_power_of_2
    893 #endif
    894 #if	(MAXALLOCSAVE > MINALLOCSIZE * 32768)
    895 		ERROR!_kmeminit:_MAXALLOCSAVE_too_big
    896 #endif
    897 #if	(MAXALLOCSAVE < NBPG)
    898 		ERROR!_kmeminit:_MAXALLOCSAVE_too_small
    899 #endif
    900 
    901 	if (sizeof(struct freelist) > (1 << MINBUCKET))
    902 		panic("minbucket too small/struct freelist too big");
    903 
    904 	/*
    905 	 * Compute the number of kmem_map pages, if we have not
    906 	 * done so already.
    907 	 */
    908 	kmeminit_nkmempages();
    909 
    910 	kmemusage = (struct kmemusage *) uvm_km_alloc(kernel_map,
    911 	    (vsize_t)(nkmempages * sizeof(struct kmemusage)), 0,
    912 	    UVM_KMF_WIRED|UVM_KMF_ZERO);
    913 	kmb = 0;
    914 	kmem_map = uvm_km_suballoc(kernel_map, &kmb,
    915 	    &kml, ((vsize_t)nkmempages << PAGE_SHIFT),
    916 	    VM_MAP_INTRSAFE, FALSE, &kmem_map_store);
    917 	uvm_km_vacache_init(kmem_map, "kvakmem", 0);
    918 	kmembase = (char *)kmb;
    919 	kmemlimit = (char *)kml;
    920 #ifdef KMEMSTATS
    921 	for (indx = 0; indx < MINBUCKET + 16; indx++) {
    922 		if (1 << indx >= PAGE_SIZE)
    923 			kmembuckets[indx].kb_elmpercl = 1;
    924 		else
    925 			kmembuckets[indx].kb_elmpercl = PAGE_SIZE / (1 << indx);
    926 		kmembuckets[indx].kb_highwat =
    927 			5 * kmembuckets[indx].kb_elmpercl;
    928 	}
    929 #endif
    930 
    931 	/* Attach all of the statically-linked malloc types. */
    932 	__link_set_foreach(ksp, malloc_types)
    933 		malloc_type_attach(*ksp);
    934 
    935 #ifdef MALLOC_DEBUG
    936 	debug_malloc_init();
    937 #endif
    938 }
    939 
    940 #ifdef DDB
    941 #include <ddb/db_output.h>
    942 
    943 /*
    944  * Dump kmem statistics from ddb.
    945  *
    946  * usage: call dump_kmemstats
    947  */
    948 void	dump_kmemstats(void);
    949 
    950 void
    951 dump_kmemstats(void)
    952 {
    953 #ifdef KMEMSTATS
    954 	struct malloc_type *ksp;
    955 
    956 	for (ksp = kmemstatistics; ksp != NULL; ksp = ksp->ks_next) {
    957 		if (ksp->ks_memuse == 0)
    958 			continue;
    959 		db_printf("%s%.*s %ld\n", ksp->ks_shortdesc,
    960 		    (int)(20 - strlen(ksp->ks_shortdesc)),
    961 		    "                    ",
    962 		    ksp->ks_memuse);
    963 	}
    964 #else
    965 	db_printf("Kmem stats are not being collected.\n");
    966 #endif /* KMEMSTATS */
    967 }
    968 #endif /* DDB */
    969 
    970 
    971 #if 0
    972 /*
    973  * Diagnostic messages about "Data modified on
    974  * freelist" indicate a memory corruption, but
    975  * they do not help tracking it down.
    976  * This function can be called at various places
    977  * to sanity check malloc's freelist and discover
    978  * where does the corruption take place.
    979  */
    980 int
    981 freelist_sanitycheck(void) {
    982 	int i,j;
    983 	struct kmembuckets *kbp;
    984 	struct freelist *freep;
    985 	int rv = 0;
    986 
    987 	for (i = MINBUCKET; i <= MINBUCKET + 15; i++) {
    988 		kbp = &kmembuckets[i];
    989 		freep = (struct freelist *)kbp->kb_next;
    990 		j = 0;
    991 		while(freep) {
    992 			vm_map_lock(kmem_map);
    993 			rv = uvm_map_checkprot(kmem_map, (vaddr_t)freep,
    994 			    (vaddr_t)freep + sizeof(struct freelist),
    995 			    VM_PROT_WRITE);
    996 			vm_map_unlock(kmem_map);
    997 
    998 			if ((rv == 0) || (*(int *)freep != WEIRD_ADDR)) {
    999 				printf("bucket %i, chunck %d at %p modified\n",
   1000 				    i, j, freep);
   1001 				return 1;
   1002 			}
   1003 			freep = (struct freelist *)freep->next;
   1004 			j++;
   1005 		}
   1006 	}
   1007 
   1008 	return 0;
   1009 }
   1010 #endif
   1011