Home | History | Annotate | Line # | Download | only in kern
kern_malloc.c revision 1.119.4.3
      1 /*	$NetBSD: kern_malloc.c,v 1.119.4.3 2010/03/11 15:04:17 yamt 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.119.4.3 2010/03/11 15:04:17 yamt Exp $");
     70 
     71 #include <sys/param.h>
     72 #include <sys/proc.h>
     73 #include <sys/kernel.h>
     74 #include <sys/malloc.h>
     75 #include <sys/systm.h>
     76 #include <sys/debug.h>
     77 #include <sys/mutex.h>
     78 #include <sys/lockdebug.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[((char *)(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 /*
    196  * Fuzz factor for neighbour address match this must be a mask of the lower
    197  * bits we wish to ignore when comparing addresses
    198  */
    199 __uintptr_t malloclog_fuzz = 0x7FL;
    200 
    201 
    202 static void
    203 domlog(void *a, long size, struct malloc_type *type, int action,
    204     const char *file, long line)
    205 {
    206 
    207 	malloclog[malloclogptr].addr = a;
    208 	malloclog[malloclogptr].size = size;
    209 	malloclog[malloclogptr].type = type;
    210 	malloclog[malloclogptr].action = action;
    211 	malloclog[malloclogptr].file = file;
    212 	malloclog[malloclogptr].line = line;
    213 	malloclogptr++;
    214 	if (malloclogptr >= MALLOCLOGSIZE)
    215 		malloclogptr = 0;
    216 }
    217 
    218 #ifdef DIAGNOSTIC
    219 static void
    220 hitmlog(void *a)
    221 {
    222 	struct malloclog *lp;
    223 	long l;
    224 
    225 #define	PRT do { \
    226 	lp = &malloclog[l]; \
    227 	if (lp->addr == a && lp->action) { \
    228 		printf("malloc log entry %ld:\n", l); \
    229 		printf("\taddr = %p\n", lp->addr); \
    230 		printf("\tsize = %ld\n", lp->size); \
    231 		printf("\ttype = %s\n", lp->type->ks_shortdesc); \
    232 		printf("\taction = %s\n", lp->action == 1 ? "alloc" : "free"); \
    233 		printf("\tfile = %s\n", lp->file); \
    234 		printf("\tline = %ld\n", lp->line); \
    235 	} \
    236 } while (/* CONSTCOND */0)
    237 
    238 /*
    239  * Print fuzzy matched "neighbour" - look for the memory block that has
    240  * been allocated below the address we are interested in.  We look for a
    241  * base address + size that is within malloclog_fuzz of our target
    242  * address. If the base address and target address are the same then it is
    243  * likely we have found a free (size is 0 in this case) so we won't report
    244  * those, they will get reported by PRT anyway.
    245  */
    246 #define	NPRT do { \
    247 	__uintptr_t fuzz_mask = ~(malloclog_fuzz); \
    248 	lp = &malloclog[l]; \
    249 	if ((__uintptr_t)lp->addr != (__uintptr_t)a && \
    250 	    (((__uintptr_t)lp->addr + lp->size + malloclog_fuzz) & fuzz_mask) \
    251 	    == ((__uintptr_t)a & fuzz_mask) && lp->action) {		\
    252 		printf("neighbour malloc log entry %ld:\n", l); \
    253 		printf("\taddr = %p\n", lp->addr); \
    254 		printf("\tsize = %ld\n", lp->size); \
    255 		printf("\ttype = %s\n", lp->type->ks_shortdesc); \
    256 		printf("\taction = %s\n", lp->action == 1 ? "alloc" : "free"); \
    257 		printf("\tfile = %s\n", lp->file); \
    258 		printf("\tline = %ld\n", lp->line); \
    259 	} \
    260 } while (/* CONSTCOND */0)
    261 
    262 	for (l = malloclogptr; l < MALLOCLOGSIZE; l++) {
    263 		PRT;
    264 		NPRT;
    265 	}
    266 
    267 
    268 	for (l = 0; l < malloclogptr; l++) {
    269 		PRT;
    270 		NPRT;
    271 	}
    272 
    273 #undef PRT
    274 }
    275 #endif /* DIAGNOSTIC */
    276 #endif /* MALLOCLOG */
    277 
    278 #ifdef DIAGNOSTIC
    279 /*
    280  * This structure provides a set of masks to catch unaligned frees.
    281  */
    282 const long addrmask[] = { 0,
    283 	0x00000001, 0x00000003, 0x00000007, 0x0000000f,
    284 	0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff,
    285 	0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff,
    286 	0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff,
    287 };
    288 
    289 /*
    290  * The WEIRD_ADDR is used as known text to copy into free objects so
    291  * that modifications after frees can be detected.
    292  */
    293 #define	WEIRD_ADDR	((uint32_t) 0xdeadbeef)
    294 #ifdef DEBUG
    295 #define	MAX_COPY	PAGE_SIZE
    296 #else
    297 #define	MAX_COPY	32
    298 #endif
    299 
    300 /*
    301  * Normally the freelist structure is used only to hold the list pointer
    302  * for free objects.  However, when running with diagnostics, the first
    303  * 8/16 bytes of the structure is unused except for diagnostic information,
    304  * and the free list pointer is at offset 8/16 in the structure.  Since the
    305  * first 8 bytes is the portion of the structure most often modified, this
    306  * helps to detect memory reuse problems and avoid free list corruption.
    307  */
    308 struct freelist {
    309 	uint32_t spare0;
    310 #ifdef _LP64
    311 	uint32_t spare1;		/* explicit padding */
    312 #endif
    313 	struct malloc_type *type;
    314 	void *	next;
    315 };
    316 #else /* !DIAGNOSTIC */
    317 struct freelist {
    318 	void *	next;
    319 };
    320 #endif /* DIAGNOSTIC */
    321 
    322 kmutex_t malloc_lock;
    323 
    324 /*
    325  * Allocate a block of memory
    326  */
    327 #ifdef MALLOCLOG
    328 void *
    329 _kern_malloc(unsigned long size, struct malloc_type *ksp, int flags,
    330     const char *file, long line)
    331 #else
    332 void *
    333 kern_malloc(unsigned long size, struct malloc_type *ksp, int flags)
    334 #endif /* MALLOCLOG */
    335 {
    336 	struct kmembuckets *kbp;
    337 	struct kmemusage *kup;
    338 	struct freelist *freep;
    339 	long indx, npg, allocsize;
    340 	char *va, *cp, *savedlist;
    341 #ifdef DIAGNOSTIC
    342 	uint32_t *end, *lp;
    343 	int copysize;
    344 #endif
    345 
    346 #ifdef LOCKDEBUG
    347 	if ((flags & M_NOWAIT) == 0) {
    348 		ASSERT_SLEEPABLE();
    349 	}
    350 #endif
    351 #ifdef MALLOC_DEBUG
    352 	if (debug_malloc(size, ksp, flags, (void *) &va)) {
    353 		if (va != 0) {
    354 			FREECHECK_OUT(&malloc_freecheck, (void *)va);
    355 		}
    356 		return ((void *) va);
    357 	}
    358 #endif
    359 	indx = BUCKETINDX(size);
    360 	kbp = &kmembuckets[indx];
    361 	mutex_spin_enter(&malloc_lock);
    362 #ifdef KMEMSTATS
    363 	while (ksp->ks_memuse >= ksp->ks_limit) {
    364 		if (flags & M_NOWAIT) {
    365 			mutex_spin_exit(&malloc_lock);
    366 			return ((void *) NULL);
    367 		}
    368 		if (ksp->ks_limblocks < 65535)
    369 			ksp->ks_limblocks++;
    370 		mtsleep((void *)ksp, PSWP+2, ksp->ks_shortdesc, 0,
    371 			&malloc_lock);
    372 	}
    373 	ksp->ks_size |= 1 << indx;
    374 #endif
    375 #ifdef DIAGNOSTIC
    376 	copysize = 1 << indx < MAX_COPY ? 1 << indx : MAX_COPY;
    377 #endif
    378 	if (kbp->kb_next == NULL) {
    379 		int s;
    380 		kbp->kb_last = NULL;
    381 		if (size > MAXALLOCSAVE)
    382 			allocsize = round_page(size);
    383 		else
    384 			allocsize = 1 << indx;
    385 		npg = btoc(allocsize);
    386 		mutex_spin_exit(&malloc_lock);
    387 		s = splvm();
    388 		va = (void *) uvm_km_alloc(kmem_map,
    389 		    (vsize_t)ctob(npg), 0,
    390 		    ((flags & M_NOWAIT) ? UVM_KMF_NOWAIT : 0) |
    391 		    ((flags & M_CANFAIL) ? UVM_KMF_CANFAIL : 0) |
    392 		    UVM_KMF_WIRED);
    393 		splx(s);
    394 		if (__predict_false(va == NULL)) {
    395 			/*
    396 			 * Kmem_malloc() can return NULL, even if it can
    397 			 * wait, if there is no map space available, because
    398 			 * it can't fix that problem.  Neither can we,
    399 			 * right now.  (We should release pages which
    400 			 * are completely free and which are in kmembuckets
    401 			 * with too many free elements.)
    402 			 */
    403 			if ((flags & (M_NOWAIT|M_CANFAIL)) == 0)
    404 				panic("malloc: out of space in kmem_map");
    405 			return (NULL);
    406 		}
    407 		mutex_spin_enter(&malloc_lock);
    408 #ifdef KMEMSTATS
    409 		kbp->kb_total += kbp->kb_elmpercl;
    410 #endif
    411 		kup = btokup(va);
    412 		kup->ku_indx = indx;
    413 		if (allocsize > MAXALLOCSAVE) {
    414 			if (npg > 65535)
    415 				panic("malloc: allocation too large");
    416 			kup->ku_pagecnt = npg;
    417 #ifdef KMEMSTATS
    418 			ksp->ks_memuse += allocsize;
    419 #endif
    420 			goto out;
    421 		}
    422 #ifdef KMEMSTATS
    423 		kup->ku_freecnt = kbp->kb_elmpercl;
    424 		kbp->kb_totalfree += kbp->kb_elmpercl;
    425 #endif
    426 		/*
    427 		 * Just in case we blocked while allocating memory,
    428 		 * and someone else also allocated memory for this
    429 		 * kmembucket, don't assume the list is still empty.
    430 		 */
    431 		savedlist = kbp->kb_next;
    432 		kbp->kb_next = cp = va + (npg << PAGE_SHIFT) - allocsize;
    433 		for (;;) {
    434 			freep = (struct freelist *)cp;
    435 #ifdef DIAGNOSTIC
    436 			/*
    437 			 * Copy in known text to detect modification
    438 			 * after freeing.
    439 			 */
    440 			end = (uint32_t *)&cp[copysize];
    441 			for (lp = (uint32_t *)cp; lp < end; lp++)
    442 				*lp = WEIRD_ADDR;
    443 			freep->type = M_FREE;
    444 #endif /* DIAGNOSTIC */
    445 			if (cp <= va)
    446 				break;
    447 			cp -= allocsize;
    448 			freep->next = cp;
    449 		}
    450 		freep->next = savedlist;
    451 		if (savedlist == NULL)
    452 			kbp->kb_last = (void *)freep;
    453 	}
    454 	va = kbp->kb_next;
    455 	kbp->kb_next = ((struct freelist *)va)->next;
    456 #ifdef DIAGNOSTIC
    457 	freep = (struct freelist *)va;
    458 	/* XXX potential to get garbage pointer here. */
    459 	if (kbp->kb_next) {
    460 		int rv;
    461 		vaddr_t addr = (vaddr_t)kbp->kb_next;
    462 
    463 		vm_map_lock(kmem_map);
    464 		rv = uvm_map_checkprot(kmem_map, addr,
    465 		    addr + sizeof(struct freelist), VM_PROT_WRITE);
    466 		vm_map_unlock(kmem_map);
    467 
    468 		if (__predict_false(rv == 0)) {
    469 			printf("Data modified on freelist: "
    470 			    "word %ld of object %p size %ld previous type %s "
    471 			    "(invalid addr %p)\n",
    472 			    (long)((int32_t *)&kbp->kb_next - (int32_t *)kbp),
    473 			    va, size, "foo", kbp->kb_next);
    474 #ifdef MALLOCLOG
    475 			hitmlog(va);
    476 #endif
    477 			kbp->kb_next = NULL;
    478 		}
    479 	}
    480 
    481 	/* Fill the fields that we've used with WEIRD_ADDR */
    482 #ifdef _LP64
    483 	freep->type = (struct malloc_type *)
    484 	    (WEIRD_ADDR | (((u_long) WEIRD_ADDR) << 32));
    485 #else
    486 	freep->type = (struct malloc_type *) WEIRD_ADDR;
    487 #endif
    488 	end = (uint32_t *)&freep->next +
    489 	    (sizeof(freep->next) / sizeof(int32_t));
    490 	for (lp = (uint32_t *)&freep->next; lp < end; lp++)
    491 		*lp = WEIRD_ADDR;
    492 
    493 	/* and check that the data hasn't been modified. */
    494 	end = (uint32_t *)&va[copysize];
    495 	for (lp = (uint32_t *)va; lp < end; lp++) {
    496 		if (__predict_true(*lp == WEIRD_ADDR))
    497 			continue;
    498 		printf("Data modified on freelist: "
    499 		    "word %ld of object %p size %ld previous type %s "
    500 		    "(0x%x != 0x%x)\n",
    501 		    (long)(lp - (uint32_t *)va), va, size,
    502 		    "bar", *lp, WEIRD_ADDR);
    503 #ifdef MALLOCLOG
    504 		hitmlog(va);
    505 #endif
    506 		break;
    507 	}
    508 
    509 	freep->spare0 = 0;
    510 #endif /* DIAGNOSTIC */
    511 #ifdef KMEMSTATS
    512 	kup = btokup(va);
    513 	if (kup->ku_indx != indx)
    514 		panic("malloc: wrong bucket");
    515 	if (kup->ku_freecnt == 0)
    516 		panic("malloc: lost data");
    517 	kup->ku_freecnt--;
    518 	kbp->kb_totalfree--;
    519 	ksp->ks_memuse += 1 << indx;
    520 out:
    521 	kbp->kb_calls++;
    522 	ksp->ks_inuse++;
    523 	ksp->ks_calls++;
    524 	if (ksp->ks_memuse > ksp->ks_maxused)
    525 		ksp->ks_maxused = ksp->ks_memuse;
    526 #else
    527 out:
    528 #endif
    529 #ifdef MALLOCLOG
    530 	domlog(va, size, ksp, 1, file, line);
    531 #endif
    532 	mutex_spin_exit(&malloc_lock);
    533 	if ((flags & M_ZERO) != 0)
    534 		memset(va, 0, size);
    535 	FREECHECK_OUT(&malloc_freecheck, (void *)va);
    536 	return ((void *) va);
    537 }
    538 
    539 /*
    540  * Free a block of memory allocated by malloc.
    541  */
    542 #ifdef MALLOCLOG
    543 void
    544 _kern_free(void *addr, struct malloc_type *ksp, const char *file, long line)
    545 #else
    546 void
    547 kern_free(void *addr, struct malloc_type *ksp)
    548 #endif /* MALLOCLOG */
    549 {
    550 	struct kmembuckets *kbp;
    551 	struct kmemusage *kup;
    552 	struct freelist *freep;
    553 	long size;
    554 #ifdef DIAGNOSTIC
    555 	void *cp;
    556 	int32_t *end, *lp;
    557 	long alloc, copysize;
    558 #endif
    559 
    560 	FREECHECK_IN(&malloc_freecheck, addr);
    561 #ifdef MALLOC_DEBUG
    562 	if (debug_free(addr, ksp))
    563 		return;
    564 #endif
    565 
    566 #ifdef DIAGNOSTIC
    567 	/*
    568 	 * Ensure that we're free'ing something that we could
    569 	 * have allocated in the first place.  That is, check
    570 	 * to see that the address is within kmem_map.
    571 	 */
    572 	if (__predict_false((vaddr_t)addr < vm_map_min(kmem_map) ||
    573 	    (vaddr_t)addr >= vm_map_max(kmem_map)))
    574 		panic("free: addr %p not within kmem_map", addr);
    575 #endif
    576 
    577 	kup = btokup(addr);
    578 	size = 1 << kup->ku_indx;
    579 	kbp = &kmembuckets[kup->ku_indx];
    580 
    581 	LOCKDEBUG_MEM_CHECK(addr,
    582 	    size <= MAXALLOCSAVE ? size : ctob(kup->ku_pagecnt));
    583 
    584 	mutex_spin_enter(&malloc_lock);
    585 #ifdef MALLOCLOG
    586 	domlog(addr, 0, ksp, 2, file, line);
    587 #endif
    588 #ifdef DIAGNOSTIC
    589 	/*
    590 	 * Check for returns of data that do not point to the
    591 	 * beginning of the allocation.
    592 	 */
    593 	if (size > PAGE_SIZE)
    594 		alloc = addrmask[BUCKETINDX(PAGE_SIZE)];
    595 	else
    596 		alloc = addrmask[kup->ku_indx];
    597 	if (((u_long)addr & alloc) != 0)
    598 		panic("free: unaligned addr %p, size %ld, type %s, mask %ld",
    599 		    addr, size, ksp->ks_shortdesc, alloc);
    600 #endif /* DIAGNOSTIC */
    601 	if (size > MAXALLOCSAVE) {
    602 		uvm_km_free(kmem_map, (vaddr_t)addr, ctob(kup->ku_pagecnt),
    603 		    UVM_KMF_WIRED);
    604 #ifdef KMEMSTATS
    605 		size = kup->ku_pagecnt << PGSHIFT;
    606 		ksp->ks_memuse -= size;
    607 		kup->ku_indx = 0;
    608 		kup->ku_pagecnt = 0;
    609 		if (ksp->ks_memuse + size >= ksp->ks_limit &&
    610 		    ksp->ks_memuse < ksp->ks_limit)
    611 			wakeup((void *)ksp);
    612 #ifdef DIAGNOSTIC
    613 		if (ksp->ks_inuse == 0)
    614 			panic("free 1: inuse 0, probable double free");
    615 #endif
    616 		ksp->ks_inuse--;
    617 		kbp->kb_total -= 1;
    618 #endif
    619 		mutex_spin_exit(&malloc_lock);
    620 		return;
    621 	}
    622 	freep = (struct freelist *)addr;
    623 #ifdef DIAGNOSTIC
    624 	/*
    625 	 * Check for multiple frees. Use a quick check to see if
    626 	 * it looks free before laboriously searching the freelist.
    627 	 */
    628 	if (__predict_false(freep->spare0 == WEIRD_ADDR)) {
    629 		for (cp = kbp->kb_next; cp;
    630 		    cp = ((struct freelist *)cp)->next) {
    631 			if (addr != cp)
    632 				continue;
    633 			printf("multiply freed item %p\n", addr);
    634 #ifdef MALLOCLOG
    635 			hitmlog(addr);
    636 #endif
    637 			panic("free: duplicated free");
    638 		}
    639 	}
    640 
    641 	/*
    642 	 * Copy in known text to detect modification after freeing
    643 	 * and to make it look free. Also, save the type being freed
    644 	 * so we can list likely culprit if modification is detected
    645 	 * when the object is reallocated.
    646 	 */
    647 	copysize = size < MAX_COPY ? size : MAX_COPY;
    648 	end = (int32_t *)&((char *)addr)[copysize];
    649 	for (lp = (int32_t *)addr; lp < end; lp++)
    650 		*lp = WEIRD_ADDR;
    651 	freep->type = ksp;
    652 #endif /* DIAGNOSTIC */
    653 #ifdef KMEMSTATS
    654 	kup->ku_freecnt++;
    655 	if (kup->ku_freecnt >= kbp->kb_elmpercl) {
    656 		if (kup->ku_freecnt > kbp->kb_elmpercl)
    657 			panic("free: multiple frees");
    658 		else if (kbp->kb_totalfree > kbp->kb_highwat)
    659 			kbp->kb_couldfree++;
    660 	}
    661 	kbp->kb_totalfree++;
    662 	ksp->ks_memuse -= size;
    663 	if (ksp->ks_memuse + size >= ksp->ks_limit &&
    664 	    ksp->ks_memuse < ksp->ks_limit)
    665 		wakeup((void *)ksp);
    666 #ifdef DIAGNOSTIC
    667 	if (ksp->ks_inuse == 0)
    668 		panic("free 2: inuse 0, probable double free");
    669 #endif
    670 	ksp->ks_inuse--;
    671 #endif
    672 	if (kbp->kb_next == NULL)
    673 		kbp->kb_next = addr;
    674 	else
    675 		((struct freelist *)kbp->kb_last)->next = addr;
    676 	freep->next = NULL;
    677 	kbp->kb_last = addr;
    678 	mutex_spin_exit(&malloc_lock);
    679 }
    680 
    681 /*
    682  * Change the size of a block of memory.
    683  */
    684 void *
    685 kern_realloc(void *curaddr, unsigned long newsize, struct malloc_type *ksp,
    686     int flags)
    687 {
    688 	struct kmemusage *kup;
    689 	unsigned long cursize;
    690 	void *newaddr;
    691 #ifdef DIAGNOSTIC
    692 	long alloc;
    693 #endif
    694 
    695 	/*
    696 	 * realloc() with a NULL pointer is the same as malloc().
    697 	 */
    698 	if (curaddr == NULL)
    699 		return (malloc(newsize, ksp, flags));
    700 
    701 	/*
    702 	 * realloc() with zero size is the same as free().
    703 	 */
    704 	if (newsize == 0) {
    705 		free(curaddr, ksp);
    706 		return (NULL);
    707 	}
    708 
    709 #ifdef LOCKDEBUG
    710 	if ((flags & M_NOWAIT) == 0) {
    711 		ASSERT_SLEEPABLE();
    712 	}
    713 #endif
    714 
    715 	/*
    716 	 * Find out how large the old allocation was (and do some
    717 	 * sanity checking).
    718 	 */
    719 	kup = btokup(curaddr);
    720 	cursize = 1 << kup->ku_indx;
    721 
    722 #ifdef DIAGNOSTIC
    723 	/*
    724 	 * Check for returns of data that do not point to the
    725 	 * beginning of the allocation.
    726 	 */
    727 	if (cursize > PAGE_SIZE)
    728 		alloc = addrmask[BUCKETINDX(PAGE_SIZE)];
    729 	else
    730 		alloc = addrmask[kup->ku_indx];
    731 	if (((u_long)curaddr & alloc) != 0)
    732 		panic("realloc: "
    733 		    "unaligned addr %p, size %ld, type %s, mask %ld\n",
    734 		    curaddr, cursize, ksp->ks_shortdesc, alloc);
    735 #endif /* DIAGNOSTIC */
    736 
    737 	if (cursize > MAXALLOCSAVE)
    738 		cursize = ctob(kup->ku_pagecnt);
    739 
    740 	/*
    741 	 * If we already actually have as much as they want, we're done.
    742 	 */
    743 	if (newsize <= cursize)
    744 		return (curaddr);
    745 
    746 	/*
    747 	 * Can't satisfy the allocation with the existing block.
    748 	 * Allocate a new one and copy the data.
    749 	 */
    750 	newaddr = malloc(newsize, ksp, flags);
    751 	if (__predict_false(newaddr == NULL)) {
    752 		/*
    753 		 * malloc() failed, because flags included M_NOWAIT.
    754 		 * Return NULL to indicate that failure.  The old
    755 		 * pointer is still valid.
    756 		 */
    757 		return (NULL);
    758 	}
    759 	memcpy(newaddr, curaddr, cursize);
    760 
    761 	/*
    762 	 * We were successful: free the old allocation and return
    763 	 * the new one.
    764 	 */
    765 	free(curaddr, ksp);
    766 	return (newaddr);
    767 }
    768 
    769 /*
    770  * Roundup size to the actual allocation size.
    771  */
    772 unsigned long
    773 malloc_roundup(unsigned long size)
    774 {
    775 
    776 	if (size > MAXALLOCSAVE)
    777 		return (roundup(size, PAGE_SIZE));
    778 	else
    779 		return (1 << BUCKETINDX(size));
    780 }
    781 
    782 /*
    783  * Add a malloc type to the system.
    784  */
    785 void
    786 malloc_type_attach(struct malloc_type *type)
    787 {
    788 
    789 	if (nkmempages == 0)
    790 		panic("malloc_type_attach: nkmempages == 0");
    791 
    792 	if (type->ks_magic != M_MAGIC)
    793 		panic("malloc_type_attach: bad magic");
    794 
    795 #ifdef DIAGNOSTIC
    796 	{
    797 		struct malloc_type *ksp;
    798 		for (ksp = kmemstatistics; ksp != NULL; ksp = ksp->ks_next) {
    799 			if (ksp == type)
    800 				panic("malloc_type_attach: already on list");
    801 		}
    802 	}
    803 #endif
    804 
    805 #ifdef KMEMSTATS
    806 	if (type->ks_limit == 0)
    807 		type->ks_limit = ((u_long)nkmempages << PAGE_SHIFT) * 6U / 10U;
    808 #else
    809 	type->ks_limit = 0;
    810 #endif
    811 
    812 	type->ks_next = kmemstatistics;
    813 	kmemstatistics = type;
    814 }
    815 
    816 /*
    817  * Remove a malloc type from the system..
    818  */
    819 void
    820 malloc_type_detach(struct malloc_type *type)
    821 {
    822 	struct malloc_type *ksp;
    823 
    824 #ifdef DIAGNOSTIC
    825 	if (type->ks_magic != M_MAGIC)
    826 		panic("malloc_type_detach: bad magic");
    827 #endif
    828 
    829 	if (type == kmemstatistics)
    830 		kmemstatistics = type->ks_next;
    831 	else {
    832 		for (ksp = kmemstatistics; ksp->ks_next != NULL;
    833 		     ksp = ksp->ks_next) {
    834 			if (ksp->ks_next == type) {
    835 				ksp->ks_next = type->ks_next;
    836 				break;
    837 			}
    838 		}
    839 #ifdef DIAGNOSTIC
    840 		if (ksp->ks_next == NULL)
    841 			panic("malloc_type_detach: not on list");
    842 #endif
    843 	}
    844 	type->ks_next = NULL;
    845 }
    846 
    847 /*
    848  * Set the limit on a malloc type.
    849  */
    850 void
    851 malloc_type_setlimit(struct malloc_type *type, u_long limit)
    852 {
    853 #ifdef KMEMSTATS
    854 	mutex_spin_enter(&malloc_lock);
    855 	type->ks_limit = limit;
    856 	mutex_spin_exit(&malloc_lock);
    857 #endif
    858 }
    859 
    860 /*
    861  * Compute the number of pages that kmem_map will map, that is,
    862  * the size of the kernel malloc arena.
    863  */
    864 void
    865 kmeminit_nkmempages(void)
    866 {
    867 	int npages;
    868 
    869 	if (nkmempages != 0) {
    870 		/*
    871 		 * It's already been set (by us being here before, or
    872 		 * by patching or kernel config options), bail out now.
    873 		 */
    874 		return;
    875 	}
    876 
    877 	npages = physmem;
    878 
    879 	if (npages > NKMEMPAGES_MAX)
    880 		npages = NKMEMPAGES_MAX;
    881 
    882 	if (npages < NKMEMPAGES_MIN)
    883 		npages = NKMEMPAGES_MIN;
    884 
    885 	nkmempages = npages;
    886 }
    887 
    888 /*
    889  * Initialize the kernel memory allocator
    890  */
    891 void
    892 kmeminit(void)
    893 {
    894 	__link_set_decl(malloc_types, struct malloc_type);
    895 	struct malloc_type * const *ksp;
    896 	vaddr_t kmb, kml;
    897 #ifdef KMEMSTATS
    898 	long indx;
    899 #endif
    900 
    901 #if	((MAXALLOCSAVE & (MAXALLOCSAVE - 1)) != 0)
    902 		ERROR!_kmeminit:_MAXALLOCSAVE_not_power_of_2
    903 #endif
    904 #if	(MAXALLOCSAVE > MINALLOCSIZE * 32768)
    905 		ERROR!_kmeminit:_MAXALLOCSAVE_too_big
    906 #endif
    907 #if	(MAXALLOCSAVE < NBPG)
    908 		ERROR!_kmeminit:_MAXALLOCSAVE_too_small
    909 #endif
    910 
    911 	if (sizeof(struct freelist) > (1 << MINBUCKET))
    912 		panic("minbucket too small/struct freelist too big");
    913 
    914 	mutex_init(&malloc_lock, MUTEX_DEFAULT, IPL_VM);
    915 
    916 	/*
    917 	 * Compute the number of kmem_map pages, if we have not
    918 	 * done so already.
    919 	 */
    920 	kmeminit_nkmempages();
    921 
    922 	kmemusage = (struct kmemusage *) uvm_km_alloc(kernel_map,
    923 	    (vsize_t)(nkmempages * sizeof(struct kmemusage)), 0,
    924 	    UVM_KMF_WIRED|UVM_KMF_ZERO);
    925 	kmb = 0;
    926 	kmem_map = uvm_km_suballoc(kernel_map, &kmb,
    927 	    &kml, ((vsize_t)nkmempages << PAGE_SHIFT),
    928 	    VM_MAP_INTRSAFE, false, &kmem_map_store);
    929 	uvm_km_vacache_init(kmem_map, "kvakmem", 0);
    930 	kmembase = (char *)kmb;
    931 	kmemlimit = (char *)kml;
    932 #ifdef KMEMSTATS
    933 	for (indx = 0; indx < MINBUCKET + 16; indx++) {
    934 		if (1 << indx >= PAGE_SIZE)
    935 			kmembuckets[indx].kb_elmpercl = 1;
    936 		else
    937 			kmembuckets[indx].kb_elmpercl = PAGE_SIZE / (1 << indx);
    938 		kmembuckets[indx].kb_highwat =
    939 			5 * kmembuckets[indx].kb_elmpercl;
    940 	}
    941 #endif
    942 
    943 	/* Attach all of the statically-linked malloc types. */
    944 	__link_set_foreach(ksp, malloc_types)
    945 		malloc_type_attach(*ksp);
    946 
    947 #ifdef MALLOC_DEBUG
    948 	debug_malloc_init();
    949 #endif
    950 }
    951 
    952 #ifdef DDB
    953 #include <ddb/db_output.h>
    954 
    955 /*
    956  * Dump kmem statistics from ddb.
    957  *
    958  * usage: call dump_kmemstats
    959  */
    960 void	dump_kmemstats(void);
    961 
    962 void
    963 dump_kmemstats(void)
    964 {
    965 #ifdef KMEMSTATS
    966 	struct malloc_type *ksp;
    967 
    968 	for (ksp = kmemstatistics; ksp != NULL; ksp = ksp->ks_next) {
    969 		if (ksp->ks_memuse == 0)
    970 			continue;
    971 		db_printf("%s%.*s %ld\n", ksp->ks_shortdesc,
    972 		    (int)(20 - strlen(ksp->ks_shortdesc)),
    973 		    "                    ",
    974 		    ksp->ks_memuse);
    975 	}
    976 #else
    977 	db_printf("Kmem stats are not being collected.\n");
    978 #endif /* KMEMSTATS */
    979 }
    980 #endif /* DDB */
    981 
    982 
    983 #if 0
    984 /*
    985  * Diagnostic messages about "Data modified on
    986  * freelist" indicate a memory corruption, but
    987  * they do not help tracking it down.
    988  * This function can be called at various places
    989  * to sanity check malloc's freelist and discover
    990  * where does the corruption take place.
    991  */
    992 int
    993 freelist_sanitycheck(void) {
    994 	int i,j;
    995 	struct kmembuckets *kbp;
    996 	struct freelist *freep;
    997 	int rv = 0;
    998 
    999 	for (i = MINBUCKET; i <= MINBUCKET + 15; i++) {
   1000 		kbp = &kmembuckets[i];
   1001 		freep = (struct freelist *)kbp->kb_next;
   1002 		j = 0;
   1003 		while(freep) {
   1004 			vm_map_lock(kmem_map);
   1005 			rv = uvm_map_checkprot(kmem_map, (vaddr_t)freep,
   1006 			    (vaddr_t)freep + sizeof(struct freelist),
   1007 			    VM_PROT_WRITE);
   1008 			vm_map_unlock(kmem_map);
   1009 
   1010 			if ((rv == 0) || (*(int *)freep != WEIRD_ADDR)) {
   1011 				printf("bucket %i, chunck %d at %p modified\n",
   1012 				    i, j, freep);
   1013 				return 1;
   1014 			}
   1015 			freep = (struct freelist *)freep->next;
   1016 			j++;
   1017 		}
   1018 	}
   1019 
   1020 	return 0;
   1021 }
   1022 #endif
   1023