Home | History | Annotate | Line # | Download | only in kern
kern_malloc.c revision 1.70.4.1
      1 /*	$NetBSD: kern_malloc.c,v 1.70.4.1 2002/03/22 18:59:31 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1996 Christopher G. Demetriou.  All rights reserved.
      5  * Copyright (c) 1987, 1991, 1993
      6  *	The Regents of the University of California.  All rights reserved.
      7  *
      8  * Redistribution and use in source and binary forms, with or without
      9  * modification, are permitted provided that the following conditions
     10  * are met:
     11  * 1. Redistributions of source code must retain the above copyright
     12  *    notice, this list of conditions and the following disclaimer.
     13  * 2. Redistributions in binary form must reproduce the above copyright
     14  *    notice, this list of conditions and the following disclaimer in the
     15  *    documentation and/or other materials provided with the distribution.
     16  * 3. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by the University of
     19  *	California, Berkeley and its contributors.
     20  * 4. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  *
     36  *	@(#)kern_malloc.c	8.4 (Berkeley) 5/20/95
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: kern_malloc.c,v 1.70.4.1 2002/03/22 18:59:31 thorpej Exp $");
     41 
     42 #include "opt_lockdebug.h"
     43 
     44 #include <sys/param.h>
     45 #include <sys/proc.h>
     46 #include <sys/map.h>
     47 #include <sys/kernel.h>
     48 #include <sys/malloc.h>
     49 #include <sys/systm.h>
     50 
     51 #include <uvm/uvm_extern.h>
     52 
     53 static struct vm_map kmem_map_store;
     54 struct vm_map *kmem_map = NULL;
     55 
     56 kmutex_t malloc_mutex;
     57 
     58 #include "opt_kmempages.h"
     59 
     60 #ifdef NKMEMCLUSTERS
     61 #error NKMEMCLUSTERS is obsolete; remove it from your kernel config file and use NKMEMPAGES instead or let the kernel auto-size
     62 #endif
     63 
     64 /*
     65  * Default number of pages in kmem_map.  We attempt to calculate this
     66  * at run-time, but allow it to be either patched or set in the kernel
     67  * config file.
     68  */
     69 #ifndef NKMEMPAGES
     70 #define	NKMEMPAGES	0
     71 #endif
     72 int	nkmempages = NKMEMPAGES;
     73 
     74 /*
     75  * Defaults for lower- and upper-bounds for the kmem_map page count.
     76  * Can be overridden by kernel config options.
     77  */
     78 #ifndef	NKMEMPAGES_MIN
     79 #define	NKMEMPAGES_MIN	NKMEMPAGES_MIN_DEFAULT
     80 #endif
     81 
     82 #ifndef NKMEMPAGES_MAX
     83 #define	NKMEMPAGES_MAX	NKMEMPAGES_MAX_DEFAULT
     84 #endif
     85 
     86 #include "opt_kmemstats.h"
     87 #include "opt_malloclog.h"
     88 
     89 struct kmembuckets bucket[MINBUCKET + 16];
     90 struct kmemstats kmemstats[M_LAST];
     91 struct kmemusage *kmemusage;
     92 char *kmembase, *kmemlimit;
     93 const char * const memname[] = INITKMEMNAMES;
     94 
     95 #ifdef MALLOCLOG
     96 #ifndef MALLOCLOGSIZE
     97 #define	MALLOCLOGSIZE	100000
     98 #endif
     99 
    100 struct malloclog {
    101 	void *addr;
    102 	long size;
    103 	int type;
    104 	int action;
    105 	const char *file;
    106 	long line;
    107 } malloclog[MALLOCLOGSIZE];
    108 
    109 long	malloclogptr;
    110 
    111 static void domlog(void *, long, int, int, const char *, long);
    112 static void hitmlog(void *);
    113 
    114 static void
    115 domlog(void *a, long size, int type, int action, const char *file, long line)
    116 {
    117 
    118 	malloclog[malloclogptr].addr = a;
    119 	malloclog[malloclogptr].size = size;
    120 	malloclog[malloclogptr].type = type;
    121 	malloclog[malloclogptr].action = action;
    122 	malloclog[malloclogptr].file = file;
    123 	malloclog[malloclogptr].line = line;
    124 	malloclogptr++;
    125 	if (malloclogptr >= MALLOCLOGSIZE)
    126 		malloclogptr = 0;
    127 }
    128 
    129 static void
    130 hitmlog(void *a)
    131 {
    132 	struct malloclog *lp;
    133 	long l;
    134 
    135 #define	PRT do { \
    136 	if (malloclog[l].addr == a && malloclog[l].action) { \
    137 		lp = &malloclog[l]; \
    138 		printf("malloc log entry %ld:\n", l); \
    139 		printf("\taddr = %p\n", lp->addr); \
    140 		printf("\tsize = %ld\n", lp->size); \
    141 		printf("\ttype = %s\n", memname[lp->type]); \
    142 		printf("\taction = %s\n", lp->action == 1 ? "alloc" : "free"); \
    143 		printf("\tfile = %s\n", lp->file); \
    144 		printf("\tline = %ld\n", lp->line); \
    145 	} \
    146 } while (/* CONSTCOND */0)
    147 
    148 	for (l = malloclogptr; l < MALLOCLOGSIZE; l++)
    149 		PRT;
    150 
    151 	for (l = 0; l < malloclogptr; l++)
    152 		PRT;
    153 }
    154 #endif /* MALLOCLOG */
    155 
    156 #ifdef DIAGNOSTIC
    157 /*
    158  * This structure provides a set of masks to catch unaligned frees.
    159  */
    160 const long addrmask[] = { 0,
    161 	0x00000001, 0x00000003, 0x00000007, 0x0000000f,
    162 	0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff,
    163 	0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff,
    164 	0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff,
    165 };
    166 
    167 /*
    168  * The WEIRD_ADDR is used as known text to copy into free objects so
    169  * that modifications after frees can be detected.
    170  */
    171 #define	WEIRD_ADDR	((unsigned) 0xdeadbeef)
    172 #ifdef DEBUG
    173 #define	MAX_COPY	PAGE_SIZE
    174 #else
    175 #define	MAX_COPY	32
    176 #endif
    177 
    178 /*
    179  * Normally the freelist structure is used only to hold the list pointer
    180  * for free objects.  However, when running with diagnostics, the first
    181  * 8 bytes of the structure is unused except for diagnostic information,
    182  * and the free list pointer is at offst 8 in the structure.  Since the
    183  * first 8 bytes is the portion of the structure most often modified, this
    184  * helps to detect memory reuse problems and avoid free list corruption.
    185  */
    186 struct freelist {
    187 	int32_t	spare0;
    188 	int16_t	type;
    189 	int16_t	spare1;
    190 	caddr_t	next;
    191 };
    192 #else /* !DIAGNOSTIC */
    193 struct freelist {
    194 	caddr_t	next;
    195 };
    196 #endif /* DIAGNOSTIC */
    197 
    198 /*
    199  * Allocate a block of memory
    200  */
    201 #ifdef MALLOCLOG
    202 void *
    203 _malloc(unsigned long size, int type, int flags, const char *file, long line)
    204 #else
    205 void *
    206 malloc(unsigned long size, int type, int flags)
    207 #endif /* MALLOCLOG */
    208 {
    209 	struct kmembuckets *kbp;
    210 	struct kmemusage *kup;
    211 	struct freelist *freep;
    212 	long indx, npg, allocsize;
    213 	caddr_t va, cp, savedlist;
    214 #ifdef DIAGNOSTIC
    215 	int32_t *end, *lp;
    216 	int copysize;
    217 	const char *savedtype;
    218 #endif
    219 #ifdef KMEMSTATS
    220 	struct kmemstats *ksp = &kmemstats[type];
    221 
    222 	if (__predict_false(((unsigned long)type) > M_LAST))
    223 		panic("malloc - bogus type");
    224 #endif
    225 #ifdef LOCKDEBUG
    226 	if ((flags & M_NOWAIT) == 0)
    227 		simple_lock_only_held(NULL, "malloc");
    228 #endif
    229 #ifdef MALLOC_DEBUG
    230 	if (debug_malloc(size, type, flags, (void **) &va))
    231 		return ((void *) va);
    232 #endif
    233 	indx = BUCKETINDX(size);
    234 	kbp = &bucket[indx];
    235 
    236 	mutex_enter(&malloc_mutex);
    237 
    238 #ifdef KMEMSTATS
    239 	while (ksp->ks_memuse >= ksp->ks_limit) {
    240 		if (flags & M_NOWAIT) {
    241 			mutex_exit(&malloc_mutex);
    242 			return ((void *) NULL);
    243 		}
    244 		if (ksp->ks_limblocks < 65535)
    245 			ksp->ks_limblocks++;
    246 		tsleep((caddr_t)ksp, PSWP+2, memname[type], 0);
    247 	}
    248 	ksp->ks_size |= 1 << indx;
    249 #endif
    250 #ifdef DIAGNOSTIC
    251 	copysize = 1 << indx < MAX_COPY ? 1 << indx : MAX_COPY;
    252 #endif
    253 	if (kbp->kb_next == NULL) {
    254 		kbp->kb_last = NULL;
    255 		if (size > MAXALLOCSAVE)
    256 			allocsize = round_page(size);
    257 		else
    258 			allocsize = 1 << indx;
    259 		npg = btoc(allocsize);
    260 		va = (caddr_t) uvm_km_kmemalloc(kmem_map, NULL,
    261 		    (vsize_t)ctob(npg),
    262 		    (flags & M_NOWAIT) ? UVM_KMF_NOWAIT : 0);
    263 		if (__predict_false(va == NULL)) {
    264 			/*
    265 			 * Kmem_malloc() can return NULL, even if it can
    266 			 * wait, if there is no map space avaiable, because
    267 			 * it can't fix that problem.  Neither can we,
    268 			 * right now.  (We should release pages which
    269 			 * are completely free and which are in buckets
    270 			 * with too many free elements.)
    271 			 */
    272 			if ((flags & (M_NOWAIT|M_CANFAIL)) == 0)
    273 				panic("malloc: out of space in kmem_map");
    274 			mutex_exit(&malloc_mutex);
    275 			return ((void *) NULL);
    276 		}
    277 #ifdef KMEMSTATS
    278 		kbp->kb_total += kbp->kb_elmpercl;
    279 #endif
    280 		kup = btokup(va);
    281 		kup->ku_indx = indx;
    282 		if (allocsize > MAXALLOCSAVE) {
    283 			if (npg > 65535)
    284 				panic("malloc: allocation too large");
    285 			kup->ku_pagecnt = npg;
    286 #ifdef KMEMSTATS
    287 			ksp->ks_memuse += allocsize;
    288 #endif
    289 			goto out;
    290 		}
    291 #ifdef KMEMSTATS
    292 		kup->ku_freecnt = kbp->kb_elmpercl;
    293 		kbp->kb_totalfree += kbp->kb_elmpercl;
    294 #endif
    295 		/*
    296 		 * Just in case we blocked while allocating memory,
    297 		 * and someone else also allocated memory for this
    298 		 * bucket, don't assume the list is still empty.
    299 		 */
    300 		savedlist = kbp->kb_next;
    301 		kbp->kb_next = cp = va + (npg << PAGE_SHIFT) - allocsize;
    302 		for (;;) {
    303 			freep = (struct freelist *)cp;
    304 #ifdef DIAGNOSTIC
    305 			/*
    306 			 * Copy in known text to detect modification
    307 			 * after freeing.
    308 			 */
    309 			end = (int32_t *)&cp[copysize];
    310 			for (lp = (int32_t *)cp; lp < end; lp++)
    311 				*lp = WEIRD_ADDR;
    312 			freep->type = M_FREE;
    313 #endif /* DIAGNOSTIC */
    314 			if (cp <= va)
    315 				break;
    316 			cp -= allocsize;
    317 			freep->next = cp;
    318 		}
    319 		freep->next = savedlist;
    320 		if (kbp->kb_last == NULL)
    321 			kbp->kb_last = (caddr_t)freep;
    322 	}
    323 	va = kbp->kb_next;
    324 	kbp->kb_next = ((struct freelist *)va)->next;
    325 #ifdef DIAGNOSTIC
    326 	freep = (struct freelist *)va;
    327 	savedtype = (unsigned)freep->type < M_LAST ?
    328 		memname[freep->type] : "???";
    329 	if (kbp->kb_next) {
    330 		int rv;
    331 		vaddr_t addr = (vaddr_t)kbp->kb_next;
    332 
    333 		vm_map_lock(kmem_map);
    334 		rv = uvm_map_checkprot(kmem_map, addr,
    335 		    addr + sizeof(struct freelist), VM_PROT_WRITE);
    336 		vm_map_unlock(kmem_map);
    337 
    338 		if (__predict_false(rv == 0)) {
    339 			printf("Data modified on freelist: "
    340 			    "word %ld of object %p size %ld previous type %s "
    341 			    "(invalid addr %p)\n",
    342 			    (long)((int32_t *)&kbp->kb_next - (int32_t *)kbp),
    343 			    va, size, savedtype, kbp->kb_next);
    344 #ifdef MALLOCLOG
    345 			hitmlog(va);
    346 #endif
    347 			kbp->kb_next = NULL;
    348 		}
    349 	}
    350 
    351 	/* Fill the fields that we've used with WEIRD_ADDR */
    352 #if BYTE_ORDER == BIG_ENDIAN
    353 	freep->type = WEIRD_ADDR >> 16;
    354 #endif
    355 #if BYTE_ORDER == LITTLE_ENDIAN
    356 	freep->type = (short)WEIRD_ADDR;
    357 #endif
    358 	end = (int32_t *)&freep->next +
    359 	    (sizeof(freep->next) / sizeof(int32_t));
    360 	for (lp = (int32_t *)&freep->next; lp < end; lp++)
    361 		*lp = WEIRD_ADDR;
    362 
    363 	/* and check that the data hasn't been modified. */
    364 	end = (int32_t *)&va[copysize];
    365 	for (lp = (int32_t *)va; lp < end; lp++) {
    366 		if (__predict_true(*lp == WEIRD_ADDR))
    367 			continue;
    368 		printf("Data modified on freelist: "
    369 		    "word %ld of object %p size %ld previous type %s "
    370 		    "(0x%x != 0x%x)\n",
    371 		    (long)(lp - (int32_t *)va), va, size,
    372 		    savedtype, *lp, WEIRD_ADDR);
    373 #ifdef MALLOCLOG
    374 		hitmlog(va);
    375 #endif
    376 		break;
    377 	}
    378 
    379 	freep->spare0 = 0;
    380 #endif /* DIAGNOSTIC */
    381 #ifdef KMEMSTATS
    382 	kup = btokup(va);
    383 	if (kup->ku_indx != indx)
    384 		panic("malloc: wrong bucket");
    385 	if (kup->ku_freecnt == 0)
    386 		panic("malloc: lost data");
    387 	kup->ku_freecnt--;
    388 	kbp->kb_totalfree--;
    389 	ksp->ks_memuse += 1 << indx;
    390 out:
    391 	kbp->kb_calls++;
    392 	ksp->ks_inuse++;
    393 	ksp->ks_calls++;
    394 	if (ksp->ks_memuse > ksp->ks_maxused)
    395 		ksp->ks_maxused = ksp->ks_memuse;
    396 #else
    397 out:
    398 #endif
    399 #ifdef MALLOCLOG
    400 	domlog(va, size, type, 1, file, line);
    401 #endif
    402 	mutex_exit(&malloc_mutex);
    403 	if ((flags & M_ZERO) != 0)
    404 		memset(va, 0, size);
    405 	return ((void *) va);
    406 }
    407 
    408 /*
    409  * Free a block of memory allocated by malloc.
    410  */
    411 #ifdef MALLOCLOG
    412 void
    413 _free(void *addr, int type, const char *file, long line)
    414 #else
    415 void
    416 free(void *addr, int type)
    417 #endif /* MALLOCLOG */
    418 {
    419 	struct kmembuckets *kbp;
    420 	struct kmemusage *kup;
    421 	struct freelist *freep;
    422 	long size;
    423 #ifdef DIAGNOSTIC
    424 	caddr_t cp;
    425 	int32_t *end, *lp;
    426 	long alloc, copysize;
    427 #endif
    428 #ifdef KMEMSTATS
    429 	struct kmemstats *ksp = &kmemstats[type];
    430 #endif
    431 
    432 #ifdef MALLOC_DEBUG
    433 	if (debug_free(addr, type))
    434 		return;
    435 #endif
    436 
    437 #ifdef DIAGNOSTIC
    438 	/*
    439 	 * Ensure that we're free'ing something that we could
    440 	 * have allocated in the first place.  That is, check
    441 	 * to see that the address is within kmem_map.
    442 	 */
    443 	if (__predict_false((vaddr_t)addr < kmem_map->header.start ||
    444 	    (vaddr_t)addr >= kmem_map->header.end))
    445 		panic("free: addr %p not within kmem_map", addr);
    446 #endif
    447 
    448 	kup = btokup(addr);
    449 	size = 1 << kup->ku_indx;
    450 	kbp = &bucket[kup->ku_indx];
    451 
    452 	mutex_enter(&malloc_mutex);
    453 
    454 #ifdef MALLOCLOG
    455 	domlog(addr, 0, type, 2, file, line);
    456 #endif
    457 #ifdef DIAGNOSTIC
    458 	/*
    459 	 * Check for returns of data that do not point to the
    460 	 * beginning of the allocation.
    461 	 */
    462 	if (size > PAGE_SIZE)
    463 		alloc = addrmask[BUCKETINDX(PAGE_SIZE)];
    464 	else
    465 		alloc = addrmask[kup->ku_indx];
    466 	if (((u_long)addr & alloc) != 0)
    467 		panic("free: unaligned addr %p, size %ld, type %s, mask %ld\n",
    468 		    addr, size, memname[type], alloc);
    469 #endif /* DIAGNOSTIC */
    470 	if (size > MAXALLOCSAVE) {
    471 		uvm_km_free(kmem_map, (vaddr_t)addr, ctob(kup->ku_pagecnt));
    472 #ifdef KMEMSTATS
    473 		size = kup->ku_pagecnt << PGSHIFT;
    474 		ksp->ks_memuse -= size;
    475 		kup->ku_indx = 0;
    476 		kup->ku_pagecnt = 0;
    477 		if (ksp->ks_memuse + size >= ksp->ks_limit &&
    478 		    ksp->ks_memuse < ksp->ks_limit)
    479 			wakeup((caddr_t)ksp);
    480 		ksp->ks_inuse--;
    481 		kbp->kb_total -= 1;
    482 #endif
    483 		mutex_exit(&malloc_mutex);
    484 		return;
    485 	}
    486 	freep = (struct freelist *)addr;
    487 #ifdef DIAGNOSTIC
    488 	/*
    489 	 * Check for multiple frees. Use a quick check to see if
    490 	 * it looks free before laboriously searching the freelist.
    491 	 */
    492 	if (__predict_false(freep->spare0 == WEIRD_ADDR)) {
    493 		for (cp = kbp->kb_next; cp;
    494 		    cp = ((struct freelist *)cp)->next) {
    495 			if (addr != cp)
    496 				continue;
    497 			printf("multiply freed item %p\n", addr);
    498 #ifdef MALLOCLOG
    499 			hitmlog(addr);
    500 #endif
    501 			panic("free: duplicated free");
    502 		}
    503 	}
    504 #ifdef LOCKDEBUG
    505 	/*
    506 	 * Check if we're freeing a locked simple lock.
    507 	 */
    508 	simple_lock_freecheck(addr, (char *)addr + size);
    509 #endif
    510 	/*
    511 	 * Copy in known text to detect modification after freeing
    512 	 * and to make it look free. Also, save the type being freed
    513 	 * so we can list likely culprit if modification is detected
    514 	 * when the object is reallocated.
    515 	 */
    516 	copysize = size < MAX_COPY ? size : MAX_COPY;
    517 	end = (int32_t *)&((caddr_t)addr)[copysize];
    518 	for (lp = (int32_t *)addr; lp < end; lp++)
    519 		*lp = WEIRD_ADDR;
    520 	freep->type = type;
    521 #endif /* DIAGNOSTIC */
    522 #ifdef KMEMSTATS
    523 	kup->ku_freecnt++;
    524 	if (kup->ku_freecnt >= kbp->kb_elmpercl) {
    525 		if (kup->ku_freecnt > kbp->kb_elmpercl)
    526 			panic("free: multiple frees");
    527 		else if (kbp->kb_totalfree > kbp->kb_highwat)
    528 			kbp->kb_couldfree++;
    529 	}
    530 	kbp->kb_totalfree++;
    531 	ksp->ks_memuse -= size;
    532 	if (ksp->ks_memuse + size >= ksp->ks_limit &&
    533 	    ksp->ks_memuse < ksp->ks_limit)
    534 		wakeup((caddr_t)ksp);
    535 	ksp->ks_inuse--;
    536 #endif
    537 	if (kbp->kb_next == NULL)
    538 		kbp->kb_next = addr;
    539 	else
    540 		((struct freelist *)kbp->kb_last)->next = addr;
    541 	freep->next = NULL;
    542 	kbp->kb_last = addr;
    543 	mutex_exit(&malloc_mutex);
    544 }
    545 
    546 /*
    547  * Change the size of a block of memory.
    548  */
    549 void *
    550 realloc(void *curaddr, unsigned long newsize, int type, int flags)
    551 {
    552 	struct kmemusage *kup;
    553 	long cursize;
    554 	void *newaddr;
    555 #ifdef DIAGNOSTIC
    556 	long alloc;
    557 #endif
    558 
    559 	/*
    560 	 * realloc() with a NULL pointer is the same as malloc().
    561 	 */
    562 	if (curaddr == NULL)
    563 		return (malloc(newsize, type, flags));
    564 
    565 	/*
    566 	 * realloc() with zero size is the same as free().
    567 	 */
    568 	if (newsize == 0) {
    569 		free(curaddr, type);
    570 		return (NULL);
    571 	}
    572 
    573 #ifdef LOCKDEBUG
    574 	if ((flags & M_NOWAIT) == 0)
    575 		simple_lock_only_held(NULL, "realloc");
    576 #endif
    577 
    578 	/*
    579 	 * Find out how large the old allocation was (and do some
    580 	 * sanity checking).
    581 	 */
    582 	kup = btokup(curaddr);
    583 	cursize = 1 << kup->ku_indx;
    584 
    585 #ifdef DIAGNOSTIC
    586 	/*
    587 	 * Check for returns of data that do not point to the
    588 	 * beginning of the allocation.
    589 	 */
    590 	if (cursize > PAGE_SIZE)
    591 		alloc = addrmask[BUCKETINDX(PAGE_SIZE)];
    592 	else
    593 		alloc = addrmask[kup->ku_indx];
    594 	if (((u_long)curaddr & alloc) != 0)
    595 		panic("realloc: "
    596 		    "unaligned addr %p, size %ld, type %s, mask %ld\n",
    597 		    curaddr, cursize, memname[type], alloc);
    598 #endif /* DIAGNOSTIC */
    599 
    600 	if (cursize > MAXALLOCSAVE)
    601 		cursize = ctob(kup->ku_pagecnt);
    602 
    603 	/*
    604 	 * If we already actually have as much as they want, we're done.
    605 	 */
    606 	if (newsize <= cursize)
    607 		return (curaddr);
    608 
    609 	/*
    610 	 * Can't satisfy the allocation with the existing block.
    611 	 * Allocate a new one and copy the data.
    612 	 */
    613 	newaddr = malloc(newsize, type, flags);
    614 	if (__predict_false(newaddr == NULL)) {
    615 		/*
    616 		 * malloc() failed, because flags included M_NOWAIT.
    617 		 * Return NULL to indicate that failure.  The old
    618 		 * pointer is still valid.
    619 		 */
    620 		return (NULL);
    621 	}
    622 	memcpy(newaddr, curaddr, cursize);
    623 
    624 	/*
    625 	 * We were successful: free the old allocation and return
    626 	 * the new one.
    627 	 */
    628 	free(curaddr, type);
    629 	return (newaddr);
    630 }
    631 
    632 /*
    633  * Roundup size to the actual allocation size.
    634  */
    635 unsigned long
    636 malloc_roundup(unsigned long size)
    637 {
    638 
    639 	if (size > MAXALLOCSAVE)
    640 		return (roundup(size, PAGE_SIZE));
    641 	else
    642 		return (1 << BUCKETINDX(size));
    643 }
    644 
    645 /*
    646  * Compute the number of pages that kmem_map will map, that is,
    647  * the size of the kernel malloc arena.
    648  */
    649 void
    650 kmeminit_nkmempages(void)
    651 {
    652 	int npages;
    653 
    654 	if (nkmempages != 0) {
    655 		/*
    656 		 * It's already been set (by us being here before, or
    657 		 * by patching or kernel config options), bail out now.
    658 		 */
    659 		return;
    660 	}
    661 
    662 	/*
    663 	 * We use the following (simple) formula:
    664 	 *
    665 	 *	- Starting point is physical memory / 4.
    666 	 *
    667 	 *	- Clamp it down to NKMEMPAGES_MAX.
    668 	 *
    669 	 *	- Round it up to NKMEMPAGES_MIN.
    670 	 */
    671 	npages = physmem / 4;
    672 
    673 	if (npages > NKMEMPAGES_MAX)
    674 		npages = NKMEMPAGES_MAX;
    675 
    676 	if (npages < NKMEMPAGES_MIN)
    677 		npages = NKMEMPAGES_MIN;
    678 
    679 	nkmempages = npages;
    680 }
    681 
    682 /*
    683  * Initialize the kernel memory allocator
    684  */
    685 void
    686 kmeminit(void)
    687 {
    688 #ifdef KMEMSTATS
    689 	long indx;
    690 #endif
    691 
    692 #if	((MAXALLOCSAVE & (MAXALLOCSAVE - 1)) != 0)
    693 		ERROR!_kmeminit:_MAXALLOCSAVE_not_power_of_2
    694 #endif
    695 #if	(MAXALLOCSAVE > MINALLOCSIZE * 32768)
    696 		ERROR!_kmeminit:_MAXALLOCSAVE_too_big
    697 #endif
    698 #if	(MAXALLOCSAVE < NBPG)
    699 		ERROR!_kmeminit:_MAXALLOCSAVE_too_small
    700 #endif
    701 
    702 	if (sizeof(struct freelist) > (1 << MINBUCKET))
    703 		panic("minbucket too small/struct freelist too big");
    704 
    705 	/*
    706 	 * Compute the number of kmem_map pages, if we have not
    707 	 * done so already.
    708 	 */
    709 	kmeminit_nkmempages();
    710 
    711 	kmemusage = (struct kmemusage *) uvm_km_zalloc(kernel_map,
    712 	    (vsize_t)(nkmempages * sizeof(struct kmemusage)));
    713 	kmem_map = uvm_km_suballoc(kernel_map, (vaddr_t *)&kmembase,
    714 	    (vaddr_t *)&kmemlimit, (vsize_t)(nkmempages << PAGE_SHIFT),
    715 	    VM_MAP_INTRSAFE, FALSE, &kmem_map_store);
    716 #ifdef KMEMSTATS
    717 	for (indx = 0; indx < MINBUCKET + 16; indx++) {
    718 		if (1 << indx >= PAGE_SIZE)
    719 			bucket[indx].kb_elmpercl = 1;
    720 		else
    721 			bucket[indx].kb_elmpercl = PAGE_SIZE / (1 << indx);
    722 		bucket[indx].kb_highwat = 5 * bucket[indx].kb_elmpercl;
    723 	}
    724 	for (indx = 0; indx < M_LAST; indx++)
    725 		kmemstats[indx].ks_limit =
    726 		    ((u_long)nkmempages << PAGE_SHIFT) * 6U / 10U;
    727 #endif
    728 #ifdef MALLOC_DEBUG
    729 	debug_malloc_init();
    730 #endif
    731 
    732 	mutex_init(&malloc_mutex, MUTEX_SPIN, IPL_VM);
    733 }
    734 
    735 #ifdef DDB
    736 #include <ddb/db_output.h>
    737 
    738 /*
    739  * Dump kmem statistics from ddb.
    740  *
    741  * usage: call dump_kmemstats
    742  */
    743 void	dump_kmemstats(void);
    744 
    745 void
    746 dump_kmemstats(void)
    747 {
    748 #ifdef KMEMSTATS
    749 	const char *name;
    750 	int i;
    751 
    752 	for (i = 0; i < M_LAST; i++) {
    753 		name = memname[i] ? memname[i] : "";
    754 
    755 		db_printf("%2d %s%.*s %ld\n", i, name,
    756 		    (int)(20 - strlen(name)), "                    ",
    757 		    kmemstats[i].ks_memuse);
    758 	}
    759 #else
    760 	db_printf("Kmem stats are not being collected.\n");
    761 #endif /* KMEMSTATS */
    762 }
    763 #endif /* DDB */
    764