xref: /netbsd-src/sys/kern/kern_malloc.c (revision 481fca6e59249d8ffcf24fef7cfbe7b131bfb080)
1 /*	$NetBSD: kern_malloc.c,v 1.54 2000/06/27 17:41:22 mrg 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 "opt_lockdebug.h"
40 
41 #include <sys/param.h>
42 #include <sys/proc.h>
43 #include <sys/map.h>
44 #include <sys/kernel.h>
45 #include <sys/malloc.h>
46 #include <sys/systm.h>
47 
48 #include <uvm/uvm_extern.h>
49 
50 static struct vm_map_intrsafe kmem_map_store;
51 vm_map_t kmem_map = NULL;
52 
53 #include "opt_kmempages.h"
54 
55 #ifdef NKMEMCLUSTERS
56 #error NKMEMCLUSTERS is obsolete; remove it from your kernel config file and use NKMEMPAGES instead or let the kernel auto-size
57 #endif
58 
59 /*
60  * Default number of pages in kmem_map.  We attempt to calculate this
61  * at run-time, but allow it to be either patched or set in the kernel
62  * config file.
63  */
64 #ifndef NKMEMPAGES
65 #define	NKMEMPAGES	0
66 #endif
67 int	nkmempages = NKMEMPAGES;
68 
69 /*
70  * Defaults for lower- and upper-bounds for the kmem_map page count.
71  * Can be overridden by kernel config options.
72  */
73 #ifndef	NKMEMPAGES_MIN
74 #define	NKMEMPAGES_MIN	NKMEMPAGES_MIN_DEFAULT
75 #endif
76 
77 #ifndef NKMEMPAGES_MAX
78 #define	NKMEMPAGES_MAX	NKMEMPAGES_MAX_DEFAULT
79 #endif
80 
81 #include "opt_kmemstats.h"
82 #include "opt_malloclog.h"
83 
84 struct kmembuckets bucket[MINBUCKET + 16];
85 struct kmemstats kmemstats[M_LAST];
86 struct kmemusage *kmemusage;
87 char *kmembase, *kmemlimit;
88 const char *memname[] = INITKMEMNAMES;
89 
90 #ifdef MALLOCLOG
91 #ifndef MALLOCLOGSIZE
92 #define	MALLOCLOGSIZE	100000
93 #endif
94 
95 struct malloclog {
96 	void *addr;
97 	long size;
98 	int type;
99 	int action;
100 	const char *file;
101 	long line;
102 } malloclog[MALLOCLOGSIZE];
103 
104 long	malloclogptr;
105 
106 static void domlog __P((void *a, long size, int type, int action,
107 	const char *file, long line));
108 static void hitmlog __P((void *a));
109 
110 static void
111 domlog(a, size, type, action, file, line)
112 	void *a;
113 	long size;
114 	int type;
115 	int action;
116 	const char *file;
117 	long line;
118 {
119 
120 	malloclog[malloclogptr].addr = a;
121 	malloclog[malloclogptr].size = size;
122 	malloclog[malloclogptr].type = type;
123 	malloclog[malloclogptr].action = action;
124 	malloclog[malloclogptr].file = file;
125 	malloclog[malloclogptr].line = line;
126 	malloclogptr++;
127 	if (malloclogptr >= MALLOCLOGSIZE)
128 		malloclogptr = 0;
129 }
130 
131 static void
132 hitmlog(a)
133 	void *a;
134 {
135 	struct malloclog *lp;
136 	long l;
137 
138 #define	PRT \
139 	if (malloclog[l].addr == a && malloclog[l].action) { \
140 		lp = &malloclog[l]; \
141 		printf("malloc log entry %ld:\n", l); \
142 		printf("\taddr = %p\n", lp->addr); \
143 		printf("\tsize = %ld\n", lp->size); \
144 		printf("\ttype = %s\n", memname[lp->type]); \
145 		printf("\taction = %s\n", lp->action == 1 ? "alloc" : "free"); \
146 		printf("\tfile = %s\n", lp->file); \
147 		printf("\tline = %ld\n", lp->line); \
148 	}
149 
150 	for (l = malloclogptr; l < MALLOCLOGSIZE; l++)
151 		PRT
152 
153 	for (l = 0; l < malloclogptr; l++)
154 		PRT
155 }
156 #endif /* MALLOCLOG */
157 
158 #ifdef DIAGNOSTIC
159 /*
160  * This structure provides a set of masks to catch unaligned frees.
161  */
162 long addrmask[] = { 0,
163 	0x00000001, 0x00000003, 0x00000007, 0x0000000f,
164 	0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff,
165 	0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff,
166 	0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff,
167 };
168 
169 /*
170  * The WEIRD_ADDR is used as known text to copy into free objects so
171  * that modifications after frees can be detected.
172  */
173 #define WEIRD_ADDR	((unsigned) 0xdeadbeef)
174 #define MAX_COPY	32
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(size, type, flags, file, line)
202 	unsigned long size;
203 	int type, flags;
204 	const char *file;
205 	long line;
206 #else
207 void *
208 malloc(size, type, flags)
209 	unsigned long size;
210 	int type, flags;
211 #endif /* MALLOCLOG */
212 {
213 	struct kmembuckets *kbp;
214 	struct kmemusage *kup;
215 	struct freelist *freep;
216 	long indx, npg, allocsize;
217 	int s;
218 	caddr_t va, cp, savedlist;
219 #ifdef DIAGNOSTIC
220 	int32_t *end, *lp;
221 	int copysize;
222 	const char *savedtype;
223 #endif
224 #ifdef KMEMSTATS
225 	struct kmemstats *ksp = &kmemstats[type];
226 
227 	if (__predict_false(((unsigned long)type) > M_LAST))
228 		panic("malloc - bogus type");
229 #endif
230 	indx = BUCKETINDX(size);
231 	kbp = &bucket[indx];
232 	s = splmem();
233 #ifdef KMEMSTATS
234 	while (ksp->ks_memuse >= ksp->ks_limit) {
235 		if (flags & M_NOWAIT) {
236 			splx(s);
237 			return ((void *) NULL);
238 		}
239 		if (ksp->ks_limblocks < 65535)
240 			ksp->ks_limblocks++;
241 		tsleep((caddr_t)ksp, PSWP+2, memname[type], 0);
242 	}
243 	ksp->ks_size |= 1 << indx;
244 #endif
245 #ifdef DIAGNOSTIC
246 	copysize = 1 << indx < MAX_COPY ? 1 << indx : MAX_COPY;
247 #endif
248 	if (kbp->kb_next == NULL) {
249 		kbp->kb_last = NULL;
250 		if (size > MAXALLOCSAVE)
251 			allocsize = roundup(size, PAGE_SIZE);
252 		else
253 			allocsize = 1 << indx;
254 		npg = btoc(allocsize);
255 		va = (caddr_t) uvm_km_kmemalloc(kmem_map, uvmexp.kmem_object,
256 				(vsize_t)ctob(npg),
257 				(flags & M_NOWAIT) ? UVM_KMF_NOWAIT : 0);
258 		if (__predict_false(va == NULL)) {
259 			/*
260 			 * Kmem_malloc() can return NULL, even if it can
261 			 * wait, if there is no map space avaiable, because
262 			 * it can't fix that problem.  Neither can we,
263 			 * right now.  (We should release pages which
264 			 * are completely free and which are in buckets
265 			 * with too many free elements.)
266 			 */
267 			if ((flags & M_NOWAIT) == 0)
268 				panic("malloc: out of space in kmem_map");
269 			splx(s);
270 			return ((void *) NULL);
271 		}
272 #ifdef KMEMSTATS
273 		kbp->kb_total += kbp->kb_elmpercl;
274 #endif
275 		kup = btokup(va);
276 		kup->ku_indx = indx;
277 		if (allocsize > MAXALLOCSAVE) {
278 			if (npg > 65535)
279 				panic("malloc: allocation too large");
280 			kup->ku_pagecnt = npg;
281 #ifdef KMEMSTATS
282 			ksp->ks_memuse += allocsize;
283 #endif
284 			goto out;
285 		}
286 #ifdef KMEMSTATS
287 		kup->ku_freecnt = kbp->kb_elmpercl;
288 		kbp->kb_totalfree += kbp->kb_elmpercl;
289 #endif
290 		/*
291 		 * Just in case we blocked while allocating memory,
292 		 * and someone else also allocated memory for this
293 		 * bucket, don't assume the list is still empty.
294 		 */
295 		savedlist = kbp->kb_next;
296 		kbp->kb_next = cp = va + (npg << PAGE_SHIFT) - allocsize;
297 		for (;;) {
298 			freep = (struct freelist *)cp;
299 #ifdef DIAGNOSTIC
300 			/*
301 			 * Copy in known text to detect modification
302 			 * after freeing.
303 			 */
304 			end = (int32_t *)&cp[copysize];
305 			for (lp = (int32_t *)cp; lp < end; lp++)
306 				*lp = WEIRD_ADDR;
307 			freep->type = M_FREE;
308 #endif /* DIAGNOSTIC */
309 			if (cp <= va)
310 				break;
311 			cp -= allocsize;
312 			freep->next = cp;
313 		}
314 		freep->next = savedlist;
315 		if (kbp->kb_last == NULL)
316 			kbp->kb_last = (caddr_t)freep;
317 	}
318 	va = kbp->kb_next;
319 	kbp->kb_next = ((struct freelist *)va)->next;
320 #ifdef DIAGNOSTIC
321 	freep = (struct freelist *)va;
322 	savedtype = (unsigned)freep->type < M_LAST ?
323 		memname[freep->type] : "???";
324 	if (kbp->kb_next) {
325 		int rv;
326 		vaddr_t addr = (vaddr_t)kbp->kb_next;
327 
328 		vm_map_lock(kmem_map);
329 		rv = uvm_map_checkprot(kmem_map, addr,
330 				       addr + sizeof(struct freelist),
331 				       VM_PROT_WRITE);
332 		vm_map_unlock(kmem_map);
333 
334 		if (__predict_false(rv == 0)) {
335 			printf(
336 		    "%s %ld of object %p size %ld %s %s (invalid addr %p)\n",
337 			    "Data modified on freelist: word",
338 			    (long)((int32_t *)&kbp->kb_next - (int32_t *)kbp),
339 			    va, size, "previous type", savedtype, kbp->kb_next);
340 #ifdef MALLOCLOG
341 			hitmlog(va);
342 #endif
343 			kbp->kb_next = NULL;
344 		}
345 	}
346 
347 	/* Fill the fields that we've used with WEIRD_ADDR */
348 #if BYTE_ORDER == BIG_ENDIAN
349 	freep->type = WEIRD_ADDR >> 16;
350 #endif
351 #if BYTE_ORDER == LITTLE_ENDIAN
352 	freep->type = (short)WEIRD_ADDR;
353 #endif
354 	end = (int32_t *)&freep->next +
355 	    (sizeof(freep->next) / sizeof(int32_t));
356 	for (lp = (int32_t *)&freep->next; lp < end; lp++)
357 		*lp = WEIRD_ADDR;
358 
359 	/* and check that the data hasn't been modified. */
360 	end = (int32_t *)&va[copysize];
361 	for (lp = (int32_t *)va; lp < end; lp++) {
362 		if (__predict_true(*lp == WEIRD_ADDR))
363 			continue;
364 		printf("%s %ld of object %p size %ld %s %s (0x%x != 0x%x)\n",
365 		    "Data modified on freelist: word",
366 		    (long)(lp - (int32_t *)va), va, size, "previous type",
367 		    savedtype, *lp, WEIRD_ADDR);
368 #ifdef MALLOCLOG
369 		hitmlog(va);
370 #endif
371 		break;
372 	}
373 
374 	freep->spare0 = 0;
375 #endif /* DIAGNOSTIC */
376 #ifdef KMEMSTATS
377 	kup = btokup(va);
378 	if (kup->ku_indx != indx)
379 		panic("malloc: wrong bucket");
380 	if (kup->ku_freecnt == 0)
381 		panic("malloc: lost data");
382 	kup->ku_freecnt--;
383 	kbp->kb_totalfree--;
384 	ksp->ks_memuse += 1 << indx;
385 out:
386 	kbp->kb_calls++;
387 	ksp->ks_inuse++;
388 	ksp->ks_calls++;
389 	if (ksp->ks_memuse > ksp->ks_maxused)
390 		ksp->ks_maxused = ksp->ks_memuse;
391 #else
392 out:
393 #endif
394 #ifdef MALLOCLOG
395 	domlog(va, size, type, 1, file, line);
396 #endif
397 	splx(s);
398 	return ((void *) va);
399 }
400 
401 /*
402  * Free a block of memory allocated by malloc.
403  */
404 #ifdef MALLOCLOG
405 void
406 _free(addr, type, file, line)
407 	void *addr;
408 	int type;
409 	const char *file;
410 	long line;
411 #else
412 void
413 free(addr, type)
414 	void *addr;
415 	int type;
416 #endif /* MALLOCLOG */
417 {
418 	struct kmembuckets *kbp;
419 	struct kmemusage *kup;
420 	struct freelist *freep;
421 	long size;
422 	int s;
423 #ifdef DIAGNOSTIC
424 	caddr_t cp;
425 	int32_t *end, *lp;
426 	long alloc, copysize;
427 #endif
428 #ifdef KMEMSTATS
429 	struct kmemstats *ksp = &kmemstats[type];
430 #endif
431 
432 #ifdef DIAGNOSTIC
433 	/*
434 	 * Ensure that we're free'ing something that we could
435 	 * have allocated in the first place.  That is, check
436 	 * to see that the address is within kmem_map.
437 	 */
438 	if (__predict_false((vaddr_t)addr < kmem_map->header.start ||
439 			    (vaddr_t)addr >= kmem_map->header.end))
440 		panic("free: addr %p not within kmem_map", addr);
441 #endif
442 
443 	kup = btokup(addr);
444 	size = 1 << kup->ku_indx;
445 	kbp = &bucket[kup->ku_indx];
446 	s = splmem();
447 #ifdef MALLOCLOG
448 	domlog(addr, 0, type, 2, file, line);
449 #endif
450 #ifdef DIAGNOSTIC
451 	/*
452 	 * Check for returns of data that do not point to the
453 	 * beginning of the allocation.
454 	 */
455 	if (size > PAGE_SIZE)
456 		alloc = addrmask[BUCKETINDX(PAGE_SIZE)];
457 	else
458 		alloc = addrmask[kup->ku_indx];
459 	if (((u_long)addr & alloc) != 0)
460 		panic("free: unaligned addr %p, size %ld, type %s, mask %ld\n",
461 			addr, size, memname[type], alloc);
462 #endif /* DIAGNOSTIC */
463 	if (size > MAXALLOCSAVE) {
464 		uvm_km_free(kmem_map, (vaddr_t)addr, ctob(kup->ku_pagecnt));
465 #ifdef KMEMSTATS
466 		size = kup->ku_pagecnt << PGSHIFT;
467 		ksp->ks_memuse -= size;
468 		kup->ku_indx = 0;
469 		kup->ku_pagecnt = 0;
470 		if (ksp->ks_memuse + size >= ksp->ks_limit &&
471 		    ksp->ks_memuse < ksp->ks_limit)
472 			wakeup((caddr_t)ksp);
473 		ksp->ks_inuse--;
474 		kbp->kb_total -= 1;
475 #endif
476 		splx(s);
477 		return;
478 	}
479 	freep = (struct freelist *)addr;
480 #ifdef DIAGNOSTIC
481 	/*
482 	 * Check for multiple frees. Use a quick check to see if
483 	 * it looks free before laboriously searching the freelist.
484 	 */
485 	if (__predict_false(freep->spare0 == WEIRD_ADDR)) {
486 		for (cp = kbp->kb_next; cp;
487 		    cp = ((struct freelist *)cp)->next) {
488 			if (addr != cp)
489 				continue;
490 			printf("multiply freed item %p\n", addr);
491 #ifdef MALLOCLOG
492 			hitmlog(addr);
493 #endif
494 			panic("free: duplicated free");
495 		}
496 	}
497 #ifdef LOCKDEBUG
498 	/*
499 	 * Check if we're freeing a locked simple lock.
500 	 */
501 	simple_lock_freecheck(addr, (char *)addr + size);
502 #endif
503 	/*
504 	 * Copy in known text to detect modification after freeing
505 	 * and to make it look free. Also, save the type being freed
506 	 * so we can list likely culprit if modification is detected
507 	 * when the object is reallocated.
508 	 */
509 	copysize = size < MAX_COPY ? size : MAX_COPY;
510 	end = (int32_t *)&((caddr_t)addr)[copysize];
511 	for (lp = (int32_t *)addr; lp < end; lp++)
512 		*lp = WEIRD_ADDR;
513 	freep->type = type;
514 #endif /* DIAGNOSTIC */
515 #ifdef KMEMSTATS
516 	kup->ku_freecnt++;
517 	if (kup->ku_freecnt >= kbp->kb_elmpercl) {
518 		if (kup->ku_freecnt > kbp->kb_elmpercl)
519 			panic("free: multiple frees");
520 		else if (kbp->kb_totalfree > kbp->kb_highwat)
521 			kbp->kb_couldfree++;
522 	}
523 	kbp->kb_totalfree++;
524 	ksp->ks_memuse -= size;
525 	if (ksp->ks_memuse + size >= ksp->ks_limit &&
526 	    ksp->ks_memuse < ksp->ks_limit)
527 		wakeup((caddr_t)ksp);
528 	ksp->ks_inuse--;
529 #endif
530 	if (kbp->kb_next == NULL)
531 		kbp->kb_next = addr;
532 	else
533 		((struct freelist *)kbp->kb_last)->next = addr;
534 	freep->next = NULL;
535 	kbp->kb_last = addr;
536 	splx(s);
537 }
538 
539 /*
540  * Change the size of a block of memory.
541  */
542 void *
543 realloc(curaddr, newsize, type, flags)
544 	void *curaddr;
545 	unsigned long newsize;
546 	int type, 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 	/*
570 	 * Find out how large the old allocation was (and do some
571 	 * sanity checking).
572 	 */
573 	kup = btokup(curaddr);
574 	cursize = 1 << kup->ku_indx;
575 
576 #ifdef DIAGNOSTIC
577 	/*
578 	 * Check for returns of data that do not point to the
579 	 * beginning of the allocation.
580 	 */
581 	if (cursize > PAGE_SIZE)
582 		alloc = addrmask[BUCKETINDX(PAGE_SIZE)];
583 	else
584 		alloc = addrmask[kup->ku_indx];
585 	if (((u_long)curaddr & alloc) != 0)
586 		panic("realloc: unaligned addr %p, size %ld, type %s, mask %ld\n",
587 			curaddr, cursize, memname[type], alloc);
588 #endif /* DIAGNOSTIC */
589 
590 	if (cursize > MAXALLOCSAVE)
591 		cursize = ctob(kup->ku_pagecnt);
592 
593 	/*
594 	 * If we already actually have as much as they want, we're done.
595 	 */
596 	if (newsize <= cursize)
597 		return (curaddr);
598 
599 	/*
600 	 * Can't satisfy the allocation with the existing block.
601 	 * Allocate a new one and copy the data.
602 	 */
603 	newaddr = malloc(newsize, type, flags);
604 	if (__predict_false(newaddr == NULL)) {
605 		/*
606 		 * Malloc() failed, because flags included M_NOWAIT.
607 		 * Return NULL to indicate that failure.  The old
608 		 * pointer is still valid.
609 		 */
610 		return NULL;
611 	}
612 	memcpy(newaddr, curaddr, cursize);
613 
614 	/*
615 	 * We were successful: free the old allocation and return
616 	 * the new one.
617 	 */
618 	free(curaddr, type);
619 	return (newaddr);
620 }
621 
622 /*
623  * Compute the number of pages that kmem_map will map, that is,
624  * the size of the kernel malloc arena.
625  */
626 void
627 kmeminit_nkmempages()
628 {
629 	int npages;
630 
631 	if (nkmempages != 0) {
632 		/*
633 		 * It's already been set (by us being here before, or
634 		 * by patching or kernel config options), bail out now.
635 		 */
636 		return;
637 	}
638 
639 	/*
640 	 * We use the following (simple) formula:
641 	 *
642 	 *	- Starting point is physical memory / 4.
643 	 *
644 	 *	- Clamp it down to NKMEMPAGES_MAX.
645 	 *
646 	 *	- Round it up to NKMEMPAGES_MIN.
647 	 */
648 	npages = physmem / 4;
649 
650 	if (npages > NKMEMPAGES_MAX)
651 		npages = NKMEMPAGES_MAX;
652 
653 	if (npages < NKMEMPAGES_MIN)
654 		npages = NKMEMPAGES_MIN;
655 
656 	nkmempages = npages;
657 }
658 
659 /*
660  * Initialize the kernel memory allocator
661  */
662 void
663 kmeminit()
664 {
665 #ifdef KMEMSTATS
666 	long indx;
667 #endif
668 
669 #if	((MAXALLOCSAVE & (MAXALLOCSAVE - 1)) != 0)
670 		ERROR!_kmeminit:_MAXALLOCSAVE_not_power_of_2
671 #endif
672 #if	(MAXALLOCSAVE > MINALLOCSIZE * 32768)
673 		ERROR!_kmeminit:_MAXALLOCSAVE_too_big
674 #endif
675 #if	(MAXALLOCSAVE < NBPG)
676 		ERROR!_kmeminit:_MAXALLOCSAVE_too_small
677 #endif
678 
679 	if (sizeof(struct freelist) > (1 << MINBUCKET))
680 		panic("minbucket too small/struct freelist too big");
681 
682 	/*
683 	 * Compute the number of kmem_map pages, if we have not
684 	 * done so already.
685 	 */
686 	kmeminit_nkmempages();
687 
688 	kmemusage = (struct kmemusage *) uvm_km_zalloc(kernel_map,
689 		(vsize_t)(nkmempages * sizeof(struct kmemusage)));
690 	kmem_map = uvm_km_suballoc(kernel_map, (vaddr_t *)&kmembase,
691 		(vaddr_t *)&kmemlimit, (vsize_t)(nkmempages << PAGE_SHIFT),
692 			VM_MAP_INTRSAFE, FALSE, &kmem_map_store.vmi_map);
693 #ifdef KMEMSTATS
694 	for (indx = 0; indx < MINBUCKET + 16; indx++) {
695 		if (1 << indx >= PAGE_SIZE)
696 			bucket[indx].kb_elmpercl = 1;
697 		else
698 			bucket[indx].kb_elmpercl = PAGE_SIZE / (1 << indx);
699 		bucket[indx].kb_highwat = 5 * bucket[indx].kb_elmpercl;
700 	}
701 	for (indx = 0; indx < M_LAST; indx++)
702 		kmemstats[indx].ks_limit = (nkmempages << PAGE_SHIFT) * 6 / 10;
703 #endif
704 }
705 
706 #ifdef DDB
707 #include <ddb/db_output.h>
708 
709 /*
710  * Dump kmem statistics from ddb.
711  *
712  * usage: call dump_kmemstats
713  */
714 void	dump_kmemstats __P((void));
715 
716 void
717 dump_kmemstats()
718 {
719 #ifdef KMEMSTATS
720 	const char *name;
721 	int i;
722 
723 	for (i = 0; i < M_LAST; i++) {
724 		name = memname[i] ? memname[i] : "";
725 
726 		db_printf("%2d %s%.*s %ld\n", i, name,
727 		    (int)(20 - strlen(name)), "                    ",
728 		    kmemstats[i].ks_memuse);
729 	}
730 #else
731 	db_printf("Kmem stats are not being collected.\n");
732 #endif /* KMEMSTATS */
733 }
734 #endif /* DDB */
735