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