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