kern_malloc.c revision 1.1.1.2 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.2 fvdl * @(#)kern_malloc.c 8.3 (Berkeley) 1/4/94
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 cgd #ifdef KMEMSTATS
107 1.1 cgd register struct kmemstats *ksp = &kmemstats[type];
108 1.1 cgd
109 1.1 cgd if (((unsigned long)type) > M_LAST)
110 1.1 cgd panic("malloc - bogus type");
111 1.1 cgd #endif
112 1.1 cgd indx = BUCKETINDX(size);
113 1.1 cgd kbp = &bucket[indx];
114 1.1 cgd s = splimp();
115 1.1 cgd #ifdef KMEMSTATS
116 1.1 cgd while (ksp->ks_memuse >= ksp->ks_limit) {
117 1.1 cgd if (flags & M_NOWAIT) {
118 1.1 cgd splx(s);
119 1.1 cgd return ((void *) NULL);
120 1.1 cgd }
121 1.1 cgd if (ksp->ks_limblocks < 65535)
122 1.1 cgd ksp->ks_limblocks++;
123 1.1 cgd tsleep((caddr_t)ksp, PSWP+2, memname[type], 0);
124 1.1 cgd }
125 1.1.1.2 fvdl ksp->ks_size |= 1 << indx;
126 1.1.1.2 fvdl #endif
127 1.1.1.2 fvdl #ifdef DIAGNOSTIC
128 1.1.1.2 fvdl copysize = 1 << indx < MAX_COPY ? 1 << indx : MAX_COPY;
129 1.1 cgd #endif
130 1.1 cgd if (kbp->kb_next == NULL) {
131 1.1.1.2 fvdl kbp->kb_last = NULL;
132 1.1 cgd if (size > MAXALLOCSAVE)
133 1.1 cgd allocsize = roundup(size, CLBYTES);
134 1.1 cgd else
135 1.1 cgd allocsize = 1 << indx;
136 1.1 cgd npg = clrnd(btoc(allocsize));
137 1.1 cgd va = (caddr_t) kmem_malloc(kmem_map, (vm_size_t)ctob(npg),
138 1.1 cgd !(flags & M_NOWAIT));
139 1.1 cgd if (va == NULL) {
140 1.1 cgd splx(s);
141 1.1 cgd return ((void *) NULL);
142 1.1 cgd }
143 1.1 cgd #ifdef KMEMSTATS
144 1.1 cgd kbp->kb_total += kbp->kb_elmpercl;
145 1.1 cgd #endif
146 1.1 cgd kup = btokup(va);
147 1.1 cgd kup->ku_indx = indx;
148 1.1 cgd if (allocsize > MAXALLOCSAVE) {
149 1.1 cgd if (npg > 65535)
150 1.1 cgd panic("malloc: allocation too large");
151 1.1 cgd kup->ku_pagecnt = npg;
152 1.1 cgd #ifdef KMEMSTATS
153 1.1 cgd ksp->ks_memuse += allocsize;
154 1.1 cgd #endif
155 1.1 cgd goto out;
156 1.1 cgd }
157 1.1 cgd #ifdef KMEMSTATS
158 1.1 cgd kup->ku_freecnt = kbp->kb_elmpercl;
159 1.1 cgd kbp->kb_totalfree += kbp->kb_elmpercl;
160 1.1 cgd #endif
161 1.1 cgd /*
162 1.1 cgd * Just in case we blocked while allocating memory,
163 1.1 cgd * and someone else also allocated memory for this
164 1.1 cgd * bucket, don't assume the list is still empty.
165 1.1 cgd */
166 1.1 cgd savedlist = kbp->kb_next;
167 1.1.1.2 fvdl kbp->kb_next = cp = va + (npg * NBPG) - allocsize;
168 1.1.1.2 fvdl for (;;) {
169 1.1.1.2 fvdl freep = (struct freelist *)cp;
170 1.1.1.2 fvdl #ifdef DIAGNOSTIC
171 1.1.1.2 fvdl /*
172 1.1.1.2 fvdl * Copy in known text to detect modification
173 1.1.1.2 fvdl * after freeing.
174 1.1.1.2 fvdl */
175 1.1.1.2 fvdl end = (long *)&cp[copysize];
176 1.1.1.2 fvdl for (lp = (long *)cp; lp < end; lp++)
177 1.1.1.2 fvdl *lp = WEIRD_ADDR;
178 1.1.1.2 fvdl freep->type = M_FREE;
179 1.1.1.2 fvdl #endif /* DIAGNOSTIC */
180 1.1.1.2 fvdl if (cp <= va)
181 1.1.1.2 fvdl break;
182 1.1.1.2 fvdl cp -= allocsize;
183 1.1.1.2 fvdl freep->next = cp;
184 1.1.1.2 fvdl }
185 1.1.1.2 fvdl freep->next = savedlist;
186 1.1.1.2 fvdl if (kbp->kb_last == NULL)
187 1.1.1.2 fvdl kbp->kb_last = (caddr_t)freep;
188 1.1 cgd }
189 1.1 cgd va = kbp->kb_next;
190 1.1.1.2 fvdl kbp->kb_next = ((struct freelist *)va)->next;
191 1.1.1.2 fvdl #ifdef DIAGNOSTIC
192 1.1.1.2 fvdl freep = (struct freelist *)va;
193 1.1.1.2 fvdl savedtype = (unsigned)freep->type < M_LAST ?
194 1.1.1.2 fvdl memname[freep->type] : "???";
195 1.1.1.2 fvdl if (kbp->kb_next &&
196 1.1.1.2 fvdl !kernacc(kbp->kb_next, sizeof(struct freelist), 0)) {
197 1.1.1.2 fvdl printf("%s of object 0x%x size %d %s %s (invalid addr 0x%x)\n",
198 1.1.1.2 fvdl "Data modified on freelist: word 2.5", va, size,
199 1.1.1.2 fvdl "previous type", savedtype, kbp->kb_next);
200 1.1.1.2 fvdl kbp->kb_next = NULL;
201 1.1.1.2 fvdl }
202 1.1.1.2 fvdl #if BYTE_ORDER == BIG_ENDIAN
203 1.1.1.2 fvdl freep->type = WEIRD_ADDR >> 16;
204 1.1.1.2 fvdl #endif
205 1.1.1.2 fvdl #if BYTE_ORDER == LITTLE_ENDIAN
206 1.1.1.2 fvdl freep->type = (short)WEIRD_ADDR;
207 1.1.1.2 fvdl #endif
208 1.1.1.2 fvdl if (((long)(&freep->next)) & 0x2)
209 1.1.1.2 fvdl freep->next = (caddr_t)((WEIRD_ADDR >> 16)|(WEIRD_ADDR << 16));
210 1.1.1.2 fvdl else
211 1.1.1.2 fvdl freep->next = (caddr_t)WEIRD_ADDR;
212 1.1.1.2 fvdl end = (long *)&va[copysize];
213 1.1.1.2 fvdl for (lp = (long *)va; lp < end; lp++) {
214 1.1.1.2 fvdl if (*lp == WEIRD_ADDR)
215 1.1.1.2 fvdl continue;
216 1.1.1.2 fvdl printf("%s %d of object 0x%x size %d %s %s (0x%x != 0x%x)\n",
217 1.1.1.2 fvdl "Data modified on freelist: word", lp - (long *)va,
218 1.1.1.2 fvdl va, size, "previous type", savedtype, *lp, WEIRD_ADDR);
219 1.1.1.2 fvdl break;
220 1.1.1.2 fvdl }
221 1.1.1.2 fvdl freep->spare0 = 0;
222 1.1.1.2 fvdl #endif /* DIAGNOSTIC */
223 1.1 cgd #ifdef KMEMSTATS
224 1.1 cgd kup = btokup(va);
225 1.1 cgd if (kup->ku_indx != indx)
226 1.1 cgd panic("malloc: wrong bucket");
227 1.1 cgd if (kup->ku_freecnt == 0)
228 1.1 cgd panic("malloc: lost data");
229 1.1 cgd kup->ku_freecnt--;
230 1.1 cgd kbp->kb_totalfree--;
231 1.1 cgd ksp->ks_memuse += 1 << indx;
232 1.1 cgd out:
233 1.1 cgd kbp->kb_calls++;
234 1.1 cgd ksp->ks_inuse++;
235 1.1 cgd ksp->ks_calls++;
236 1.1 cgd if (ksp->ks_memuse > ksp->ks_maxused)
237 1.1 cgd ksp->ks_maxused = ksp->ks_memuse;
238 1.1 cgd #else
239 1.1 cgd out:
240 1.1 cgd #endif
241 1.1 cgd splx(s);
242 1.1 cgd return ((void *) va);
243 1.1 cgd }
244 1.1 cgd
245 1.1 cgd /*
246 1.1 cgd * Free a block of memory allocated by malloc.
247 1.1 cgd */
248 1.1 cgd void
249 1.1 cgd free(addr, type)
250 1.1 cgd void *addr;
251 1.1 cgd int type;
252 1.1 cgd {
253 1.1 cgd register struct kmembuckets *kbp;
254 1.1 cgd register struct kmemusage *kup;
255 1.1.1.2 fvdl register struct freelist *freep;
256 1.1.1.2 fvdl long size;
257 1.1 cgd int s;
258 1.1.1.2 fvdl #ifdef DIAGNOSTIC
259 1.1.1.2 fvdl caddr_t cp;
260 1.1.1.2 fvdl long *end, *lp, alloc, copysize;
261 1.1.1.2 fvdl #endif
262 1.1 cgd #ifdef KMEMSTATS
263 1.1 cgd register struct kmemstats *ksp = &kmemstats[type];
264 1.1 cgd #endif
265 1.1 cgd
266 1.1 cgd kup = btokup(addr);
267 1.1 cgd size = 1 << kup->ku_indx;
268 1.1.1.2 fvdl kbp = &bucket[kup->ku_indx];
269 1.1.1.2 fvdl s = splimp();
270 1.1 cgd #ifdef DIAGNOSTIC
271 1.1.1.2 fvdl /*
272 1.1.1.2 fvdl * Check for returns of data that do not point to the
273 1.1.1.2 fvdl * beginning of the allocation.
274 1.1.1.2 fvdl */
275 1.1 cgd if (size > NBPG * CLSIZE)
276 1.1 cgd alloc = addrmask[BUCKETINDX(NBPG * CLSIZE)];
277 1.1 cgd else
278 1.1 cgd alloc = addrmask[kup->ku_indx];
279 1.1.1.2 fvdl if (((u_long)addr & alloc) != 0)
280 1.1.1.2 fvdl panic("free: unaligned addr 0x%x, size %d, type %s, mask %d\n",
281 1.1.1.2 fvdl addr, size, memname[type], alloc);
282 1.1 cgd #endif /* DIAGNOSTIC */
283 1.1 cgd if (size > MAXALLOCSAVE) {
284 1.1 cgd kmem_free(kmem_map, (vm_offset_t)addr, ctob(kup->ku_pagecnt));
285 1.1 cgd #ifdef KMEMSTATS
286 1.1 cgd size = kup->ku_pagecnt << PGSHIFT;
287 1.1 cgd ksp->ks_memuse -= size;
288 1.1 cgd kup->ku_indx = 0;
289 1.1 cgd kup->ku_pagecnt = 0;
290 1.1 cgd if (ksp->ks_memuse + size >= ksp->ks_limit &&
291 1.1 cgd ksp->ks_memuse < ksp->ks_limit)
292 1.1 cgd wakeup((caddr_t)ksp);
293 1.1 cgd ksp->ks_inuse--;
294 1.1 cgd kbp->kb_total -= 1;
295 1.1 cgd #endif
296 1.1 cgd splx(s);
297 1.1 cgd return;
298 1.1 cgd }
299 1.1.1.2 fvdl freep = (struct freelist *)addr;
300 1.1.1.2 fvdl #ifdef DIAGNOSTIC
301 1.1.1.2 fvdl /*
302 1.1.1.2 fvdl * Check for multiple frees. Use a quick check to see if
303 1.1.1.2 fvdl * it looks free before laboriously searching the freelist.
304 1.1.1.2 fvdl */
305 1.1.1.2 fvdl if (freep->spare0 == WEIRD_ADDR) {
306 1.1.1.2 fvdl for (cp = kbp->kb_next; cp; cp = *(caddr_t *)cp) {
307 1.1.1.2 fvdl if (addr != cp)
308 1.1.1.2 fvdl continue;
309 1.1.1.2 fvdl printf("multiply freed item 0x%x\n", addr);
310 1.1.1.2 fvdl panic("free: duplicated free");
311 1.1.1.2 fvdl }
312 1.1.1.2 fvdl }
313 1.1.1.2 fvdl /*
314 1.1.1.2 fvdl * Copy in known text to detect modification after freeing
315 1.1.1.2 fvdl * and to make it look free. Also, save the type being freed
316 1.1.1.2 fvdl * so we can list likely culprit if modification is detected
317 1.1.1.2 fvdl * when the object is reallocated.
318 1.1.1.2 fvdl */
319 1.1.1.2 fvdl copysize = size < MAX_COPY ? size : MAX_COPY;
320 1.1.1.2 fvdl end = (long *)&((caddr_t)addr)[copysize];
321 1.1.1.2 fvdl for (lp = (long *)addr; lp < end; lp++)
322 1.1.1.2 fvdl *lp = WEIRD_ADDR;
323 1.1.1.2 fvdl freep->type = type;
324 1.1.1.2 fvdl #endif /* DIAGNOSTIC */
325 1.1 cgd #ifdef KMEMSTATS
326 1.1 cgd kup->ku_freecnt++;
327 1.1 cgd if (kup->ku_freecnt >= kbp->kb_elmpercl)
328 1.1 cgd if (kup->ku_freecnt > kbp->kb_elmpercl)
329 1.1 cgd panic("free: multiple frees");
330 1.1 cgd else if (kbp->kb_totalfree > kbp->kb_highwat)
331 1.1 cgd kbp->kb_couldfree++;
332 1.1 cgd kbp->kb_totalfree++;
333 1.1 cgd ksp->ks_memuse -= size;
334 1.1 cgd if (ksp->ks_memuse + size >= ksp->ks_limit &&
335 1.1 cgd ksp->ks_memuse < ksp->ks_limit)
336 1.1 cgd wakeup((caddr_t)ksp);
337 1.1 cgd ksp->ks_inuse--;
338 1.1 cgd #endif
339 1.1.1.2 fvdl if (kbp->kb_next == NULL)
340 1.1.1.2 fvdl kbp->kb_next = addr;
341 1.1.1.2 fvdl else
342 1.1.1.2 fvdl ((struct freelist *)kbp->kb_last)->next = addr;
343 1.1.1.2 fvdl freep->next = NULL;
344 1.1.1.2 fvdl kbp->kb_last = addr;
345 1.1 cgd splx(s);
346 1.1 cgd }
347 1.1 cgd
348 1.1 cgd /*
349 1.1 cgd * Initialize the kernel memory allocator
350 1.1 cgd */
351 1.1 cgd kmeminit()
352 1.1 cgd {
353 1.1 cgd register long indx;
354 1.1 cgd int npg;
355 1.1 cgd
356 1.1 cgd #if ((MAXALLOCSAVE & (MAXALLOCSAVE - 1)) != 0)
357 1.1 cgd ERROR!_kmeminit:_MAXALLOCSAVE_not_power_of_2
358 1.1 cgd #endif
359 1.1 cgd #if (MAXALLOCSAVE > MINALLOCSIZE * 32768)
360 1.1 cgd ERROR!_kmeminit:_MAXALLOCSAVE_too_big
361 1.1 cgd #endif
362 1.1 cgd #if (MAXALLOCSAVE < CLBYTES)
363 1.1 cgd ERROR!_kmeminit:_MAXALLOCSAVE_too_small
364 1.1 cgd #endif
365 1.1 cgd npg = VM_KMEM_SIZE/ NBPG;
366 1.1 cgd kmemusage = (struct kmemusage *) kmem_alloc(kernel_map,
367 1.1 cgd (vm_size_t)(npg * sizeof(struct kmemusage)));
368 1.1 cgd kmem_map = kmem_suballoc(kernel_map, (vm_offset_t *)&kmembase,
369 1.1 cgd (vm_offset_t *)&kmemlimit, (vm_size_t)(npg * NBPG), FALSE);
370 1.1 cgd #ifdef KMEMSTATS
371 1.1 cgd for (indx = 0; indx < MINBUCKET + 16; indx++) {
372 1.1 cgd if (1 << indx >= CLBYTES)
373 1.1 cgd bucket[indx].kb_elmpercl = 1;
374 1.1 cgd else
375 1.1 cgd bucket[indx].kb_elmpercl = CLBYTES / (1 << indx);
376 1.1 cgd bucket[indx].kb_highwat = 5 * bucket[indx].kb_elmpercl;
377 1.1 cgd }
378 1.1 cgd for (indx = 0; indx < M_LAST; indx++)
379 1.1 cgd kmemstats[indx].ks_limit = npg * NBPG * 6 / 10;
380 1.1 cgd #endif
381 1.1 cgd }
382