Home | History | Annotate | Line # | Download | only in kern
kern_malloc.c revision 1.111.8.2
      1 /*	$NetBSD: kern_malloc.c,v 1.111.8.2 2007/11/08 11:00:02 matt 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.111.8.2 2007/11/08 11:00:02 matt 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 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 	void *	next;
    276 };
    277 #else /* !DIAGNOSTIC */
    278 struct freelist {
    279 	void *	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 kmutex_t malloc_lock;
    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 	char *va, *cp, *savedlist;
    324 #ifdef DIAGNOSTIC
    325 	uint32_t *end, *lp;
    326 	int copysize;
    327 #endif
    328 
    329 #ifdef LOCKDEBUG
    330 	if ((flags & M_NOWAIT) == 0)
    331 		ASSERT_SLEEPABLE(NULL, "malloc");
    332 #endif
    333 #ifdef MALLOC_DEBUG
    334 	if (debug_malloc(size, ksp, flags, (void *) &va)) {
    335 		if (va != 0)
    336 			FREECHECK_OUT(&malloc_freecheck, (void *)va);
    337 		return ((void *) va);
    338 	}
    339 #endif
    340 	indx = BUCKETINDX(size);
    341 	kbp = &kmembuckets[indx];
    342 	mutex_spin_enter(&malloc_lock);
    343 #ifdef KMEMSTATS
    344 	while (ksp->ks_memuse >= ksp->ks_limit) {
    345 		if (flags & M_NOWAIT) {
    346 			mutex_spin_exit(&malloc_lock);
    347 			return ((void *) NULL);
    348 		}
    349 		if (ksp->ks_limblocks < 65535)
    350 			ksp->ks_limblocks++;
    351 		mtsleep((void *)ksp, PSWP+2, ksp->ks_shortdesc, 0,
    352 			&malloc_lock);
    353 	}
    354 	ksp->ks_size |= 1 << indx;
    355 #endif
    356 #ifdef DIAGNOSTIC
    357 	copysize = 1 << indx < MAX_COPY ? 1 << indx : MAX_COPY;
    358 #endif
    359 	if (kbp->kb_next == NULL) {
    360 		int s;
    361 		kbp->kb_last = NULL;
    362 		if (size > MAXALLOCSAVE)
    363 			allocsize = round_page(size);
    364 		else
    365 			allocsize = 1 << indx;
    366 		npg = btoc(allocsize);
    367 		mutex_spin_exit(&malloc_lock);
    368 		s = splvm();
    369 		va = (void *) uvm_km_alloc(kmem_map,
    370 		    (vsize_t)ctob(npg), 0,
    371 		    ((flags & M_NOWAIT) ? UVM_KMF_NOWAIT : 0) |
    372 		    ((flags & M_CANFAIL) ? UVM_KMF_CANFAIL : 0) |
    373 		    UVM_KMF_WIRED);
    374 		splx(s);
    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 			return (NULL);
    387 		}
    388 		mutex_spin_enter(&malloc_lock);
    389 #ifdef KMEMSTATS
    390 		kbp->kb_total += kbp->kb_elmpercl;
    391 #endif
    392 		kup = btokup(va);
    393 		kup->ku_indx = indx;
    394 		if (allocsize > MAXALLOCSAVE) {
    395 			if (npg > 65535)
    396 				panic("malloc: allocation too large");
    397 			kup->ku_pagecnt = npg;
    398 #ifdef KMEMSTATS
    399 			ksp->ks_memuse += allocsize;
    400 #endif
    401 			goto out;
    402 		}
    403 #ifdef KMEMSTATS
    404 		kup->ku_freecnt = kbp->kb_elmpercl;
    405 		kbp->kb_totalfree += kbp->kb_elmpercl;
    406 #endif
    407 		/*
    408 		 * Just in case we blocked while allocating memory,
    409 		 * and someone else also allocated memory for this
    410 		 * kmembucket, don't assume the list is still empty.
    411 		 */
    412 		savedlist = kbp->kb_next;
    413 		kbp->kb_next = cp = va + (npg << PAGE_SHIFT) - allocsize;
    414 		for (;;) {
    415 			freep = (struct freelist *)cp;
    416 #ifdef DIAGNOSTIC
    417 			/*
    418 			 * Copy in known text to detect modification
    419 			 * after freeing.
    420 			 */
    421 			end = (uint32_t *)&cp[copysize];
    422 			for (lp = (uint32_t *)cp; lp < end; lp++)
    423 				*lp = WEIRD_ADDR;
    424 			freep->type = M_FREE;
    425 #endif /* DIAGNOSTIC */
    426 			if (cp <= va)
    427 				break;
    428 			cp -= allocsize;
    429 			freep->next = cp;
    430 		}
    431 		freep->next = savedlist;
    432 		if (kbp->kb_last == NULL)
    433 			kbp->kb_last = (void *)freep;
    434 	}
    435 	va = kbp->kb_next;
    436 	kbp->kb_next = ((struct freelist *)va)->next;
    437 #ifdef DIAGNOSTIC
    438 	freep = (struct freelist *)va;
    439 	/* XXX potential to get garbage pointer here. */
    440 	if (kbp->kb_next) {
    441 		int rv;
    442 		vaddr_t addr = (vaddr_t)kbp->kb_next;
    443 
    444 		vm_map_lock(kmem_map);
    445 		rv = uvm_map_checkprot(kmem_map, addr,
    446 		    addr + sizeof(struct freelist), VM_PROT_WRITE);
    447 		vm_map_unlock(kmem_map);
    448 
    449 		if (__predict_false(rv == 0)) {
    450 			printf("Data modified on freelist: "
    451 			    "word %ld of object %p size %ld previous type %s "
    452 			    "(invalid addr %p)\n",
    453 			    (long)((int32_t *)&kbp->kb_next - (int32_t *)kbp),
    454 			    va, size, "foo", kbp->kb_next);
    455 #ifdef MALLOCLOG
    456 			hitmlog(va);
    457 #endif
    458 			kbp->kb_next = NULL;
    459 		}
    460 	}
    461 
    462 	/* Fill the fields that we've used with WEIRD_ADDR */
    463 #ifdef _LP64
    464 	freep->type = (struct malloc_type *)
    465 	    (WEIRD_ADDR | (((u_long) WEIRD_ADDR) << 32));
    466 #else
    467 	freep->type = (struct malloc_type *) WEIRD_ADDR;
    468 #endif
    469 	end = (uint32_t *)&freep->next +
    470 	    (sizeof(freep->next) / sizeof(int32_t));
    471 	for (lp = (uint32_t *)&freep->next; lp < end; lp++)
    472 		*lp = WEIRD_ADDR;
    473 
    474 	/* and check that the data hasn't been modified. */
    475 	end = (uint32_t *)&va[copysize];
    476 	for (lp = (uint32_t *)va; lp < end; lp++) {
    477 		if (__predict_true(*lp == WEIRD_ADDR))
    478 			continue;
    479 		printf("Data modified on freelist: "
    480 		    "word %ld of object %p size %ld previous type %s "
    481 		    "(0x%x != 0x%x)\n",
    482 		    (long)(lp - (uint32_t *)va), va, size,
    483 		    "bar", *lp, WEIRD_ADDR);
    484 #ifdef MALLOCLOG
    485 		hitmlog(va);
    486 #endif
    487 		break;
    488 	}
    489 
    490 	freep->spare0 = 0;
    491 #endif /* DIAGNOSTIC */
    492 #ifdef KMEMSTATS
    493 	kup = btokup(va);
    494 	if (kup->ku_indx != indx)
    495 		panic("malloc: wrong bucket");
    496 	if (kup->ku_freecnt == 0)
    497 		panic("malloc: lost data");
    498 	kup->ku_freecnt--;
    499 	kbp->kb_totalfree--;
    500 	ksp->ks_memuse += 1 << indx;
    501 out:
    502 	kbp->kb_calls++;
    503 	ksp->ks_inuse++;
    504 	ksp->ks_calls++;
    505 	if (ksp->ks_memuse > ksp->ks_maxused)
    506 		ksp->ks_maxused = ksp->ks_memuse;
    507 #else
    508 out:
    509 #endif
    510 #ifdef MALLOCLOG
    511 	domlog(va, size, ksp, 1, file, line);
    512 #endif
    513 	mutex_spin_exit(&malloc_lock);
    514 	if ((flags & M_ZERO) != 0)
    515 		memset(va, 0, size);
    516 	FREECHECK_OUT(&malloc_freecheck, (void *)va);
    517 	return ((void *) va);
    518 }
    519 
    520 /*
    521  * Free a block of memory allocated by malloc.
    522  */
    523 #ifdef MALLOCLOG
    524 void
    525 _free(void *addr, struct malloc_type *ksp, const char *file, long line)
    526 #else
    527 void
    528 free(void *addr, struct malloc_type *ksp)
    529 #endif /* MALLOCLOG */
    530 {
    531 	struct kmembuckets *kbp;
    532 	struct kmemusage *kup;
    533 	struct freelist *freep;
    534 	long size;
    535 #ifdef DIAGNOSTIC
    536 	void *cp;
    537 	int32_t *end, *lp;
    538 	long alloc, copysize;
    539 #endif
    540 
    541 	FREECHECK_IN(&malloc_freecheck, addr);
    542 #ifdef MALLOC_DEBUG
    543 	if (debug_free(addr, ksp))
    544 		return;
    545 #endif
    546 
    547 #ifdef DIAGNOSTIC
    548 	/*
    549 	 * Ensure that we're free'ing something that we could
    550 	 * have allocated in the first place.  That is, check
    551 	 * to see that the address is within kmem_map.
    552 	 */
    553 	if (__predict_false((vaddr_t)addr < vm_map_min(kmem_map) ||
    554 	    (vaddr_t)addr >= vm_map_max(kmem_map)))
    555 		panic("free: addr %p not within kmem_map", addr);
    556 #endif
    557 
    558 	kup = btokup(addr);
    559 	size = 1 << kup->ku_indx;
    560 	kbp = &kmembuckets[kup->ku_indx];
    561 
    562 	LOCKDEBUG_MEM_CHECK(addr, size);
    563 
    564 	mutex_spin_enter(&malloc_lock);
    565 #ifdef MALLOCLOG
    566 	domlog(addr, 0, ksp, 2, file, line);
    567 #endif
    568 #ifdef DIAGNOSTIC
    569 	/*
    570 	 * Check for returns of data that do not point to the
    571 	 * beginning of the allocation.
    572 	 */
    573 	if (size > PAGE_SIZE)
    574 		alloc = addrmask[BUCKETINDX(PAGE_SIZE)];
    575 	else
    576 		alloc = addrmask[kup->ku_indx];
    577 	if (((u_long)addr & alloc) != 0)
    578 		panic("free: unaligned addr %p, size %ld, type %s, mask %ld",
    579 		    addr, size, ksp->ks_shortdesc, alloc);
    580 #endif /* DIAGNOSTIC */
    581 	if (size > MAXALLOCSAVE) {
    582 		uvm_km_free(kmem_map, (vaddr_t)addr, ctob(kup->ku_pagecnt),
    583 		    UVM_KMF_WIRED);
    584 #ifdef KMEMSTATS
    585 		size = kup->ku_pagecnt << PGSHIFT;
    586 		ksp->ks_memuse -= size;
    587 		kup->ku_indx = 0;
    588 		kup->ku_pagecnt = 0;
    589 		if (ksp->ks_memuse + size >= ksp->ks_limit &&
    590 		    ksp->ks_memuse < ksp->ks_limit)
    591 			wakeup((void *)ksp);
    592 #ifdef DIAGNOSTIC
    593 		if (ksp->ks_inuse == 0)
    594 			panic("free 1: inuse 0, probable double free");
    595 #endif
    596 		ksp->ks_inuse--;
    597 		kbp->kb_total -= 1;
    598 #endif
    599 		mutex_spin_exit(&malloc_lock);
    600 		return;
    601 	}
    602 	freep = (struct freelist *)addr;
    603 #ifdef DIAGNOSTIC
    604 	/*
    605 	 * Check for multiple frees. Use a quick check to see if
    606 	 * it looks free before laboriously searching the freelist.
    607 	 */
    608 	if (__predict_false(freep->spare0 == WEIRD_ADDR)) {
    609 		for (cp = kbp->kb_next; cp;
    610 		    cp = ((struct freelist *)cp)->next) {
    611 			if (addr != cp)
    612 				continue;
    613 			printf("multiply freed item %p\n", addr);
    614 #ifdef MALLOCLOG
    615 			hitmlog(addr);
    616 #endif
    617 			panic("free: duplicated free");
    618 		}
    619 	}
    620 
    621 	/*
    622 	 * Copy in known text to detect modification after freeing
    623 	 * and to make it look free. Also, save the type being freed
    624 	 * so we can list likely culprit if modification is detected
    625 	 * when the object is reallocated.
    626 	 */
    627 	copysize = size < MAX_COPY ? size : MAX_COPY;
    628 	end = (int32_t *)&((char *)addr)[copysize];
    629 	for (lp = (int32_t *)addr; lp < end; lp++)
    630 		*lp = WEIRD_ADDR;
    631 	freep->type = ksp;
    632 #endif /* DIAGNOSTIC */
    633 #ifdef KMEMSTATS
    634 	kup->ku_freecnt++;
    635 	if (kup->ku_freecnt >= kbp->kb_elmpercl) {
    636 		if (kup->ku_freecnt > kbp->kb_elmpercl)
    637 			panic("free: multiple frees");
    638 		else if (kbp->kb_totalfree > kbp->kb_highwat)
    639 			kbp->kb_couldfree++;
    640 	}
    641 	kbp->kb_totalfree++;
    642 	ksp->ks_memuse -= size;
    643 	if (ksp->ks_memuse + size >= ksp->ks_limit &&
    644 	    ksp->ks_memuse < ksp->ks_limit)
    645 		wakeup((void *)ksp);
    646 #ifdef DIAGNOSTIC
    647 	if (ksp->ks_inuse == 0)
    648 		panic("free 2: inuse 0, probable double free");
    649 #endif
    650 	ksp->ks_inuse--;
    651 #endif
    652 	if (kbp->kb_next == NULL)
    653 		kbp->kb_next = addr;
    654 	else
    655 		((struct freelist *)kbp->kb_last)->next = addr;
    656 	freep->next = NULL;
    657 	kbp->kb_last = addr;
    658 	mutex_spin_exit(&malloc_lock);
    659 }
    660 
    661 /*
    662  * Change the size of a block of memory.
    663  */
    664 void *
    665 realloc(void *curaddr, unsigned long newsize, struct malloc_type *ksp,
    666     int flags)
    667 {
    668 	struct kmemusage *kup;
    669 	unsigned long cursize;
    670 	void *newaddr;
    671 #ifdef DIAGNOSTIC
    672 	long alloc;
    673 #endif
    674 
    675 	/*
    676 	 * realloc() with a NULL pointer is the same as malloc().
    677 	 */
    678 	if (curaddr == NULL)
    679 		return (malloc(newsize, ksp, flags));
    680 
    681 	/*
    682 	 * realloc() with zero size is the same as free().
    683 	 */
    684 	if (newsize == 0) {
    685 		free(curaddr, ksp);
    686 		return (NULL);
    687 	}
    688 
    689 #ifdef LOCKDEBUG
    690 	if ((flags & M_NOWAIT) == 0)
    691 		ASSERT_SLEEPABLE(NULL, "realloc");
    692 #endif
    693 
    694 	/*
    695 	 * Find out how large the old allocation was (and do some
    696 	 * sanity checking).
    697 	 */
    698 	kup = btokup(curaddr);
    699 	cursize = 1 << kup->ku_indx;
    700 
    701 #ifdef DIAGNOSTIC
    702 	/*
    703 	 * Check for returns of data that do not point to the
    704 	 * beginning of the allocation.
    705 	 */
    706 	if (cursize > PAGE_SIZE)
    707 		alloc = addrmask[BUCKETINDX(PAGE_SIZE)];
    708 	else
    709 		alloc = addrmask[kup->ku_indx];
    710 	if (((u_long)curaddr & alloc) != 0)
    711 		panic("realloc: "
    712 		    "unaligned addr %p, size %ld, type %s, mask %ld\n",
    713 		    curaddr, cursize, ksp->ks_shortdesc, alloc);
    714 #endif /* DIAGNOSTIC */
    715 
    716 	if (cursize > MAXALLOCSAVE)
    717 		cursize = ctob(kup->ku_pagecnt);
    718 
    719 	/*
    720 	 * If we already actually have as much as they want, we're done.
    721 	 */
    722 	if (newsize <= cursize)
    723 		return (curaddr);
    724 
    725 	/*
    726 	 * Can't satisfy the allocation with the existing block.
    727 	 * Allocate a new one and copy the data.
    728 	 */
    729 	newaddr = malloc(newsize, ksp, flags);
    730 	if (__predict_false(newaddr == NULL)) {
    731 		/*
    732 		 * malloc() failed, because flags included M_NOWAIT.
    733 		 * Return NULL to indicate that failure.  The old
    734 		 * pointer is still valid.
    735 		 */
    736 		return (NULL);
    737 	}
    738 	memcpy(newaddr, curaddr, cursize);
    739 
    740 	/*
    741 	 * We were successful: free the old allocation and return
    742 	 * the new one.
    743 	 */
    744 	free(curaddr, ksp);
    745 	return (newaddr);
    746 }
    747 
    748 /*
    749  * Roundup size to the actual allocation size.
    750  */
    751 unsigned long
    752 malloc_roundup(unsigned long size)
    753 {
    754 
    755 	if (size > MAXALLOCSAVE)
    756 		return (roundup(size, PAGE_SIZE));
    757 	else
    758 		return (1 << BUCKETINDX(size));
    759 }
    760 
    761 /*
    762  * Add a malloc type to the system.
    763  */
    764 void
    765 malloc_type_attach(struct malloc_type *type)
    766 {
    767 
    768 	if (nkmempages == 0)
    769 		panic("malloc_type_attach: nkmempages == 0");
    770 
    771 	if (type->ks_magic != M_MAGIC)
    772 		panic("malloc_type_attach: bad magic");
    773 
    774 #ifdef DIAGNOSTIC
    775 	{
    776 		struct malloc_type *ksp;
    777 		for (ksp = kmemstatistics; ksp != NULL; ksp = ksp->ks_next) {
    778 			if (ksp == type)
    779 				panic("malloc_type_attach: already on list");
    780 		}
    781 	}
    782 #endif
    783 
    784 #ifdef KMEMSTATS
    785 	if (type->ks_limit == 0)
    786 		type->ks_limit = ((u_long)nkmempages << PAGE_SHIFT) * 6U / 10U;
    787 #else
    788 	type->ks_limit = 0;
    789 #endif
    790 
    791 	type->ks_next = kmemstatistics;
    792 	kmemstatistics = type;
    793 }
    794 
    795 /*
    796  * Remove a malloc type from the system..
    797  */
    798 void
    799 malloc_type_detach(struct malloc_type *type)
    800 {
    801 	struct malloc_type *ksp;
    802 
    803 #ifdef DIAGNOSTIC
    804 	if (type->ks_magic != M_MAGIC)
    805 		panic("malloc_type_detach: bad magic");
    806 #endif
    807 
    808 	if (type == kmemstatistics)
    809 		kmemstatistics = type->ks_next;
    810 	else {
    811 		for (ksp = kmemstatistics; ksp->ks_next != NULL;
    812 		     ksp = ksp->ks_next) {
    813 			if (ksp->ks_next == type) {
    814 				ksp->ks_next = type->ks_next;
    815 				break;
    816 			}
    817 		}
    818 #ifdef DIAGNOSTIC
    819 		if (ksp->ks_next == NULL)
    820 			panic("malloc_type_detach: not on list");
    821 #endif
    822 	}
    823 	type->ks_next = NULL;
    824 }
    825 
    826 /*
    827  * Set the limit on a malloc type.
    828  */
    829 void
    830 malloc_type_setlimit(struct malloc_type *type, u_long limit)
    831 {
    832 #ifdef KMEMSTATS
    833 	mutex_spin_enter(&malloc_lock);
    834 	type->ks_limit = limit;
    835 	mutex_spin_exit(&malloc_lock);
    836 #endif
    837 }
    838 
    839 /*
    840  * Compute the number of pages that kmem_map will map, that is,
    841  * the size of the kernel malloc arena.
    842  */
    843 void
    844 kmeminit_nkmempages(void)
    845 {
    846 	int npages;
    847 
    848 	if (nkmempages != 0) {
    849 		/*
    850 		 * It's already been set (by us being here before, or
    851 		 * by patching or kernel config options), bail out now.
    852 		 */
    853 		return;
    854 	}
    855 
    856 	npages = physmem;
    857 
    858 	if (npages > NKMEMPAGES_MAX)
    859 		npages = NKMEMPAGES_MAX;
    860 
    861 	if (npages < NKMEMPAGES_MIN)
    862 		npages = NKMEMPAGES_MIN;
    863 
    864 	nkmempages = npages;
    865 }
    866 
    867 /*
    868  * Initialize the kernel memory allocator
    869  */
    870 void
    871 kmeminit(void)
    872 {
    873 	__link_set_decl(malloc_types, struct malloc_type);
    874 	struct malloc_type * const *ksp;
    875 	vaddr_t kmb, kml;
    876 #ifdef KMEMSTATS
    877 	long indx;
    878 #endif
    879 
    880 #if	((MAXALLOCSAVE & (MAXALLOCSAVE - 1)) != 0)
    881 		ERROR!_kmeminit:_MAXALLOCSAVE_not_power_of_2
    882 #endif
    883 #if	(MAXALLOCSAVE > MINALLOCSIZE * 32768)
    884 		ERROR!_kmeminit:_MAXALLOCSAVE_too_big
    885 #endif
    886 #if	(MAXALLOCSAVE < NBPG)
    887 		ERROR!_kmeminit:_MAXALLOCSAVE_too_small
    888 #endif
    889 
    890 	if (sizeof(struct freelist) > (1 << MINBUCKET))
    891 		panic("minbucket too small/struct freelist too big");
    892 
    893 	mutex_init(&malloc_lock, MUTEX_DRIVER, IPL_VM);
    894 
    895 	/*
    896 	 * Compute the number of kmem_map pages, if we have not
    897 	 * done so already.
    898 	 */
    899 	kmeminit_nkmempages();
    900 
    901 	kmemusage = (struct kmemusage *) uvm_km_alloc(kernel_map,
    902 	    (vsize_t)(nkmempages * sizeof(struct kmemusage)), 0,
    903 	    UVM_KMF_WIRED|UVM_KMF_ZERO);
    904 	kmb = 0;
    905 	kmem_map = uvm_km_suballoc(kernel_map, &kmb,
    906 	    &kml, ((vsize_t)nkmempages << PAGE_SHIFT),
    907 	    VM_MAP_INTRSAFE, false, &kmem_map_store);
    908 	uvm_km_vacache_init(kmem_map, "kvakmem", 0);
    909 	kmembase = (char *)kmb;
    910 	kmemlimit = (char *)kml;
    911 #ifdef KMEMSTATS
    912 	for (indx = 0; indx < MINBUCKET + 16; indx++) {
    913 		if (1 << indx >= PAGE_SIZE)
    914 			kmembuckets[indx].kb_elmpercl = 1;
    915 		else
    916 			kmembuckets[indx].kb_elmpercl = PAGE_SIZE / (1 << indx);
    917 		kmembuckets[indx].kb_highwat =
    918 			5 * kmembuckets[indx].kb_elmpercl;
    919 	}
    920 #endif
    921 
    922 	/* Attach all of the statically-linked malloc types. */
    923 	__link_set_foreach(ksp, malloc_types)
    924 		malloc_type_attach(*ksp);
    925 
    926 #ifdef MALLOC_DEBUG
    927 	debug_malloc_init();
    928 #endif
    929 }
    930 
    931 #ifdef DDB
    932 #include <ddb/db_output.h>
    933 
    934 /*
    935  * Dump kmem statistics from ddb.
    936  *
    937  * usage: call dump_kmemstats
    938  */
    939 void	dump_kmemstats(void);
    940 
    941 void
    942 dump_kmemstats(void)
    943 {
    944 #ifdef KMEMSTATS
    945 	struct malloc_type *ksp;
    946 
    947 	for (ksp = kmemstatistics; ksp != NULL; ksp = ksp->ks_next) {
    948 		if (ksp->ks_memuse == 0)
    949 			continue;
    950 		db_printf("%s%.*s %ld\n", ksp->ks_shortdesc,
    951 		    (int)(20 - strlen(ksp->ks_shortdesc)),
    952 		    "                    ",
    953 		    ksp->ks_memuse);
    954 	}
    955 #else
    956 	db_printf("Kmem stats are not being collected.\n");
    957 #endif /* KMEMSTATS */
    958 }
    959 #endif /* DDB */
    960 
    961 
    962 #if 0
    963 /*
    964  * Diagnostic messages about "Data modified on
    965  * freelist" indicate a memory corruption, but
    966  * they do not help tracking it down.
    967  * This function can be called at various places
    968  * to sanity check malloc's freelist and discover
    969  * where does the corruption take place.
    970  */
    971 int
    972 freelist_sanitycheck(void) {
    973 	int i,j;
    974 	struct kmembuckets *kbp;
    975 	struct freelist *freep;
    976 	int rv = 0;
    977 
    978 	for (i = MINBUCKET; i <= MINBUCKET + 15; i++) {
    979 		kbp = &kmembuckets[i];
    980 		freep = (struct freelist *)kbp->kb_next;
    981 		j = 0;
    982 		while(freep) {
    983 			vm_map_lock(kmem_map);
    984 			rv = uvm_map_checkprot(kmem_map, (vaddr_t)freep,
    985 			    (vaddr_t)freep + sizeof(struct freelist),
    986 			    VM_PROT_WRITE);
    987 			vm_map_unlock(kmem_map);
    988 
    989 			if ((rv == 0) || (*(int *)freep != WEIRD_ADDR)) {
    990 				printf("bucket %i, chunck %d at %p modified\n",
    991 				    i, j, freep);
    992 				return 1;
    993 			}
    994 			freep = (struct freelist *)freep->next;
    995 			j++;
    996 		}
    997 	}
    998 
    999 	return 0;
   1000 }
   1001 #endif
   1002