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