1/* $NetBSD: kern_malloc.c,v 1.11 1995/05/01 22:39:11 cgd 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.3 (Berkeley) 1/4/94 32 */ 33 34#include <sys/param.h> 35#include <sys/proc.h> 36#include <sys/map.h> 37#include <sys/kernel.h> 38#include <sys/malloc.h> 39 40#include <vm/vm.h> 41#include <vm/vm_kern.h> 42 43struct kmembuckets bucket[MINBUCKET + 16]; 44struct kmemstats kmemstats[M_LAST]; 45struct kmemusage *kmemusage; 46char *kmembase, *kmemlimit; 47char *memname[] = INITKMEMNAMES; 48 49#ifdef DIAGNOSTIC 50/* 51 * This structure provides a set of masks to catch unaligned frees. 52 */ 53long addrmask[] = { 0, 54 0x00000001, 0x00000003, 0x00000007, 0x0000000f, 55 0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff, 56 0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff, 57 0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff, 58}; 59 60/* 61 * The WEIRD_ADDR is used as known text to copy into free objects so 62 * that modifications after frees can be detected. 63 */ 64#define WEIRD_ADDR 0xdeadbeef 65#define MAX_COPY 32 66 67/* 68 * Normally the freelist structure is used only to hold the list pointer 69 * for free objects. However, when running with diagnostics, the first 70 * 8 bytes of the structure is unused except for diagnostic information, 71 * and the free list pointer is at offst 8 in the structure. Since the 72 * first 8 bytes is the portion of the structure most often modified, this 73 * helps to detect memory reuse problems and avoid free list corruption. 74 */ 75struct freelist { 76 int32_t spare0; 77 int16_t type; 78 int16_t spare1; 79 caddr_t next; 80}; 81#else /* !DIAGNOSTIC */ 82struct freelist { 83 caddr_t next; 84}; 85#endif /* DIAGNOSTIC */ 86 87/* 88 * Allocate a block of memory 89 */ 90void * 91malloc(size, type, flags) 92 unsigned long size; 93 int type, flags; 94{ 95 register struct kmembuckets *kbp; 96 register struct kmemusage *kup; 97 register struct freelist *freep; 98 long indx, npg, allocsize; 99 int s; 100 caddr_t va, cp, savedlist; 101#ifdef DIAGNOSTIC 102 int32_t *end, *lp; 103 int copysize; 104 char *savedtype; 105#endif 106#ifdef KMEMSTATS 107 register struct kmemstats *ksp = &kmemstats[type]; 108 109 if (((unsigned long)type) > M_LAST) 110 panic("malloc - bogus type"); 111#endif 112 indx = BUCKETINDX(size); 113 kbp = &bucket[indx]; 114 s = splimp(); 115#ifdef KMEMSTATS 116 while (ksp->ks_memuse >= ksp->ks_limit) { 117 if (flags & M_NOWAIT) { 118 splx(s); 119 return ((void *) NULL); 120 } 121 if (ksp->ks_limblocks < 65535) 122 ksp->ks_limblocks++; 123 tsleep((caddr_t)ksp, PSWP+2, memname[type], 0); 124 } 125 ksp->ks_size |= 1 << indx; 126#endif 127#ifdef DIAGNOSTIC 128 copysize = 1 << indx < MAX_COPY ? 1 << indx : MAX_COPY; 129#endif 130 if (kbp->kb_next == NULL) { 131 kbp->kb_last = NULL; 132 if (size > MAXALLOCSAVE) 133 allocsize = roundup(size, CLBYTES); 134 else 135 allocsize = 1 << indx; 136 npg = clrnd(btoc(allocsize)); 137 va = (caddr_t) kmem_malloc(kmem_map, (vm_size_t)ctob(npg), 138 !(flags & M_NOWAIT)); 139 if (va == NULL) { 140 splx(s); 141 return ((void *) NULL); 142 } 143#ifdef KMEMSTATS 144 kbp->kb_total += kbp->kb_elmpercl; 145#endif 146 kup = btokup(va); 147 kup->ku_indx = indx; 148 if (allocsize > MAXALLOCSAVE) { 149 if (npg > 65535) 150 panic("malloc: allocation too large"); 151 kup->ku_pagecnt = npg; 152#ifdef KMEMSTATS 153 ksp->ks_memuse += allocsize; 154#endif 155 goto out; 156 } 157#ifdef KMEMSTATS 158 kup->ku_freecnt = kbp->kb_elmpercl; 159 kbp->kb_totalfree += kbp->kb_elmpercl; 160#endif 161 /* 162 * Just in case we blocked while allocating memory, 163 * and someone else also allocated memory for this 164 * bucket, don't assume the list is still empty. 165 */ 166 savedlist = kbp->kb_next; 167 kbp->kb_next = cp = va + (npg * NBPG) - allocsize; 168 for (;;) { 169 freep = (struct freelist *)cp; 170#ifdef DIAGNOSTIC 171 /* 172 * Copy in known text to detect modification 173 * after freeing. 174 */ 175 end = (int32_t *)&cp[copysize]; 176 for (lp = (int32_t *)cp; lp < end; lp++) 177 *lp = WEIRD_ADDR; 178 freep->type = M_FREE; 179#endif /* DIAGNOSTIC */ 180 if (cp <= va) 181 break; 182 cp -= allocsize; 183 freep->next = cp; 184 } 185 freep->next = savedlist; 186 if (kbp->kb_last == NULL) 187 kbp->kb_last = (caddr_t)freep; 188 } 189 va = kbp->kb_next; 190 kbp->kb_next = ((struct freelist *)va)->next; 191#ifdef DIAGNOSTIC 192 freep = (struct freelist *)va; 193 savedtype = (unsigned)freep->type < M_LAST ? 194 memname[freep->type] : "???"; 195 if (kbp->kb_next && 196 !kernacc(kbp->kb_next, sizeof(struct freelist), 0)) { 197 printf("%s %d of object %p size %d %s %s (invalid addr %p)\n", 198 "Data modified on freelist: word", 199 (int32_t *)&kbp->kb_next - (int32_t *)kbp, va, size, 200 "previous type", savedtype, kbp->kb_next); 201 kbp->kb_next = NULL; 202 } 203 204 /* Fill the fields that we've used with WEIRD_ADDR */ 205#if BYTE_ORDER == BIG_ENDIAN 206 freep->type = WEIRD_ADDR >> 16; 207#endif 208#if BYTE_ORDER == LITTLE_ENDIAN 209 freep->type = (short)WEIRD_ADDR; 210#endif 211 end = (int32_t *)&freep->next + 212 (sizeof(freep->next) / sizeof(int32_t)); 213 for (lp = (int32_t *)&freep->next; lp < end; lp++) 214 *lp = WEIRD_ADDR; 215 216 /* and check that the data hasn't been modified. */ 217 end = (int32_t *)&va[copysize]; 218 for (lp = (int32_t *)va; lp < end; lp++) { 219 if (*lp == WEIRD_ADDR) 220 continue; 221 printf("%s %d of object %p size %d %s %s (%p != %p)\n", 222 "Data modified on freelist: word", lp - (int32_t *)va, 223 va, size, "previous type", savedtype, *lp, WEIRD_ADDR); 224 break; 225 } 226 227 freep->spare0 = 0; 228#endif /* DIAGNOSTIC */ 229#ifdef KMEMSTATS 230 kup = btokup(va); 231 if (kup->ku_indx != indx) 232 panic("malloc: wrong bucket"); 233 if (kup->ku_freecnt == 0) 234 panic("malloc: lost data"); 235 kup->ku_freecnt--; 236 kbp->kb_totalfree--; 237 ksp->ks_memuse += 1 << indx; 238out: 239 kbp->kb_calls++; 240 ksp->ks_inuse++; 241 ksp->ks_calls++; 242 if (ksp->ks_memuse > ksp->ks_maxused) 243 ksp->ks_maxused = ksp->ks_memuse; 244#else 245out: 246#endif 247 splx(s); 248 return ((void *) va); 249} 250 251/* 252 * Free a block of memory allocated by malloc. 253 */ 254void 255free(addr, type) 256 void *addr; 257 int type; 258{ 259 register struct kmembuckets *kbp; 260 register struct kmemusage *kup; 261 register struct freelist *freep; 262 long size; 263 int s; 264#ifdef DIAGNOSTIC 265 caddr_t cp; 266 int32_t *end, *lp; 267 long alloc, copysize; 268#endif 269#ifdef KMEMSTATS 270 register struct kmemstats *ksp = &kmemstats[type]; 271#endif 272 273 kup = btokup(addr); 274 size = 1 << kup->ku_indx; 275 kbp = &bucket[kup->ku_indx]; 276 s = splimp(); 277#ifdef DIAGNOSTIC 278 /* 279 * Check for returns of data that do not point to the 280 * beginning of the allocation. 281 */ 282 if (size > NBPG * CLSIZE) 283 alloc = addrmask[BUCKETINDX(NBPG * CLSIZE)]; 284 else 285 alloc = addrmask[kup->ku_indx]; 286 if (((u_long)addr & alloc) != 0) 287 panic("free: unaligned addr 0x%x, size %d, type %s, mask %d\n", 288 addr, size, memname[type], alloc); 289#endif /* DIAGNOSTIC */ 290 if (size > MAXALLOCSAVE) { 291 kmem_free(kmem_map, (vm_offset_t)addr, ctob(kup->ku_pagecnt)); 292#ifdef KMEMSTATS 293 size = kup->ku_pagecnt << PGSHIFT; 294 ksp->ks_memuse -= size; 295 kup->ku_indx = 0; 296 kup->ku_pagecnt = 0; 297 if (ksp->ks_memuse + size >= ksp->ks_limit && 298 ksp->ks_memuse < ksp->ks_limit) 299 wakeup((caddr_t)ksp); 300 ksp->ks_inuse--; 301 kbp->kb_total -= 1; 302#endif 303 splx(s); 304 return; 305 } 306 freep = (struct freelist *)addr; 307#ifdef DIAGNOSTIC 308 /* 309 * Check for multiple frees. Use a quick check to see if 310 * it looks free before laboriously searching the freelist. 311 */ 312 if (freep->spare0 == WEIRD_ADDR) { 313 for (cp = kbp->kb_next; cp; cp = *(caddr_t *)cp) { 314 if (addr != cp) 315 continue; 316 printf("multiply freed item %p\n", addr); 317 panic("free: duplicated free"); 318 } 319 } 320 /* 321 * Copy in known text to detect modification after freeing 322 * and to make it look free. Also, save the type being freed 323 * so we can list likely culprit if modification is detected 324 * when the object is reallocated. 325 */ 326 copysize = size < MAX_COPY ? size : MAX_COPY; 327 end = (int32_t *)&((caddr_t)addr)[copysize]; 328 for (lp = (int32_t *)addr; lp < end; lp++) 329 *lp = WEIRD_ADDR; 330 freep->type = type; 331#endif /* DIAGNOSTIC */ 332#ifdef KMEMSTATS 333 kup->ku_freecnt++; 334 if (kup->ku_freecnt >= kbp->kb_elmpercl) 335 if (kup->ku_freecnt > kbp->kb_elmpercl) 336 panic("free: multiple frees"); 337 else if (kbp->kb_totalfree > kbp->kb_highwat) 338 kbp->kb_couldfree++; 339 kbp->kb_totalfree++; 340 ksp->ks_memuse -= size; 341 if (ksp->ks_memuse + size >= ksp->ks_limit && 342 ksp->ks_memuse < ksp->ks_limit) 343 wakeup((caddr_t)ksp); 344 ksp->ks_inuse--; 345#endif 346 if (kbp->kb_next == NULL) 347 kbp->kb_next = addr; 348 else 349 ((struct freelist *)kbp->kb_last)->next = addr; 350 freep->next = NULL; 351 kbp->kb_last = addr; 352 splx(s); 353} 354 355/* 356 * Initialize the kernel memory allocator 357 */ 358kmeminit() 359{ 360 register long indx; 361 int npg; 362 363#if ((MAXALLOCSAVE & (MAXALLOCSAVE - 1)) != 0) 364 ERROR!_kmeminit:_MAXALLOCSAVE_not_power_of_2 365#endif 366#if (MAXALLOCSAVE > MINALLOCSIZE * 32768) 367 ERROR!_kmeminit:_MAXALLOCSAVE_too_big 368#endif 369#if (MAXALLOCSAVE < CLBYTES) 370 ERROR!_kmeminit:_MAXALLOCSAVE_too_small 371#endif 372 373 if (sizeof(struct freelist) > (1 << MINBUCKET)) 374 panic("minbucket too small/struct freelist too big"); 375 376 npg = VM_KMEM_SIZE/ NBPG; 377 kmemusage = (struct kmemusage *) kmem_alloc(kernel_map, 378 (vm_size_t)(npg * sizeof(struct kmemusage))); 379 kmem_map = kmem_suballoc(kernel_map, (vm_offset_t *)&kmembase, 380 (vm_offset_t *)&kmemlimit, (vm_size_t)(npg * NBPG), FALSE); 381#ifdef KMEMSTATS 382 for (indx = 0; indx < MINBUCKET + 16; indx++) { 383 if (1 << indx >= CLBYTES) 384 bucket[indx].kb_elmpercl = 1; 385 else 386 bucket[indx].kb_elmpercl = CLBYTES / (1 << indx); 387 bucket[indx].kb_highwat = 5 * bucket[indx].kb_elmpercl; 388 } 389 for (indx = 0; indx < M_LAST; indx++) 390 kmemstats[indx].ks_limit = npg * NBPG * 6 / 10; 391#endif 392} 393