Home | History | Annotate | Line # | Download | only in kern
kern_malloc.c revision 1.8
      1 /*
      2  * Copyright (c) 1987, 1991, 1993
      3  *	The Regents of the University of California.  All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  * 1. Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  * 2. Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in the
     12  *    documentation and/or other materials provided with the distribution.
     13  * 3. All advertising materials mentioning features or use of this software
     14  *    must display the following acknowledgement:
     15  *	This product includes software developed by the University of
     16  *	California, Berkeley and its contributors.
     17  * 4. Neither the name of the University nor the names of its contributors
     18  *    may be used to endorse or promote products derived from this software
     19  *    without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  *
     33  *	from: @(#)kern_malloc.c	8.3 (Berkeley) 1/4/94
     34  *	$Id: kern_malloc.c,v 1.8 1994/05/13 08:32:17 cgd Exp $
     35  */
     36 
     37 #include <sys/param.h>
     38 #include <sys/proc.h>
     39 #include <sys/map.h>
     40 #include <sys/kernel.h>
     41 #include <sys/malloc.h>
     42 
     43 #include <vm/vm.h>
     44 #include <vm/vm_kern.h>
     45 
     46 struct kmembuckets bucket[MINBUCKET + 16];
     47 struct kmemstats kmemstats[M_LAST];
     48 struct kmemusage *kmemusage;
     49 char *kmembase, *kmemlimit;
     50 char *memname[] = INITKMEMNAMES;
     51 
     52 #ifdef DIAGNOSTIC
     53 /*
     54  * This structure provides a set of masks to catch unaligned frees.
     55  */
     56 long addrmask[] = { 0,
     57 	0x00000001, 0x00000003, 0x00000007, 0x0000000f,
     58 	0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff,
     59 	0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff,
     60 	0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff,
     61 };
     62 
     63 /*
     64  * The WEIRD_ADDR is used as known text to copy into free objects so
     65  * that modifications after frees can be detected.
     66  */
     67 #define WEIRD_ADDR	0xdeadbeef
     68 #define MAX_COPY	32
     69 
     70 /*
     71  * Normally the first word of the structure is used to hold the list
     72  * pointer for free objects. However, when running with diagnostics,
     73  * we use the third and fourth fields, so as to catch modifications
     74  * in the most commonly trashed first two words.
     75  */
     76 struct freelist {
     77 	long	spare0;
     78 	short	type;
     79 	long	spare1;
     80 	caddr_t	next;
     81 };
     82 #else /* !DIAGNOSTIC */
     83 struct freelist {
     84 	caddr_t	next;
     85 };
     86 #endif /* DIAGNOSTIC */
     87 
     88 /*
     89  * Allocate a block of memory
     90  */
     91 void *
     92 malloc(size, type, flags)
     93 	unsigned long size;
     94 	int type, flags;
     95 {
     96 	register struct kmembuckets *kbp;
     97 	register struct kmemusage *kup;
     98 	register struct freelist *freep;
     99 	long indx, npg, allocsize;
    100 	int s;
    101 	caddr_t va, cp, savedlist;
    102 #ifdef DIAGNOSTIC
    103 	long *end, *lp;
    104 	int copysize;
    105 	char *savedtype;
    106 #endif
    107 #ifdef KMEMSTATS
    108 	register struct kmemstats *ksp = &kmemstats[type];
    109 
    110 	if (((unsigned long)type) > M_LAST)
    111 		panic("malloc - bogus type");
    112 #endif
    113 	indx = BUCKETINDX(size);
    114 	kbp = &bucket[indx];
    115 	s = splimp();
    116 #ifdef KMEMSTATS
    117 	while (ksp->ks_memuse >= ksp->ks_limit) {
    118 		if (flags & M_NOWAIT) {
    119 			splx(s);
    120 			return ((void *) NULL);
    121 		}
    122 		if (ksp->ks_limblocks < 65535)
    123 			ksp->ks_limblocks++;
    124 		tsleep((caddr_t)ksp, PSWP+2, memname[type], 0);
    125 	}
    126 	ksp->ks_size |= 1 << indx;
    127 #endif
    128 #ifdef DIAGNOSTIC
    129 	copysize = 1 << indx < MAX_COPY ? 1 << indx : MAX_COPY;
    130 #endif
    131 	if (kbp->kb_next == NULL) {
    132 		kbp->kb_last = NULL;
    133 		if (size > MAXALLOCSAVE)
    134 			allocsize = roundup(size, CLBYTES);
    135 		else
    136 			allocsize = 1 << indx;
    137 		npg = clrnd(btoc(allocsize));
    138 		va = (caddr_t) kmem_malloc(kmem_map, (vm_size_t)ctob(npg),
    139 					   !(flags & M_NOWAIT));
    140 		if (va == NULL) {
    141 			splx(s);
    142 			return ((void *) NULL);
    143 		}
    144 #ifdef KMEMSTATS
    145 		kbp->kb_total += kbp->kb_elmpercl;
    146 #endif
    147 		kup = btokup(va);
    148 		kup->ku_indx = indx;
    149 		if (allocsize > MAXALLOCSAVE) {
    150 			if (npg > 65535)
    151 				panic("malloc: allocation too large");
    152 			kup->ku_pagecnt = npg;
    153 #ifdef KMEMSTATS
    154 			ksp->ks_memuse += allocsize;
    155 #endif
    156 			goto out;
    157 		}
    158 #ifdef KMEMSTATS
    159 		kup->ku_freecnt = kbp->kb_elmpercl;
    160 		kbp->kb_totalfree += kbp->kb_elmpercl;
    161 #endif
    162 		/*
    163 		 * Just in case we blocked while allocating memory,
    164 		 * and someone else also allocated memory for this
    165 		 * bucket, don't assume the list is still empty.
    166 		 */
    167 		savedlist = kbp->kb_next;
    168 		kbp->kb_next = cp = va + (npg * NBPG) - allocsize;
    169 		for (;;) {
    170 			freep = (struct freelist *)cp;
    171 #ifdef DIAGNOSTIC
    172 			/*
    173 			 * Copy in known text to detect modification
    174 			 * after freeing.
    175 			 */
    176 			end = (long *)&cp[copysize];
    177 			for (lp = (long *)cp; lp < end; lp++)
    178 				*lp = WEIRD_ADDR;
    179 			freep->type = M_FREE;
    180 #endif /* DIAGNOSTIC */
    181 			if (cp <= va)
    182 				break;
    183 			cp -= allocsize;
    184 			freep->next = cp;
    185 		}
    186 		freep->next = savedlist;
    187 		if (kbp->kb_last == NULL)
    188 			kbp->kb_last = (caddr_t)freep;
    189 	}
    190 	va = kbp->kb_next;
    191 	kbp->kb_next = ((struct freelist *)va)->next;
    192 #ifdef DIAGNOSTIC
    193 	freep = (struct freelist *)va;
    194 	savedtype = (unsigned)freep->type < M_LAST ?
    195 		memname[freep->type] : "???";
    196 	if (kbp->kb_next &&
    197 	    !kernacc(kbp->kb_next, sizeof(struct freelist), 0)) {
    198 		printf("%s of object 0x%x size %d %s %s (invalid addr 0x%x)\n",
    199 			"Data modified on freelist: word 2.5", va, size,
    200 			"previous type", savedtype, kbp->kb_next);
    201 		kbp->kb_next = NULL;
    202 	}
    203 #if BYTE_ORDER == BIG_ENDIAN
    204 	freep->type = WEIRD_ADDR >> 16;
    205 #endif
    206 #if BYTE_ORDER == LITTLE_ENDIAN
    207 	freep->type = (short)WEIRD_ADDR;
    208 #endif
    209 	if (((long)(&freep->next)) & 0x2)
    210 		freep->next = (caddr_t)((WEIRD_ADDR >> 16)|(WEIRD_ADDR << 16));
    211 	else
    212 		freep->next = (caddr_t)WEIRD_ADDR;
    213 	end = (long *)&va[copysize];
    214 	for (lp = (long *)va; lp < end; lp++) {
    215 		if (*lp == WEIRD_ADDR)
    216 			continue;
    217 		printf("%s %d of object 0x%x size %d %s %s (0x%x != 0x%x)\n",
    218 			"Data modified on freelist: word", lp - (long *)va,
    219 			va, size, "previous type", savedtype, *lp, WEIRD_ADDR);
    220 		break;
    221 	}
    222 	freep->spare0 = 0;
    223 #endif /* DIAGNOSTIC */
    224 #ifdef KMEMSTATS
    225 	kup = btokup(va);
    226 	if (kup->ku_indx != indx)
    227 		panic("malloc: wrong bucket");
    228 	if (kup->ku_freecnt == 0)
    229 		panic("malloc: lost data");
    230 	kup->ku_freecnt--;
    231 	kbp->kb_totalfree--;
    232 	ksp->ks_memuse += 1 << indx;
    233 out:
    234 	kbp->kb_calls++;
    235 	ksp->ks_inuse++;
    236 	ksp->ks_calls++;
    237 	if (ksp->ks_memuse > ksp->ks_maxused)
    238 		ksp->ks_maxused = ksp->ks_memuse;
    239 #else
    240 out:
    241 #endif
    242 	splx(s);
    243 	return ((void *) va);
    244 }
    245 
    246 /*
    247  * Free a block of memory allocated by malloc.
    248  */
    249 void
    250 free(addr, type)
    251 	void *addr;
    252 	int type;
    253 {
    254 	register struct kmembuckets *kbp;
    255 	register struct kmemusage *kup;
    256 	register struct freelist *freep;
    257 	long size;
    258 	int s;
    259 #ifdef DIAGNOSTIC
    260 	caddr_t cp;
    261 	long *end, *lp, alloc, copysize;
    262 #endif
    263 #ifdef KMEMSTATS
    264 	register struct kmemstats *ksp = &kmemstats[type];
    265 #endif
    266 
    267 	kup = btokup(addr);
    268 	size = 1 << kup->ku_indx;
    269 	kbp = &bucket[kup->ku_indx];
    270 	s = splimp();
    271 #ifdef DIAGNOSTIC
    272 	/*
    273 	 * Check for returns of data that do not point to the
    274 	 * beginning of the allocation.
    275 	 */
    276 	if (size > NBPG * CLSIZE)
    277 		alloc = addrmask[BUCKETINDX(NBPG * CLSIZE)];
    278 	else
    279 		alloc = addrmask[kup->ku_indx];
    280 	if (((u_long)addr & alloc) != 0)
    281 		panic("free: unaligned addr 0x%x, size %d, type %s, mask %d\n",
    282 			addr, size, memname[type], alloc);
    283 #endif /* DIAGNOSTIC */
    284 	if (size > MAXALLOCSAVE) {
    285 		kmem_free(kmem_map, (vm_offset_t)addr, ctob(kup->ku_pagecnt));
    286 #ifdef KMEMSTATS
    287 		size = kup->ku_pagecnt << PGSHIFT;
    288 		ksp->ks_memuse -= size;
    289 		kup->ku_indx = 0;
    290 		kup->ku_pagecnt = 0;
    291 		if (ksp->ks_memuse + size >= ksp->ks_limit &&
    292 		    ksp->ks_memuse < ksp->ks_limit)
    293 			wakeup((caddr_t)ksp);
    294 		ksp->ks_inuse--;
    295 		kbp->kb_total -= 1;
    296 #endif
    297 		splx(s);
    298 		return;
    299 	}
    300 	freep = (struct freelist *)addr;
    301 #ifdef DIAGNOSTIC
    302 	/*
    303 	 * Check for multiple frees. Use a quick check to see if
    304 	 * it looks free before laboriously searching the freelist.
    305 	 */
    306 	if (freep->spare0 == WEIRD_ADDR) {
    307 		for (cp = kbp->kb_next; cp; cp = *(caddr_t *)cp) {
    308 			if (addr != cp)
    309 				continue;
    310 			printf("multiply freed item 0x%x\n", addr);
    311 			panic("free: duplicated free");
    312 		}
    313 	}
    314 	/*
    315 	 * Copy in known text to detect modification after freeing
    316 	 * and to make it look free. Also, save the type being freed
    317 	 * so we can list likely culprit if modification is detected
    318 	 * when the object is reallocated.
    319 	 */
    320 	copysize = size < MAX_COPY ? size : MAX_COPY;
    321 	end = (long *)&((caddr_t)addr)[copysize];
    322 	for (lp = (long *)addr; lp < end; lp++)
    323 		*lp = WEIRD_ADDR;
    324 	freep->type = type;
    325 #endif /* DIAGNOSTIC */
    326 #ifdef KMEMSTATS
    327 	kup->ku_freecnt++;
    328 	if (kup->ku_freecnt >= kbp->kb_elmpercl)
    329 		if (kup->ku_freecnt > kbp->kb_elmpercl)
    330 			panic("free: multiple frees");
    331 		else if (kbp->kb_totalfree > kbp->kb_highwat)
    332 			kbp->kb_couldfree++;
    333 	kbp->kb_totalfree++;
    334 	ksp->ks_memuse -= size;
    335 	if (ksp->ks_memuse + size >= ksp->ks_limit &&
    336 	    ksp->ks_memuse < ksp->ks_limit)
    337 		wakeup((caddr_t)ksp);
    338 	ksp->ks_inuse--;
    339 #endif
    340 	if (kbp->kb_next == NULL)
    341 		kbp->kb_next = addr;
    342 	else
    343 		((struct freelist *)kbp->kb_last)->next = addr;
    344 	freep->next = NULL;
    345 	kbp->kb_last = addr;
    346 	splx(s);
    347 }
    348 
    349 /*
    350  * Initialize the kernel memory allocator
    351  */
    352 kmeminit()
    353 {
    354 	register long indx;
    355 	int npg;
    356 
    357 #if	((MAXALLOCSAVE & (MAXALLOCSAVE - 1)) != 0)
    358 		ERROR!_kmeminit:_MAXALLOCSAVE_not_power_of_2
    359 #endif
    360 #if	(MAXALLOCSAVE > MINALLOCSIZE * 32768)
    361 		ERROR!_kmeminit:_MAXALLOCSAVE_too_big
    362 #endif
    363 #if	(MAXALLOCSAVE < CLBYTES)
    364 		ERROR!_kmeminit:_MAXALLOCSAVE_too_small
    365 #endif
    366 	npg = VM_KMEM_SIZE/ NBPG;
    367 	kmemusage = (struct kmemusage *) kmem_alloc(kernel_map,
    368 		(vm_size_t)(npg * sizeof(struct kmemusage)));
    369 	kmem_map = kmem_suballoc(kernel_map, (vm_offset_t *)&kmembase,
    370 		(vm_offset_t *)&kmemlimit, (vm_size_t)(npg * NBPG), FALSE);
    371 #ifdef KMEMSTATS
    372 	for (indx = 0; indx < MINBUCKET + 16; indx++) {
    373 		if (1 << indx >= CLBYTES)
    374 			bucket[indx].kb_elmpercl = 1;
    375 		else
    376 			bucket[indx].kb_elmpercl = CLBYTES / (1 << indx);
    377 		bucket[indx].kb_highwat = 5 * bucket[indx].kb_elmpercl;
    378 	}
    379 	for (indx = 0; indx < M_LAST; indx++)
    380 		kmemstats[indx].ks_limit = npg * NBPG * 6 / 10;
    381 #endif
    382 }
    383