Home | History | Annotate | Line # | Download | only in kern
kern_malloc.c revision 1.20
      1 /*	$NetBSD: kern_malloc.c,v 1.20 1996/08/27 20:01:42 cgd Exp $	*/
      2 
      3 /*
      4  * Copyright 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.3 (Berkeley) 1/4/94
     37  */
     38 
     39 #include <sys/param.h>
     40 #include <sys/proc.h>
     41 #include <sys/map.h>
     42 #include <sys/kernel.h>
     43 #include <sys/malloc.h>
     44 #include <sys/systm.h>
     45 
     46 #include <vm/vm.h>
     47 #include <vm/vm_kern.h>
     48 
     49 struct kmembuckets bucket[MINBUCKET + 16];
     50 struct kmemstats kmemstats[M_LAST];
     51 struct kmemusage *kmemusage;
     52 char *kmembase, *kmemlimit;
     53 char *memname[] = INITKMEMNAMES;
     54 
     55 #ifdef DIAGNOSTIC
     56 /*
     57  * This structure provides a set of masks to catch unaligned frees.
     58  */
     59 long addrmask[] = { 0,
     60 	0x00000001, 0x00000003, 0x00000007, 0x0000000f,
     61 	0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff,
     62 	0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff,
     63 	0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff,
     64 };
     65 
     66 /*
     67  * The WEIRD_ADDR is used as known text to copy into free objects so
     68  * that modifications after frees can be detected.
     69  */
     70 #define WEIRD_ADDR	((unsigned) 0xdeadbeef)
     71 #define MAX_COPY	32
     72 
     73 /*
     74  * Normally the freelist structure is used only to hold the list pointer
     75  * for free objects.  However, when running with diagnostics, the first
     76  * 8 bytes of the structure is unused except for diagnostic information,
     77  * and the free list pointer is at offst 8 in the structure.  Since the
     78  * first 8 bytes is the portion of the structure most often modified, this
     79  * helps to detect memory reuse problems and avoid free list corruption.
     80  */
     81 struct freelist {
     82 	int32_t	spare0;
     83 	int16_t	type;
     84 	int16_t	spare1;
     85 	caddr_t	next;
     86 };
     87 #else /* !DIAGNOSTIC */
     88 struct freelist {
     89 	caddr_t	next;
     90 };
     91 #endif /* DIAGNOSTIC */
     92 
     93 /*
     94  * Allocate a block of memory
     95  */
     96 void *
     97 malloc(size, type, flags)
     98 	unsigned long size;
     99 	int type, flags;
    100 {
    101 	register struct kmembuckets *kbp;
    102 	register struct kmemusage *kup;
    103 	register struct freelist *freep;
    104 	long indx, npg, allocsize;
    105 	int s;
    106 	caddr_t va, cp, savedlist;
    107 #ifdef DIAGNOSTIC
    108 	int32_t *end, *lp;
    109 	int copysize;
    110 	char *savedtype;
    111 #endif
    112 #ifdef KMEMSTATS
    113 	register struct kmemstats *ksp = &kmemstats[type];
    114 
    115 	if (((unsigned long)type) > M_LAST)
    116 		panic("malloc - bogus type");
    117 #endif
    118 	indx = BUCKETINDX(size);
    119 	kbp = &bucket[indx];
    120 	s = splimp();
    121 #ifdef KMEMSTATS
    122 	while (ksp->ks_memuse >= ksp->ks_limit) {
    123 		if (flags & M_NOWAIT) {
    124 			splx(s);
    125 			return ((void *) NULL);
    126 		}
    127 		if (ksp->ks_limblocks < 65535)
    128 			ksp->ks_limblocks++;
    129 		tsleep((caddr_t)ksp, PSWP+2, memname[type], 0);
    130 	}
    131 	ksp->ks_size |= 1 << indx;
    132 #endif
    133 #ifdef DIAGNOSTIC
    134 	copysize = 1 << indx < MAX_COPY ? 1 << indx : MAX_COPY;
    135 #endif
    136 	if (kbp->kb_next == NULL) {
    137 		kbp->kb_last = NULL;
    138 		if (size > MAXALLOCSAVE)
    139 			allocsize = roundup(size, CLBYTES);
    140 		else
    141 			allocsize = 1 << indx;
    142 		npg = clrnd(btoc(allocsize));
    143 		va = (caddr_t) kmem_malloc(kmem_map, (vm_size_t)ctob(npg),
    144 					   !(flags & M_NOWAIT));
    145 		if (va == NULL) {
    146 			/*
    147 			 * Kmem_malloc() can return NULL, even if it can
    148 			 * wait, if there is no map space avaiable, because
    149 			 * it can't fix that problem.  Neither can we,
    150 			 * right now.  (We should release pages which
    151 			 * are completely free and which are in buckets
    152 			 * with too many free elements.)
    153 			 */
    154 			if ((flags & M_NOWAIT) == 0)
    155 				panic("malloc: out of space in kmem_map");
    156 			splx(s);
    157 			return ((void *) NULL);
    158 		}
    159 #ifdef KMEMSTATS
    160 		kbp->kb_total += kbp->kb_elmpercl;
    161 #endif
    162 		kup = btokup(va);
    163 		kup->ku_indx = indx;
    164 		if (allocsize > MAXALLOCSAVE) {
    165 			if (npg > 65535)
    166 				panic("malloc: allocation too large");
    167 			kup->ku_pagecnt = npg;
    168 #ifdef KMEMSTATS
    169 			ksp->ks_memuse += allocsize;
    170 #endif
    171 			goto out;
    172 		}
    173 #ifdef KMEMSTATS
    174 		kup->ku_freecnt = kbp->kb_elmpercl;
    175 		kbp->kb_totalfree += kbp->kb_elmpercl;
    176 #endif
    177 		/*
    178 		 * Just in case we blocked while allocating memory,
    179 		 * and someone else also allocated memory for this
    180 		 * bucket, don't assume the list is still empty.
    181 		 */
    182 		savedlist = kbp->kb_next;
    183 		kbp->kb_next = cp = va + (npg * NBPG) - allocsize;
    184 		for (;;) {
    185 			freep = (struct freelist *)cp;
    186 #ifdef DIAGNOSTIC
    187 			/*
    188 			 * Copy in known text to detect modification
    189 			 * after freeing.
    190 			 */
    191 			end = (int32_t *)&cp[copysize];
    192 			for (lp = (int32_t *)cp; lp < end; lp++)
    193 				*lp = WEIRD_ADDR;
    194 			freep->type = M_FREE;
    195 #endif /* DIAGNOSTIC */
    196 			if (cp <= va)
    197 				break;
    198 			cp -= allocsize;
    199 			freep->next = cp;
    200 		}
    201 		freep->next = savedlist;
    202 		if (kbp->kb_last == NULL)
    203 			kbp->kb_last = (caddr_t)freep;
    204 	}
    205 	va = kbp->kb_next;
    206 	kbp->kb_next = ((struct freelist *)va)->next;
    207 #ifdef DIAGNOSTIC
    208 	freep = (struct freelist *)va;
    209 	savedtype = (unsigned)freep->type < M_LAST ?
    210 		memname[freep->type] : "???";
    211 	if (kbp->kb_next &&
    212 	    !kernacc(kbp->kb_next, sizeof(struct freelist), 0)) {
    213 		printf("%s %ld of object %p size %ld %s %s (invalid addr %p)\n",
    214 			"Data modified on freelist: word",
    215 			(long)((int32_t *)&kbp->kb_next - (int32_t *)kbp),
    216 			va, size, "previous type", savedtype, kbp->kb_next);
    217 		kbp->kb_next = NULL;
    218 	}
    219 
    220 	/* Fill the fields that we've used with WEIRD_ADDR */
    221 #if BYTE_ORDER == BIG_ENDIAN
    222 	freep->type = WEIRD_ADDR >> 16;
    223 #endif
    224 #if BYTE_ORDER == LITTLE_ENDIAN
    225 	freep->type = (short)WEIRD_ADDR;
    226 #endif
    227 	end = (int32_t *)&freep->next +
    228 	    (sizeof(freep->next) / sizeof(int32_t));
    229 	for (lp = (int32_t *)&freep->next; lp < end; lp++)
    230 		*lp = WEIRD_ADDR;
    231 
    232 	/* and check that the data hasn't been modified. */
    233 	end = (int32_t *)&va[copysize];
    234 	for (lp = (int32_t *)va; lp < end; lp++) {
    235 		if (*lp == WEIRD_ADDR)
    236 			continue;
    237 		printf("%s %ld of object %p size %ld %s %s (0x%x != 0x%x)\n",
    238 			"Data modified on freelist: word",
    239 			(long)(lp - (int32_t *)va), va, size, "previous type",
    240 			savedtype, *lp, WEIRD_ADDR);
    241 		break;
    242 	}
    243 
    244 	freep->spare0 = 0;
    245 #endif /* DIAGNOSTIC */
    246 #ifdef KMEMSTATS
    247 	kup = btokup(va);
    248 	if (kup->ku_indx != indx)
    249 		panic("malloc: wrong bucket");
    250 	if (kup->ku_freecnt == 0)
    251 		panic("malloc: lost data");
    252 	kup->ku_freecnt--;
    253 	kbp->kb_totalfree--;
    254 	ksp->ks_memuse += 1 << indx;
    255 out:
    256 	kbp->kb_calls++;
    257 	ksp->ks_inuse++;
    258 	ksp->ks_calls++;
    259 	if (ksp->ks_memuse > ksp->ks_maxused)
    260 		ksp->ks_maxused = ksp->ks_memuse;
    261 #else
    262 out:
    263 #endif
    264 	splx(s);
    265 	return ((void *) va);
    266 }
    267 
    268 /*
    269  * Free a block of memory allocated by malloc.
    270  */
    271 void
    272 free(addr, type)
    273 	void *addr;
    274 	int type;
    275 {
    276 	register struct kmembuckets *kbp;
    277 	register struct kmemusage *kup;
    278 	register struct freelist *freep;
    279 	long size;
    280 	int s;
    281 #ifdef DIAGNOSTIC
    282 	caddr_t cp;
    283 	int32_t *end, *lp;
    284 	long alloc, copysize;
    285 #endif
    286 #ifdef KMEMSTATS
    287 	register struct kmemstats *ksp = &kmemstats[type];
    288 #endif
    289 
    290 	kup = btokup(addr);
    291 	size = 1 << kup->ku_indx;
    292 	kbp = &bucket[kup->ku_indx];
    293 	s = splimp();
    294 #ifdef DIAGNOSTIC
    295 	/*
    296 	 * Check for returns of data that do not point to the
    297 	 * beginning of the allocation.
    298 	 */
    299 	if (size > NBPG * CLSIZE)
    300 		alloc = addrmask[BUCKETINDX(NBPG * CLSIZE)];
    301 	else
    302 		alloc = addrmask[kup->ku_indx];
    303 	if (((u_long)addr & alloc) != 0)
    304 		panic("free: unaligned addr %p, size %ld, type %s, mask %ld\n",
    305 			addr, size, memname[type], alloc);
    306 #endif /* DIAGNOSTIC */
    307 	if (size > MAXALLOCSAVE) {
    308 		kmem_free(kmem_map, (vm_offset_t)addr, ctob(kup->ku_pagecnt));
    309 #ifdef KMEMSTATS
    310 		size = kup->ku_pagecnt << PGSHIFT;
    311 		ksp->ks_memuse -= size;
    312 		kup->ku_indx = 0;
    313 		kup->ku_pagecnt = 0;
    314 		if (ksp->ks_memuse + size >= ksp->ks_limit &&
    315 		    ksp->ks_memuse < ksp->ks_limit)
    316 			wakeup((caddr_t)ksp);
    317 		ksp->ks_inuse--;
    318 		kbp->kb_total -= 1;
    319 #endif
    320 		splx(s);
    321 		return;
    322 	}
    323 	freep = (struct freelist *)addr;
    324 #ifdef DIAGNOSTIC
    325 	/*
    326 	 * Check for multiple frees. Use a quick check to see if
    327 	 * it looks free before laboriously searching the freelist.
    328 	 */
    329 	if (freep->spare0 == WEIRD_ADDR) {
    330 		for (cp = kbp->kb_next; cp;
    331 		    cp = ((struct freelist *)cp)->next) {
    332 			if (addr != cp)
    333 				continue;
    334 			printf("multiply freed item %p\n", addr);
    335 			panic("free: duplicated free");
    336 		}
    337 	}
    338 	/*
    339 	 * Copy in known text to detect modification after freeing
    340 	 * and to make it look free. Also, save the type being freed
    341 	 * so we can list likely culprit if modification is detected
    342 	 * when the object is reallocated.
    343 	 */
    344 	copysize = size < MAX_COPY ? size : MAX_COPY;
    345 	end = (int32_t *)&((caddr_t)addr)[copysize];
    346 	for (lp = (int32_t *)addr; lp < end; lp++)
    347 		*lp = WEIRD_ADDR;
    348 	freep->type = type;
    349 #endif /* DIAGNOSTIC */
    350 #ifdef KMEMSTATS
    351 	kup->ku_freecnt++;
    352 	if (kup->ku_freecnt >= kbp->kb_elmpercl)
    353 		if (kup->ku_freecnt > kbp->kb_elmpercl)
    354 			panic("free: multiple frees");
    355 		else if (kbp->kb_totalfree > kbp->kb_highwat)
    356 			kbp->kb_couldfree++;
    357 	kbp->kb_totalfree++;
    358 	ksp->ks_memuse -= size;
    359 	if (ksp->ks_memuse + size >= ksp->ks_limit &&
    360 	    ksp->ks_memuse < ksp->ks_limit)
    361 		wakeup((caddr_t)ksp);
    362 	ksp->ks_inuse--;
    363 #endif
    364 	if (kbp->kb_next == NULL)
    365 		kbp->kb_next = addr;
    366 	else
    367 		((struct freelist *)kbp->kb_last)->next = addr;
    368 	freep->next = NULL;
    369 	kbp->kb_last = addr;
    370 	splx(s);
    371 }
    372 
    373 /*
    374  * Change the size of a block of memory.
    375  */
    376 void *
    377 realloc(curaddr, newsize, type, flags)
    378 	void *curaddr;
    379 	unsigned long newsize;
    380 	int type, flags;
    381 {
    382 	register struct kmemusage *kup;
    383 	long cursize;
    384 	void *newaddr;
    385 #ifdef DIAGNOSTIC
    386 	long alloc;
    387 #endif
    388 
    389 	/*
    390 	 * Realloc() with a NULL pointer is the same as malloc().
    391 	 */
    392 	if (curaddr == NULL)
    393 		return (malloc(newsize, type, flags));
    394 
    395 	/*
    396 	 * Realloc() with zero size is the same as free().
    397 	 */
    398 	if (newsize == 0) {
    399 		free(curaddr, type);
    400 		return (NULL);
    401 	}
    402 
    403 	/*
    404 	 * Find out how large the old allocation was (and do some
    405 	 * sanity checking).
    406 	 */
    407 	kup = btokup(curaddr);
    408 	cursize = 1 << kup->ku_indx;
    409 
    410 #ifdef DIAGNOSTIC
    411 	/*
    412 	 * Check for returns of data that do not point to the
    413 	 * beginning of the allocation.
    414 	 */
    415 	if (cursize > NBPG * CLSIZE)
    416 		alloc = addrmask[BUCKETINDX(NBPG * CLSIZE)];
    417 	else
    418 		alloc = addrmask[kup->ku_indx];
    419 	if (((u_long)curaddr & alloc) != 0)
    420 		panic("realloc: unaligned addr %p, size %ld, type %s, mask %ld\n",
    421 			curaddr, cursize, memname[type], alloc);
    422 #endif /* DIAGNOSTIC */
    423 
    424 	if (cursize > MAXALLOCSAVE)
    425 		cursize = ctob(kup->ku_pagecnt);
    426 
    427 	/*
    428 	 * If we already actually have as much as they want, we're done.
    429 	 */
    430 	if (newsize <= cursize)
    431 		return (curaddr);
    432 
    433 	/*
    434 	 * Can't satisfy the allocation with the existing block.
    435 	 * Allocate a new one and copy the data.
    436 	 */
    437 	newaddr = malloc(newsize, type, flags);
    438 	if (newaddr == NULL) {
    439 		/*
    440 		 * Malloc() failed, because flags included M_NOWAIT.
    441 		 * Return NULL to indicate that failure.  The old
    442 		 * pointer is still valid.
    443 		 */
    444 		return NULL;
    445 	}
    446 	bcopy(curaddr, newaddr, cursize);
    447 
    448 	/*
    449 	 * We were successful: free the old allocation and return
    450 	 * the new one.
    451 	 */
    452 	free(curaddr, type);
    453 	return (newaddr);
    454 }
    455 
    456 /*
    457  * Initialize the kernel memory allocator
    458  */
    459 void
    460 kmeminit()
    461 {
    462 	register long indx;
    463 	int npg;
    464 
    465 #if	((MAXALLOCSAVE & (MAXALLOCSAVE - 1)) != 0)
    466 		ERROR!_kmeminit:_MAXALLOCSAVE_not_power_of_2
    467 #endif
    468 #if	(MAXALLOCSAVE > MINALLOCSIZE * 32768)
    469 		ERROR!_kmeminit:_MAXALLOCSAVE_too_big
    470 #endif
    471 #if	(MAXALLOCSAVE < CLBYTES)
    472 		ERROR!_kmeminit:_MAXALLOCSAVE_too_small
    473 #endif
    474 
    475 	if (sizeof(struct freelist) > (1 << MINBUCKET))
    476 		panic("minbucket too small/struct freelist too big");
    477 
    478 	npg = VM_KMEM_SIZE/ NBPG;
    479 	kmemusage = (struct kmemusage *) kmem_alloc(kernel_map,
    480 		(vm_size_t)(npg * sizeof(struct kmemusage)));
    481 	kmem_map = kmem_suballoc(kernel_map, (vm_offset_t *)&kmembase,
    482 		(vm_offset_t *)&kmemlimit, (vm_size_t)(npg * NBPG), FALSE);
    483 #ifdef KMEMSTATS
    484 	for (indx = 0; indx < MINBUCKET + 16; indx++) {
    485 		if (1 << indx >= CLBYTES)
    486 			bucket[indx].kb_elmpercl = 1;
    487 		else
    488 			bucket[indx].kb_elmpercl = CLBYTES / (1 << indx);
    489 		bucket[indx].kb_highwat = 5 * bucket[indx].kb_elmpercl;
    490 	}
    491 	for (indx = 0; indx < M_LAST; indx++)
    492 		kmemstats[indx].ks_limit = npg * NBPG * 6 / 10;
    493 #endif
    494 }
    495