kern_malloc.c revision 1.53 1 1.53 mrg /* $NetBSD: kern_malloc.c,v 1.53 2000/06/26 14:21:14 mrg Exp $ */
2 1.9 cgd
3 1.1 cgd /*
4 1.37 christos * Copyright (c) 1996 Christopher G. Demetriou. All rights reserved.
5 1.8 cgd * Copyright (c) 1987, 1991, 1993
6 1.8 cgd * The Regents of the University of California. All rights reserved.
7 1.1 cgd *
8 1.1 cgd * Redistribution and use in source and binary forms, with or without
9 1.1 cgd * modification, are permitted provided that the following conditions
10 1.1 cgd * are met:
11 1.1 cgd * 1. Redistributions of source code must retain the above copyright
12 1.1 cgd * notice, this list of conditions and the following disclaimer.
13 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer in the
15 1.1 cgd * documentation and/or other materials provided with the distribution.
16 1.1 cgd * 3. All advertising materials mentioning features or use of this software
17 1.1 cgd * must display the following acknowledgement:
18 1.1 cgd * This product includes software developed by the University of
19 1.1 cgd * California, Berkeley and its contributors.
20 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
21 1.1 cgd * may be used to endorse or promote products derived from this software
22 1.1 cgd * without specific prior written permission.
23 1.1 cgd *
24 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 cgd * SUCH DAMAGE.
35 1.1 cgd *
36 1.32 fvdl * @(#)kern_malloc.c 8.4 (Berkeley) 5/20/95
37 1.1 cgd */
38 1.31 mrg
39 1.33 thorpej #include "opt_lockdebug.h"
40 1.1 cgd
41 1.7 mycroft #include <sys/param.h>
42 1.7 mycroft #include <sys/proc.h>
43 1.8 cgd #include <sys/map.h>
44 1.7 mycroft #include <sys/kernel.h>
45 1.7 mycroft #include <sys/malloc.h>
46 1.12 christos #include <sys/systm.h>
47 1.7 mycroft
48 1.7 mycroft #include <vm/vm.h>
49 1.24 thorpej
50 1.28 mrg #include <uvm/uvm_extern.h>
51 1.28 mrg
52 1.44 thorpej static struct vm_map_intrsafe kmem_map_store;
53 1.28 mrg vm_map_t kmem_map = NULL;
54 1.28 mrg
55 1.49 thorpej #include "opt_kmempages.h"
56 1.49 thorpej
57 1.49 thorpej #ifdef NKMEMCLUSTERS
58 1.52 sommerfe #error NKMEMCLUSTERS is obsolete; remove it from your kernel config file and use NKMEMPAGES instead or let the kernel auto-size
59 1.49 thorpej #endif
60 1.49 thorpej
61 1.49 thorpej /*
62 1.49 thorpej * Default number of pages in kmem_map. We attempt to calculate this
63 1.49 thorpej * at run-time, but allow it to be either patched or set in the kernel
64 1.49 thorpej * config file.
65 1.49 thorpej */
66 1.49 thorpej #ifndef NKMEMPAGES
67 1.49 thorpej #define NKMEMPAGES 0
68 1.49 thorpej #endif
69 1.49 thorpej int nkmempages = NKMEMPAGES;
70 1.49 thorpej
71 1.49 thorpej /*
72 1.49 thorpej * Defaults for lower- and upper-bounds for the kmem_map page count.
73 1.49 thorpej * Can be overridden by kernel config options.
74 1.49 thorpej */
75 1.49 thorpej #ifndef NKMEMPAGES_MIN
76 1.49 thorpej #define NKMEMPAGES_MIN NKMEMPAGES_MIN_DEFAULT
77 1.49 thorpej #endif
78 1.49 thorpej
79 1.49 thorpej #ifndef NKMEMPAGES_MAX
80 1.49 thorpej #define NKMEMPAGES_MAX NKMEMPAGES_MAX_DEFAULT
81 1.49 thorpej #endif
82 1.49 thorpej
83 1.24 thorpej #include "opt_kmemstats.h"
84 1.27 thorpej #include "opt_malloclog.h"
85 1.12 christos
86 1.1 cgd struct kmembuckets bucket[MINBUCKET + 16];
87 1.8 cgd struct kmemstats kmemstats[M_LAST];
88 1.1 cgd struct kmemusage *kmemusage;
89 1.1 cgd char *kmembase, *kmemlimit;
90 1.25 mycroft const char *memname[] = INITKMEMNAMES;
91 1.1 cgd
92 1.27 thorpej #ifdef MALLOCLOG
93 1.27 thorpej #ifndef MALLOCLOGSIZE
94 1.27 thorpej #define MALLOCLOGSIZE 100000
95 1.27 thorpej #endif
96 1.27 thorpej
97 1.27 thorpej struct malloclog {
98 1.27 thorpej void *addr;
99 1.27 thorpej long size;
100 1.27 thorpej int type;
101 1.27 thorpej int action;
102 1.27 thorpej const char *file;
103 1.27 thorpej long line;
104 1.27 thorpej } malloclog[MALLOCLOGSIZE];
105 1.27 thorpej
106 1.27 thorpej long malloclogptr;
107 1.27 thorpej
108 1.27 thorpej static void domlog __P((void *a, long size, int type, int action,
109 1.27 thorpej const char *file, long line));
110 1.27 thorpej static void hitmlog __P((void *a));
111 1.27 thorpej
112 1.27 thorpej static void
113 1.27 thorpej domlog(a, size, type, action, file, line)
114 1.27 thorpej void *a;
115 1.27 thorpej long size;
116 1.27 thorpej int type;
117 1.27 thorpej int action;
118 1.27 thorpej const char *file;
119 1.27 thorpej long line;
120 1.27 thorpej {
121 1.27 thorpej
122 1.27 thorpej malloclog[malloclogptr].addr = a;
123 1.27 thorpej malloclog[malloclogptr].size = size;
124 1.27 thorpej malloclog[malloclogptr].type = type;
125 1.27 thorpej malloclog[malloclogptr].action = action;
126 1.27 thorpej malloclog[malloclogptr].file = file;
127 1.27 thorpej malloclog[malloclogptr].line = line;
128 1.27 thorpej malloclogptr++;
129 1.27 thorpej if (malloclogptr >= MALLOCLOGSIZE)
130 1.27 thorpej malloclogptr = 0;
131 1.27 thorpej }
132 1.27 thorpej
133 1.27 thorpej static void
134 1.27 thorpej hitmlog(a)
135 1.27 thorpej void *a;
136 1.27 thorpej {
137 1.27 thorpej struct malloclog *lp;
138 1.27 thorpej long l;
139 1.27 thorpej
140 1.27 thorpej #define PRT \
141 1.27 thorpej if (malloclog[l].addr == a && malloclog[l].action) { \
142 1.27 thorpej lp = &malloclog[l]; \
143 1.27 thorpej printf("malloc log entry %ld:\n", l); \
144 1.27 thorpej printf("\taddr = %p\n", lp->addr); \
145 1.27 thorpej printf("\tsize = %ld\n", lp->size); \
146 1.27 thorpej printf("\ttype = %s\n", memname[lp->type]); \
147 1.27 thorpej printf("\taction = %s\n", lp->action == 1 ? "alloc" : "free"); \
148 1.27 thorpej printf("\tfile = %s\n", lp->file); \
149 1.27 thorpej printf("\tline = %ld\n", lp->line); \
150 1.27 thorpej }
151 1.27 thorpej
152 1.27 thorpej for (l = malloclogptr; l < MALLOCLOGSIZE; l++)
153 1.27 thorpej PRT
154 1.27 thorpej
155 1.27 thorpej for (l = 0; l < malloclogptr; l++)
156 1.27 thorpej PRT
157 1.27 thorpej }
158 1.27 thorpej #endif /* MALLOCLOG */
159 1.27 thorpej
160 1.8 cgd #ifdef DIAGNOSTIC
161 1.8 cgd /*
162 1.8 cgd * This structure provides a set of masks to catch unaligned frees.
163 1.8 cgd */
164 1.8 cgd long addrmask[] = { 0,
165 1.8 cgd 0x00000001, 0x00000003, 0x00000007, 0x0000000f,
166 1.8 cgd 0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff,
167 1.8 cgd 0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff,
168 1.8 cgd 0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff,
169 1.8 cgd };
170 1.8 cgd
171 1.8 cgd /*
172 1.8 cgd * The WEIRD_ADDR is used as known text to copy into free objects so
173 1.8 cgd * that modifications after frees can be detected.
174 1.8 cgd */
175 1.12 christos #define WEIRD_ADDR ((unsigned) 0xdeadbeef)
176 1.8 cgd #define MAX_COPY 32
177 1.8 cgd
178 1.8 cgd /*
179 1.11 cgd * Normally the freelist structure is used only to hold the list pointer
180 1.11 cgd * for free objects. However, when running with diagnostics, the first
181 1.11 cgd * 8 bytes of the structure is unused except for diagnostic information,
182 1.11 cgd * and the free list pointer is at offst 8 in the structure. Since the
183 1.11 cgd * first 8 bytes is the portion of the structure most often modified, this
184 1.11 cgd * helps to detect memory reuse problems and avoid free list corruption.
185 1.8 cgd */
186 1.8 cgd struct freelist {
187 1.11 cgd int32_t spare0;
188 1.11 cgd int16_t type;
189 1.11 cgd int16_t spare1;
190 1.8 cgd caddr_t next;
191 1.8 cgd };
192 1.8 cgd #else /* !DIAGNOSTIC */
193 1.8 cgd struct freelist {
194 1.8 cgd caddr_t next;
195 1.8 cgd };
196 1.8 cgd #endif /* DIAGNOSTIC */
197 1.8 cgd
198 1.1 cgd /*
199 1.1 cgd * Allocate a block of memory
200 1.1 cgd */
201 1.27 thorpej #ifdef MALLOCLOG
202 1.27 thorpej void *
203 1.27 thorpej _malloc(size, type, flags, file, line)
204 1.27 thorpej unsigned long size;
205 1.27 thorpej int type, flags;
206 1.27 thorpej const char *file;
207 1.27 thorpej long line;
208 1.27 thorpej #else
209 1.1 cgd void *
210 1.1 cgd malloc(size, type, flags)
211 1.1 cgd unsigned long size;
212 1.1 cgd int type, flags;
213 1.27 thorpej #endif /* MALLOCLOG */
214 1.1 cgd {
215 1.50 augustss struct kmembuckets *kbp;
216 1.50 augustss struct kmemusage *kup;
217 1.50 augustss struct freelist *freep;
218 1.5 andrew long indx, npg, allocsize;
219 1.1 cgd int s;
220 1.1 cgd caddr_t va, cp, savedlist;
221 1.8 cgd #ifdef DIAGNOSTIC
222 1.11 cgd int32_t *end, *lp;
223 1.8 cgd int copysize;
224 1.26 mycroft const char *savedtype;
225 1.8 cgd #endif
226 1.1 cgd #ifdef KMEMSTATS
227 1.50 augustss struct kmemstats *ksp = &kmemstats[type];
228 1.1 cgd
229 1.51 thorpej if (__predict_false(((unsigned long)type) > M_LAST))
230 1.1 cgd panic("malloc - bogus type");
231 1.1 cgd #endif
232 1.1 cgd indx = BUCKETINDX(size);
233 1.1 cgd kbp = &bucket[indx];
234 1.46 fvdl s = splmem();
235 1.1 cgd #ifdef KMEMSTATS
236 1.1 cgd while (ksp->ks_memuse >= ksp->ks_limit) {
237 1.1 cgd if (flags & M_NOWAIT) {
238 1.1 cgd splx(s);
239 1.1 cgd return ((void *) NULL);
240 1.1 cgd }
241 1.1 cgd if (ksp->ks_limblocks < 65535)
242 1.1 cgd ksp->ks_limblocks++;
243 1.1 cgd tsleep((caddr_t)ksp, PSWP+2, memname[type], 0);
244 1.1 cgd }
245 1.8 cgd ksp->ks_size |= 1 << indx;
246 1.8 cgd #endif
247 1.8 cgd #ifdef DIAGNOSTIC
248 1.8 cgd copysize = 1 << indx < MAX_COPY ? 1 << indx : MAX_COPY;
249 1.1 cgd #endif
250 1.1 cgd if (kbp->kb_next == NULL) {
251 1.8 cgd kbp->kb_last = NULL;
252 1.1 cgd if (size > MAXALLOCSAVE)
253 1.49 thorpej allocsize = roundup(size, PAGE_SIZE);
254 1.1 cgd else
255 1.1 cgd allocsize = 1 << indx;
256 1.47 ragge npg = btoc(allocsize);
257 1.28 mrg va = (caddr_t) uvm_km_kmemalloc(kmem_map, uvmexp.kmem_object,
258 1.35 eeh (vsize_t)ctob(npg),
259 1.28 mrg (flags & M_NOWAIT) ? UVM_KMF_NOWAIT : 0);
260 1.51 thorpej if (__predict_false(va == NULL)) {
261 1.17 cgd /*
262 1.17 cgd * Kmem_malloc() can return NULL, even if it can
263 1.17 cgd * wait, if there is no map space avaiable, because
264 1.17 cgd * it can't fix that problem. Neither can we,
265 1.17 cgd * right now. (We should release pages which
266 1.17 cgd * are completely free and which are in buckets
267 1.17 cgd * with too many free elements.)
268 1.17 cgd */
269 1.17 cgd if ((flags & M_NOWAIT) == 0)
270 1.17 cgd panic("malloc: out of space in kmem_map");
271 1.6 cgd splx(s);
272 1.6 cgd return ((void *) NULL);
273 1.1 cgd }
274 1.1 cgd #ifdef KMEMSTATS
275 1.1 cgd kbp->kb_total += kbp->kb_elmpercl;
276 1.1 cgd #endif
277 1.1 cgd kup = btokup(va);
278 1.1 cgd kup->ku_indx = indx;
279 1.1 cgd if (allocsize > MAXALLOCSAVE) {
280 1.1 cgd if (npg > 65535)
281 1.1 cgd panic("malloc: allocation too large");
282 1.1 cgd kup->ku_pagecnt = npg;
283 1.1 cgd #ifdef KMEMSTATS
284 1.1 cgd ksp->ks_memuse += allocsize;
285 1.1 cgd #endif
286 1.1 cgd goto out;
287 1.1 cgd }
288 1.1 cgd #ifdef KMEMSTATS
289 1.1 cgd kup->ku_freecnt = kbp->kb_elmpercl;
290 1.1 cgd kbp->kb_totalfree += kbp->kb_elmpercl;
291 1.1 cgd #endif
292 1.1 cgd /*
293 1.1 cgd * Just in case we blocked while allocating memory,
294 1.1 cgd * and someone else also allocated memory for this
295 1.1 cgd * bucket, don't assume the list is still empty.
296 1.1 cgd */
297 1.1 cgd savedlist = kbp->kb_next;
298 1.49 thorpej kbp->kb_next = cp = va + (npg << PAGE_SHIFT) - allocsize;
299 1.8 cgd for (;;) {
300 1.8 cgd freep = (struct freelist *)cp;
301 1.8 cgd #ifdef DIAGNOSTIC
302 1.8 cgd /*
303 1.8 cgd * Copy in known text to detect modification
304 1.8 cgd * after freeing.
305 1.8 cgd */
306 1.11 cgd end = (int32_t *)&cp[copysize];
307 1.11 cgd for (lp = (int32_t *)cp; lp < end; lp++)
308 1.8 cgd *lp = WEIRD_ADDR;
309 1.8 cgd freep->type = M_FREE;
310 1.8 cgd #endif /* DIAGNOSTIC */
311 1.8 cgd if (cp <= va)
312 1.8 cgd break;
313 1.8 cgd cp -= allocsize;
314 1.8 cgd freep->next = cp;
315 1.8 cgd }
316 1.8 cgd freep->next = savedlist;
317 1.8 cgd if (kbp->kb_last == NULL)
318 1.8 cgd kbp->kb_last = (caddr_t)freep;
319 1.1 cgd }
320 1.1 cgd va = kbp->kb_next;
321 1.8 cgd kbp->kb_next = ((struct freelist *)va)->next;
322 1.8 cgd #ifdef DIAGNOSTIC
323 1.8 cgd freep = (struct freelist *)va;
324 1.8 cgd savedtype = (unsigned)freep->type < M_LAST ?
325 1.8 cgd memname[freep->type] : "???";
326 1.29 chs if (kbp->kb_next) {
327 1.29 chs int rv;
328 1.35 eeh vaddr_t addr = (vaddr_t)kbp->kb_next;
329 1.29 chs
330 1.43 thorpej vm_map_lock(kmem_map);
331 1.29 chs rv = uvm_map_checkprot(kmem_map, addr,
332 1.29 chs addr + sizeof(struct freelist),
333 1.29 chs VM_PROT_WRITE);
334 1.43 thorpej vm_map_unlock(kmem_map);
335 1.29 chs
336 1.51 thorpej if (__predict_false(rv == 0)) {
337 1.41 mrg printf(
338 1.21 christos "%s %ld of object %p size %ld %s %s (invalid addr %p)\n",
339 1.41 mrg "Data modified on freelist: word",
340 1.41 mrg (long)((int32_t *)&kbp->kb_next - (int32_t *)kbp),
341 1.41 mrg va, size, "previous type", savedtype, kbp->kb_next);
342 1.27 thorpej #ifdef MALLOCLOG
343 1.41 mrg hitmlog(va);
344 1.27 thorpej #endif
345 1.41 mrg kbp->kb_next = NULL;
346 1.29 chs }
347 1.8 cgd }
348 1.11 cgd
349 1.11 cgd /* Fill the fields that we've used with WEIRD_ADDR */
350 1.8 cgd #if BYTE_ORDER == BIG_ENDIAN
351 1.8 cgd freep->type = WEIRD_ADDR >> 16;
352 1.8 cgd #endif
353 1.8 cgd #if BYTE_ORDER == LITTLE_ENDIAN
354 1.8 cgd freep->type = (short)WEIRD_ADDR;
355 1.8 cgd #endif
356 1.11 cgd end = (int32_t *)&freep->next +
357 1.11 cgd (sizeof(freep->next) / sizeof(int32_t));
358 1.11 cgd for (lp = (int32_t *)&freep->next; lp < end; lp++)
359 1.11 cgd *lp = WEIRD_ADDR;
360 1.11 cgd
361 1.11 cgd /* and check that the data hasn't been modified. */
362 1.11 cgd end = (int32_t *)&va[copysize];
363 1.11 cgd for (lp = (int32_t *)va; lp < end; lp++) {
364 1.51 thorpej if (__predict_true(*lp == WEIRD_ADDR))
365 1.8 cgd continue;
366 1.22 christos printf("%s %ld of object %p size %ld %s %s (0x%x != 0x%x)\n",
367 1.21 christos "Data modified on freelist: word",
368 1.21 christos (long)(lp - (int32_t *)va), va, size, "previous type",
369 1.21 christos savedtype, *lp, WEIRD_ADDR);
370 1.27 thorpej #ifdef MALLOCLOG
371 1.27 thorpej hitmlog(va);
372 1.27 thorpej #endif
373 1.8 cgd break;
374 1.8 cgd }
375 1.11 cgd
376 1.8 cgd freep->spare0 = 0;
377 1.8 cgd #endif /* DIAGNOSTIC */
378 1.1 cgd #ifdef KMEMSTATS
379 1.1 cgd kup = btokup(va);
380 1.1 cgd if (kup->ku_indx != indx)
381 1.1 cgd panic("malloc: wrong bucket");
382 1.1 cgd if (kup->ku_freecnt == 0)
383 1.1 cgd panic("malloc: lost data");
384 1.1 cgd kup->ku_freecnt--;
385 1.1 cgd kbp->kb_totalfree--;
386 1.1 cgd ksp->ks_memuse += 1 << indx;
387 1.1 cgd out:
388 1.1 cgd kbp->kb_calls++;
389 1.1 cgd ksp->ks_inuse++;
390 1.1 cgd ksp->ks_calls++;
391 1.1 cgd if (ksp->ks_memuse > ksp->ks_maxused)
392 1.1 cgd ksp->ks_maxused = ksp->ks_memuse;
393 1.1 cgd #else
394 1.1 cgd out:
395 1.1 cgd #endif
396 1.27 thorpej #ifdef MALLOCLOG
397 1.27 thorpej domlog(va, size, type, 1, file, line);
398 1.27 thorpej #endif
399 1.1 cgd splx(s);
400 1.1 cgd return ((void *) va);
401 1.1 cgd }
402 1.1 cgd
403 1.1 cgd /*
404 1.1 cgd * Free a block of memory allocated by malloc.
405 1.1 cgd */
406 1.27 thorpej #ifdef MALLOCLOG
407 1.27 thorpej void
408 1.27 thorpej _free(addr, type, file, line)
409 1.27 thorpej void *addr;
410 1.27 thorpej int type;
411 1.27 thorpej const char *file;
412 1.27 thorpej long line;
413 1.27 thorpej #else
414 1.1 cgd void
415 1.1 cgd free(addr, type)
416 1.1 cgd void *addr;
417 1.1 cgd int type;
418 1.27 thorpej #endif /* MALLOCLOG */
419 1.1 cgd {
420 1.50 augustss struct kmembuckets *kbp;
421 1.50 augustss struct kmemusage *kup;
422 1.50 augustss struct freelist *freep;
423 1.8 cgd long size;
424 1.8 cgd int s;
425 1.5 andrew #ifdef DIAGNOSTIC
426 1.8 cgd caddr_t cp;
427 1.11 cgd int32_t *end, *lp;
428 1.11 cgd long alloc, copysize;
429 1.5 andrew #endif
430 1.1 cgd #ifdef KMEMSTATS
431 1.50 augustss struct kmemstats *ksp = &kmemstats[type];
432 1.48 thorpej #endif
433 1.48 thorpej
434 1.48 thorpej #ifdef DIAGNOSTIC
435 1.48 thorpej /*
436 1.48 thorpej * Ensure that we're free'ing something that we could
437 1.48 thorpej * have allocated in the first place. That is, check
438 1.48 thorpej * to see that the address is within kmem_map.
439 1.48 thorpej */
440 1.51 thorpej if (__predict_false((vaddr_t)addr < kmem_map->header.start ||
441 1.51 thorpej (vaddr_t)addr >= kmem_map->header.end))
442 1.48 thorpej panic("free: addr %p not within kmem_map", addr);
443 1.1 cgd #endif
444 1.1 cgd
445 1.1 cgd kup = btokup(addr);
446 1.1 cgd size = 1 << kup->ku_indx;
447 1.8 cgd kbp = &bucket[kup->ku_indx];
448 1.46 fvdl s = splmem();
449 1.27 thorpej #ifdef MALLOCLOG
450 1.27 thorpej domlog(addr, 0, type, 2, file, line);
451 1.27 thorpej #endif
452 1.1 cgd #ifdef DIAGNOSTIC
453 1.8 cgd /*
454 1.8 cgd * Check for returns of data that do not point to the
455 1.8 cgd * beginning of the allocation.
456 1.8 cgd */
457 1.49 thorpej if (size > PAGE_SIZE)
458 1.49 thorpej alloc = addrmask[BUCKETINDX(PAGE_SIZE)];
459 1.1 cgd else
460 1.1 cgd alloc = addrmask[kup->ku_indx];
461 1.8 cgd if (((u_long)addr & alloc) != 0)
462 1.15 christos panic("free: unaligned addr %p, size %ld, type %s, mask %ld\n",
463 1.8 cgd addr, size, memname[type], alloc);
464 1.1 cgd #endif /* DIAGNOSTIC */
465 1.1 cgd if (size > MAXALLOCSAVE) {
466 1.35 eeh uvm_km_free(kmem_map, (vaddr_t)addr, ctob(kup->ku_pagecnt));
467 1.1 cgd #ifdef KMEMSTATS
468 1.1 cgd size = kup->ku_pagecnt << PGSHIFT;
469 1.1 cgd ksp->ks_memuse -= size;
470 1.1 cgd kup->ku_indx = 0;
471 1.1 cgd kup->ku_pagecnt = 0;
472 1.1 cgd if (ksp->ks_memuse + size >= ksp->ks_limit &&
473 1.1 cgd ksp->ks_memuse < ksp->ks_limit)
474 1.1 cgd wakeup((caddr_t)ksp);
475 1.1 cgd ksp->ks_inuse--;
476 1.1 cgd kbp->kb_total -= 1;
477 1.1 cgd #endif
478 1.1 cgd splx(s);
479 1.1 cgd return;
480 1.1 cgd }
481 1.8 cgd freep = (struct freelist *)addr;
482 1.8 cgd #ifdef DIAGNOSTIC
483 1.8 cgd /*
484 1.8 cgd * Check for multiple frees. Use a quick check to see if
485 1.8 cgd * it looks free before laboriously searching the freelist.
486 1.8 cgd */
487 1.51 thorpej if (__predict_false(freep->spare0 == WEIRD_ADDR)) {
488 1.16 cgd for (cp = kbp->kb_next; cp;
489 1.16 cgd cp = ((struct freelist *)cp)->next) {
490 1.8 cgd if (addr != cp)
491 1.8 cgd continue;
492 1.22 christos printf("multiply freed item %p\n", addr);
493 1.27 thorpej #ifdef MALLOCLOG
494 1.27 thorpej hitmlog(addr);
495 1.27 thorpej #endif
496 1.8 cgd panic("free: duplicated free");
497 1.8 cgd }
498 1.8 cgd }
499 1.38 chs #ifdef LOCKDEBUG
500 1.38 chs /*
501 1.38 chs * Check if we're freeing a locked simple lock.
502 1.38 chs */
503 1.40 chs simple_lock_freecheck(addr, (char *)addr + size);
504 1.38 chs #endif
505 1.8 cgd /*
506 1.8 cgd * Copy in known text to detect modification after freeing
507 1.8 cgd * and to make it look free. Also, save the type being freed
508 1.8 cgd * so we can list likely culprit if modification is detected
509 1.8 cgd * when the object is reallocated.
510 1.8 cgd */
511 1.8 cgd copysize = size < MAX_COPY ? size : MAX_COPY;
512 1.11 cgd end = (int32_t *)&((caddr_t)addr)[copysize];
513 1.11 cgd for (lp = (int32_t *)addr; lp < end; lp++)
514 1.8 cgd *lp = WEIRD_ADDR;
515 1.8 cgd freep->type = type;
516 1.8 cgd #endif /* DIAGNOSTIC */
517 1.1 cgd #ifdef KMEMSTATS
518 1.1 cgd kup->ku_freecnt++;
519 1.36 thorpej if (kup->ku_freecnt >= kbp->kb_elmpercl) {
520 1.1 cgd if (kup->ku_freecnt > kbp->kb_elmpercl)
521 1.1 cgd panic("free: multiple frees");
522 1.1 cgd else if (kbp->kb_totalfree > kbp->kb_highwat)
523 1.1 cgd kbp->kb_couldfree++;
524 1.36 thorpej }
525 1.1 cgd kbp->kb_totalfree++;
526 1.1 cgd ksp->ks_memuse -= size;
527 1.1 cgd if (ksp->ks_memuse + size >= ksp->ks_limit &&
528 1.1 cgd ksp->ks_memuse < ksp->ks_limit)
529 1.1 cgd wakeup((caddr_t)ksp);
530 1.1 cgd ksp->ks_inuse--;
531 1.1 cgd #endif
532 1.8 cgd if (kbp->kb_next == NULL)
533 1.8 cgd kbp->kb_next = addr;
534 1.8 cgd else
535 1.8 cgd ((struct freelist *)kbp->kb_last)->next = addr;
536 1.8 cgd freep->next = NULL;
537 1.8 cgd kbp->kb_last = addr;
538 1.1 cgd splx(s);
539 1.20 cgd }
540 1.20 cgd
541 1.20 cgd /*
542 1.20 cgd * Change the size of a block of memory.
543 1.20 cgd */
544 1.20 cgd void *
545 1.20 cgd realloc(curaddr, newsize, type, flags)
546 1.20 cgd void *curaddr;
547 1.20 cgd unsigned long newsize;
548 1.20 cgd int type, flags;
549 1.20 cgd {
550 1.50 augustss struct kmemusage *kup;
551 1.20 cgd long cursize;
552 1.20 cgd void *newaddr;
553 1.20 cgd #ifdef DIAGNOSTIC
554 1.20 cgd long alloc;
555 1.20 cgd #endif
556 1.20 cgd
557 1.20 cgd /*
558 1.20 cgd * Realloc() with a NULL pointer is the same as malloc().
559 1.20 cgd */
560 1.20 cgd if (curaddr == NULL)
561 1.20 cgd return (malloc(newsize, type, flags));
562 1.20 cgd
563 1.20 cgd /*
564 1.20 cgd * Realloc() with zero size is the same as free().
565 1.20 cgd */
566 1.20 cgd if (newsize == 0) {
567 1.20 cgd free(curaddr, type);
568 1.20 cgd return (NULL);
569 1.20 cgd }
570 1.20 cgd
571 1.20 cgd /*
572 1.20 cgd * Find out how large the old allocation was (and do some
573 1.20 cgd * sanity checking).
574 1.20 cgd */
575 1.20 cgd kup = btokup(curaddr);
576 1.20 cgd cursize = 1 << kup->ku_indx;
577 1.20 cgd
578 1.20 cgd #ifdef DIAGNOSTIC
579 1.20 cgd /*
580 1.20 cgd * Check for returns of data that do not point to the
581 1.20 cgd * beginning of the allocation.
582 1.20 cgd */
583 1.49 thorpej if (cursize > PAGE_SIZE)
584 1.49 thorpej alloc = addrmask[BUCKETINDX(PAGE_SIZE)];
585 1.20 cgd else
586 1.20 cgd alloc = addrmask[kup->ku_indx];
587 1.20 cgd if (((u_long)curaddr & alloc) != 0)
588 1.20 cgd panic("realloc: unaligned addr %p, size %ld, type %s, mask %ld\n",
589 1.20 cgd curaddr, cursize, memname[type], alloc);
590 1.20 cgd #endif /* DIAGNOSTIC */
591 1.20 cgd
592 1.20 cgd if (cursize > MAXALLOCSAVE)
593 1.20 cgd cursize = ctob(kup->ku_pagecnt);
594 1.20 cgd
595 1.20 cgd /*
596 1.20 cgd * If we already actually have as much as they want, we're done.
597 1.20 cgd */
598 1.20 cgd if (newsize <= cursize)
599 1.20 cgd return (curaddr);
600 1.20 cgd
601 1.20 cgd /*
602 1.20 cgd * Can't satisfy the allocation with the existing block.
603 1.20 cgd * Allocate a new one and copy the data.
604 1.20 cgd */
605 1.20 cgd newaddr = malloc(newsize, type, flags);
606 1.51 thorpej if (__predict_false(newaddr == NULL)) {
607 1.20 cgd /*
608 1.20 cgd * Malloc() failed, because flags included M_NOWAIT.
609 1.20 cgd * Return NULL to indicate that failure. The old
610 1.20 cgd * pointer is still valid.
611 1.20 cgd */
612 1.20 cgd return NULL;
613 1.20 cgd }
614 1.34 perry memcpy(newaddr, curaddr, cursize);
615 1.20 cgd
616 1.20 cgd /*
617 1.20 cgd * We were successful: free the old allocation and return
618 1.20 cgd * the new one.
619 1.20 cgd */
620 1.20 cgd free(curaddr, type);
621 1.20 cgd return (newaddr);
622 1.1 cgd }
623 1.1 cgd
624 1.1 cgd /*
625 1.49 thorpej * Compute the number of pages that kmem_map will map, that is,
626 1.49 thorpej * the size of the kernel malloc arena.
627 1.49 thorpej */
628 1.49 thorpej void
629 1.49 thorpej kmeminit_nkmempages()
630 1.49 thorpej {
631 1.49 thorpej int npages;
632 1.49 thorpej
633 1.49 thorpej if (nkmempages != 0) {
634 1.49 thorpej /*
635 1.49 thorpej * It's already been set (by us being here before, or
636 1.49 thorpej * by patching or kernel config options), bail out now.
637 1.49 thorpej */
638 1.49 thorpej return;
639 1.49 thorpej }
640 1.49 thorpej
641 1.49 thorpej /*
642 1.49 thorpej * We use the following (simple) formula:
643 1.49 thorpej *
644 1.49 thorpej * - Starting point is physical memory / 4.
645 1.49 thorpej *
646 1.49 thorpej * - Clamp it down to NKMEMPAGES_MAX.
647 1.49 thorpej *
648 1.49 thorpej * - Round it up to NKMEMPAGES_MIN.
649 1.49 thorpej */
650 1.49 thorpej npages = physmem / 4;
651 1.49 thorpej
652 1.49 thorpej if (npages > NKMEMPAGES_MAX)
653 1.49 thorpej npages = NKMEMPAGES_MAX;
654 1.49 thorpej
655 1.49 thorpej if (npages < NKMEMPAGES_MIN)
656 1.49 thorpej npages = NKMEMPAGES_MIN;
657 1.49 thorpej
658 1.49 thorpej nkmempages = npages;
659 1.49 thorpej }
660 1.49 thorpej
661 1.49 thorpej /*
662 1.1 cgd * Initialize the kernel memory allocator
663 1.1 cgd */
664 1.12 christos void
665 1.1 cgd kmeminit()
666 1.1 cgd {
667 1.23 tls #ifdef KMEMSTATS
668 1.50 augustss long indx;
669 1.23 tls #endif
670 1.1 cgd
671 1.1 cgd #if ((MAXALLOCSAVE & (MAXALLOCSAVE - 1)) != 0)
672 1.1 cgd ERROR!_kmeminit:_MAXALLOCSAVE_not_power_of_2
673 1.1 cgd #endif
674 1.1 cgd #if (MAXALLOCSAVE > MINALLOCSIZE * 32768)
675 1.1 cgd ERROR!_kmeminit:_MAXALLOCSAVE_too_big
676 1.1 cgd #endif
677 1.47 ragge #if (MAXALLOCSAVE < NBPG)
678 1.1 cgd ERROR!_kmeminit:_MAXALLOCSAVE_too_small
679 1.1 cgd #endif
680 1.11 cgd
681 1.11 cgd if (sizeof(struct freelist) > (1 << MINBUCKET))
682 1.11 cgd panic("minbucket too small/struct freelist too big");
683 1.11 cgd
684 1.49 thorpej /*
685 1.49 thorpej * Compute the number of kmem_map pages, if we have not
686 1.49 thorpej * done so already.
687 1.49 thorpej */
688 1.49 thorpej kmeminit_nkmempages();
689 1.49 thorpej
690 1.28 mrg kmemusage = (struct kmemusage *) uvm_km_zalloc(kernel_map,
691 1.49 thorpej (vsize_t)(nkmempages * sizeof(struct kmemusage)));
692 1.35 eeh kmem_map = uvm_km_suballoc(kernel_map, (vaddr_t *)&kmembase,
693 1.49 thorpej (vaddr_t *)&kmemlimit, (vsize_t)(nkmempages << PAGE_SHIFT),
694 1.44 thorpej VM_MAP_INTRSAFE, FALSE, &kmem_map_store.vmi_map);
695 1.1 cgd #ifdef KMEMSTATS
696 1.1 cgd for (indx = 0; indx < MINBUCKET + 16; indx++) {
697 1.49 thorpej if (1 << indx >= PAGE_SIZE)
698 1.1 cgd bucket[indx].kb_elmpercl = 1;
699 1.1 cgd else
700 1.49 thorpej bucket[indx].kb_elmpercl = PAGE_SIZE / (1 << indx);
701 1.1 cgd bucket[indx].kb_highwat = 5 * bucket[indx].kb_elmpercl;
702 1.1 cgd }
703 1.8 cgd for (indx = 0; indx < M_LAST; indx++)
704 1.49 thorpej kmemstats[indx].ks_limit = (nkmempages << PAGE_SHIFT) * 6 / 10;
705 1.1 cgd #endif
706 1.1 cgd }
707 1.39 thorpej
708 1.39 thorpej #ifdef DDB
709 1.39 thorpej #include <ddb/db_output.h>
710 1.39 thorpej
711 1.39 thorpej /*
712 1.39 thorpej * Dump kmem statistics from ddb.
713 1.39 thorpej *
714 1.39 thorpej * usage: call dump_kmemstats
715 1.39 thorpej */
716 1.39 thorpej void dump_kmemstats __P((void));
717 1.39 thorpej
718 1.39 thorpej void
719 1.39 thorpej dump_kmemstats()
720 1.39 thorpej {
721 1.39 thorpej #ifdef KMEMSTATS
722 1.39 thorpej const char *name;
723 1.39 thorpej int i;
724 1.39 thorpej
725 1.39 thorpej for (i = 0; i < M_LAST; i++) {
726 1.39 thorpej name = memname[i] ? memname[i] : "";
727 1.39 thorpej
728 1.39 thorpej db_printf("%2d %s%.*s %ld\n", i, name,
729 1.39 thorpej (int)(20 - strlen(name)), " ",
730 1.39 thorpej kmemstats[i].ks_memuse);
731 1.39 thorpej }
732 1.39 thorpej #else
733 1.39 thorpej db_printf("Kmem stats are not being collected.\n");
734 1.39 thorpej #endif /* KMEMSTATS */
735 1.39 thorpej }
736 1.39 thorpej #endif /* DDB */
737