xref: /openbsd-src/sys/kern/kern_malloc.c (revision daf88648c0e349d5c02e1504293082072c981640)
1 /*	$OpenBSD: kern_malloc.c,v 1.66 2007/01/12 07:41:31 art Exp $	*/
2 /*	$NetBSD: kern_malloc.c,v 1.15.4.2 1996/06/13 17:10:56 cgd Exp $	*/
3 
4 /*
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. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	@(#)kern_malloc.c	8.3 (Berkeley) 1/4/94
33  */
34 
35 #include <sys/param.h>
36 #include <sys/proc.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
39 #include <sys/systm.h>
40 #include <sys/sysctl.h>
41 #include <sys/time.h>
42 #include <sys/rwlock.h>
43 
44 #include <uvm/uvm_extern.h>
45 
46 static struct vm_map_intrsafe kmem_map_store;
47 struct vm_map *kmem_map = NULL;
48 
49 #ifdef NKMEMCLUSTERS
50 #error NKMEMCLUSTERS is obsolete; remove it from your kernel config file and use NKMEMPAGES instead or let the kernel auto-size
51 #endif
52 
53 /*
54  * Default number of pages in kmem_map.  We attempt to calculate this
55  * at run-time, but allow it to be either patched or set in the kernel
56  * config file.
57  */
58 #ifndef NKMEMPAGES
59 #define	NKMEMPAGES	0
60 #endif
61 u_int	nkmempages = NKMEMPAGES;
62 
63 /*
64  * Defaults for lower- and upper-bounds for the kmem_map page count.
65  * Can be overridden by kernel config options.
66  */
67 #ifndef	NKMEMPAGES_MIN
68 #define	NKMEMPAGES_MIN	NKMEMPAGES_MIN_DEFAULT
69 #endif
70 u_int	nkmempages_min = 0;
71 
72 #ifndef NKMEMPAGES_MAX
73 #define	NKMEMPAGES_MAX	NKMEMPAGES_MAX_DEFAULT
74 #endif
75 u_int	nkmempages_max = 0;
76 
77 struct kmembuckets bucket[MINBUCKET + 16];
78 struct kmemstats kmemstats[M_LAST];
79 struct kmemusage *kmemusage;
80 char *kmembase, *kmemlimit;
81 char buckstring[16 * sizeof("123456,")];
82 int buckstring_init = 0;
83 #if defined(KMEMSTATS) || defined(DIAGNOSTIC) || defined(FFS_SOFTUPDATES)
84 char *memname[] = INITKMEMNAMES;
85 char *memall = NULL;
86 struct rwlock sysctl_kmemlock = RWLOCK_INITIALIZER;
87 #endif
88 
89 #ifdef DIAGNOSTIC
90 /*
91  * This structure provides a set of masks to catch unaligned frees.
92  */
93 const long addrmask[] = { 0,
94 	0x00000001, 0x00000003, 0x00000007, 0x0000000f,
95 	0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff,
96 	0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff,
97 	0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff,
98 };
99 
100 /*
101  * The WEIRD_ADDR is used as known text to copy into free objects so
102  * that modifications after frees can be detected.
103  */
104 #define WEIRD_ADDR	((unsigned) 0xdeadbeef)
105 #define MAX_COPY	32
106 
107 /*
108  * Normally the freelist structure is used only to hold the list pointer
109  * for free objects.  However, when running with diagnostics, the first
110  * 8 bytes of the structure is unused except for diagnostic information,
111  * and the free list pointer is at offset 8 in the structure.  Since the
112  * first 8 bytes is the portion of the structure most often modified, this
113  * helps to detect memory reuse problems and avoid free list corruption.
114  */
115 struct freelist {
116 	int32_t	spare0;
117 	int16_t	type;
118 	int16_t	spare1;
119 	caddr_t	next;
120 };
121 #else /* !DIAGNOSTIC */
122 struct freelist {
123 	caddr_t	next;
124 };
125 #endif /* DIAGNOSTIC */
126 
127 #ifndef SMALL_KERNEL
128 struct timeval malloc_errintvl = { 5, 0 };
129 struct timeval malloc_lasterr;
130 #endif
131 
132 /*
133  * Allocate a block of memory
134  */
135 void *
136 malloc(unsigned long size, int type, int flags)
137 {
138 	struct kmembuckets *kbp;
139 	struct kmemusage *kup;
140 	struct freelist *freep;
141 	long indx, npg, allocsize;
142 	int s;
143 	caddr_t va, cp, savedlist;
144 #ifdef DIAGNOSTIC
145 	int32_t *end, *lp;
146 	int copysize;
147 	char *savedtype;
148 #endif
149 #ifdef KMEMSTATS
150 	struct kmemstats *ksp = &kmemstats[type];
151 
152 	if (((unsigned long)type) >= M_LAST)
153 		panic("malloc - bogus type");
154 #endif
155 
156 #ifdef MALLOC_DEBUG
157 	if (debug_malloc(size, type, flags, (void **)&va))
158 		return ((void *) va);
159 #endif
160 
161 	if (size > 65535 * PAGE_SIZE) {
162 		if (flags & M_CANFAIL) {
163 #ifndef SMALL_KERNEL
164 			if (ratecheck(&malloc_lasterr, &malloc_errintvl))
165 				printf("malloc(): allocation too large, "
166 				    "type = %d, size = %lu\n", type, size);
167 #endif
168 			return (NULL);
169 		} else
170 			panic("malloc: allocation too large");
171 	}
172 
173 	indx = BUCKETINDX(size);
174 	kbp = &bucket[indx];
175 	s = splvm();
176 #ifdef KMEMSTATS
177 	while (ksp->ks_memuse >= ksp->ks_limit) {
178 		if (flags & M_NOWAIT) {
179 			splx(s);
180 			return ((void *) NULL);
181 		}
182 		if (ksp->ks_limblocks < 65535)
183 			ksp->ks_limblocks++;
184 		tsleep(ksp, PSWP+2, memname[type], 0);
185 	}
186 	ksp->ks_size |= 1 << indx;
187 #endif
188 #ifdef DIAGNOSTIC
189 	copysize = 1 << indx < MAX_COPY ? 1 << indx : MAX_COPY;
190 #endif
191 	if (kbp->kb_next == NULL) {
192 		kbp->kb_last = NULL;
193 		if (size > MAXALLOCSAVE)
194 			allocsize = round_page(size);
195 		else
196 			allocsize = 1 << indx;
197 		npg = btoc(allocsize);
198 		va = (caddr_t) uvm_km_kmemalloc(kmem_map, uvmexp.kmem_object,
199 		    (vsize_t)ctob(npg),
200 		    ((flags & M_NOWAIT) ? UVM_KMF_NOWAIT : 0) |
201 		    ((flags & M_CANFAIL) ? UVM_KMF_CANFAIL : 0));
202 		if (va == NULL) {
203 			/*
204 			 * Kmem_malloc() can return NULL, even if it can
205 			 * wait, if there is no map space available, because
206 			 * it can't fix that problem.  Neither can we,
207 			 * right now.  (We should release pages which
208 			 * are completely free and which are in buckets
209 			 * with too many free elements.)
210 			 */
211 			if ((flags & (M_NOWAIT|M_CANFAIL)) == 0)
212 				panic("malloc: out of space in kmem_map");
213 			splx(s);
214 			return (NULL);
215 		}
216 #ifdef KMEMSTATS
217 		kbp->kb_total += kbp->kb_elmpercl;
218 #endif
219 		kup = btokup(va);
220 		kup->ku_indx = indx;
221 		if (allocsize > MAXALLOCSAVE) {
222 			kup->ku_pagecnt = npg;
223 #ifdef KMEMSTATS
224 			ksp->ks_memuse += allocsize;
225 #endif
226 			goto out;
227 		}
228 #ifdef KMEMSTATS
229 		kup->ku_freecnt = kbp->kb_elmpercl;
230 		kbp->kb_totalfree += kbp->kb_elmpercl;
231 #endif
232 		/*
233 		 * Just in case we blocked while allocating memory,
234 		 * and someone else also allocated memory for this
235 		 * bucket, don't assume the list is still empty.
236 		 */
237 		savedlist = kbp->kb_next;
238 		kbp->kb_next = cp = va + (npg * PAGE_SIZE) - allocsize;
239 		for (;;) {
240 			freep = (struct freelist *)cp;
241 #ifdef DIAGNOSTIC
242 			/*
243 			 * Copy in known text to detect modification
244 			 * after freeing.
245 			 */
246 			end = (int32_t *)&cp[copysize];
247 			for (lp = (int32_t *)cp; lp < end; lp++)
248 				*lp = WEIRD_ADDR;
249 			freep->type = M_FREE;
250 #endif /* DIAGNOSTIC */
251 			if (cp <= va)
252 				break;
253 			cp -= allocsize;
254 			freep->next = cp;
255 		}
256 		freep->next = savedlist;
257 		if (kbp->kb_last == NULL)
258 			kbp->kb_last = (caddr_t)freep;
259 	}
260 	va = kbp->kb_next;
261 	kbp->kb_next = ((struct freelist *)va)->next;
262 #ifdef DIAGNOSTIC
263 	freep = (struct freelist *)va;
264 	savedtype = (unsigned)freep->type < M_LAST ?
265 		memname[freep->type] : "???";
266 	if (kbp->kb_next) {
267 		int rv;
268 		vaddr_t addr = (vaddr_t)kbp->kb_next;
269 
270 		vm_map_lock(kmem_map);
271 		rv = uvm_map_checkprot(kmem_map, addr,
272 		    addr + sizeof(struct freelist), VM_PROT_WRITE);
273 		vm_map_unlock(kmem_map);
274 
275 		if (!rv)  {
276 		printf("%s %d of object %p size 0x%lx %s %s (invalid addr %p)\n",
277 			"Data modified on freelist: word",
278 			(int32_t *)&kbp->kb_next - (int32_t *)kbp, va, size,
279 			"previous type", savedtype, kbp->kb_next);
280 		kbp->kb_next = NULL;
281 		}
282 	}
283 
284 	/* Fill the fields that we've used with WEIRD_ADDR */
285 #if BYTE_ORDER == BIG_ENDIAN
286 	freep->type = WEIRD_ADDR >> 16;
287 #endif
288 #if BYTE_ORDER == LITTLE_ENDIAN
289 	freep->type = (short)WEIRD_ADDR;
290 #endif
291 	end = (int32_t *)&freep->next +
292 	    (sizeof(freep->next) / sizeof(int32_t));
293 	for (lp = (int32_t *)&freep->next; lp < end; lp++)
294 		*lp = WEIRD_ADDR;
295 
296 	/* and check that the data hasn't been modified. */
297 	end = (int32_t *)&va[copysize];
298 	for (lp = (int32_t *)va; lp < end; lp++) {
299 		if (*lp == WEIRD_ADDR)
300 			continue;
301 		printf("%s %d of object %p size 0x%lx %s %s (0x%x != 0x%x)\n",
302 			"Data modified on freelist: word", lp - (int32_t *)va,
303 			va, size, "previous type", savedtype, *lp, WEIRD_ADDR);
304 		break;
305 	}
306 
307 	freep->spare0 = 0;
308 #endif /* DIAGNOSTIC */
309 #ifdef KMEMSTATS
310 	kup = btokup(va);
311 	if (kup->ku_indx != indx)
312 		panic("malloc: wrong bucket");
313 	if (kup->ku_freecnt == 0)
314 		panic("malloc: lost data");
315 	kup->ku_freecnt--;
316 	kbp->kb_totalfree--;
317 	ksp->ks_memuse += 1 << indx;
318 out:
319 	kbp->kb_calls++;
320 	ksp->ks_inuse++;
321 	ksp->ks_calls++;
322 	if (ksp->ks_memuse > ksp->ks_maxused)
323 		ksp->ks_maxused = ksp->ks_memuse;
324 #else
325 out:
326 #endif
327 	splx(s);
328 	return ((void *) va);
329 }
330 
331 /*
332  * Free a block of memory allocated by malloc.
333  */
334 void
335 free(void *addr, int type)
336 {
337 	struct kmembuckets *kbp;
338 	struct kmemusage *kup;
339 	struct freelist *freep;
340 	long size;
341 	int s;
342 #ifdef DIAGNOSTIC
343 	caddr_t cp;
344 	int32_t *end, *lp;
345 	long alloc, copysize;
346 #endif
347 #ifdef KMEMSTATS
348 	struct kmemstats *ksp = &kmemstats[type];
349 #endif
350 
351 #ifdef MALLOC_DEBUG
352 	if (debug_free(addr, type))
353 		return;
354 #endif
355 
356 #ifdef DIAGNOSTIC
357 	if (addr < (void *)kmembase || addr >= (void *)kmemlimit)
358 		panic("free: non-malloced addr %p type %s", addr,
359 		    memname[type]);
360 #endif
361 
362 	kup = btokup(addr);
363 	size = 1 << kup->ku_indx;
364 	kbp = &bucket[kup->ku_indx];
365 	s = splvm();
366 #ifdef DIAGNOSTIC
367 	/*
368 	 * Check for returns of data that do not point to the
369 	 * beginning of the allocation.
370 	 */
371 	if (size > PAGE_SIZE)
372 		alloc = addrmask[BUCKETINDX(PAGE_SIZE)];
373 	else
374 		alloc = addrmask[kup->ku_indx];
375 	if (((u_long)addr & alloc) != 0)
376 		panic("free: unaligned addr %p, size %ld, type %s, mask %ld",
377 			addr, size, memname[type], alloc);
378 #endif /* DIAGNOSTIC */
379 	if (size > MAXALLOCSAVE) {
380 		uvm_km_free(kmem_map, (vaddr_t)addr, ctob(kup->ku_pagecnt));
381 #ifdef KMEMSTATS
382 		size = kup->ku_pagecnt << PGSHIFT;
383 		ksp->ks_memuse -= size;
384 		kup->ku_indx = 0;
385 		kup->ku_pagecnt = 0;
386 		if (ksp->ks_memuse + size >= ksp->ks_limit &&
387 		    ksp->ks_memuse < ksp->ks_limit)
388 			wakeup(ksp);
389 		ksp->ks_inuse--;
390 		kbp->kb_total -= 1;
391 #endif
392 		splx(s);
393 		return;
394 	}
395 	freep = (struct freelist *)addr;
396 #ifdef DIAGNOSTIC
397 	/*
398 	 * Check for multiple frees. Use a quick check to see if
399 	 * it looks free before laboriously searching the freelist.
400 	 */
401 	if (freep->spare0 == WEIRD_ADDR) {
402 		for (cp = kbp->kb_next; cp;
403 		    cp = ((struct freelist *)cp)->next) {
404 			if (addr != cp)
405 				continue;
406 			printf("multiply freed item %p\n", addr);
407 			panic("free: duplicated free");
408 		}
409 	}
410 	/*
411 	 * Copy in known text to detect modification after freeing
412 	 * and to make it look free. Also, save the type being freed
413 	 * so we can list likely culprit if modification is detected
414 	 * when the object is reallocated.
415 	 */
416 	copysize = size < MAX_COPY ? size : MAX_COPY;
417 	end = (int32_t *)&((caddr_t)addr)[copysize];
418 	for (lp = (int32_t *)addr; lp < end; lp++)
419 		*lp = WEIRD_ADDR;
420 	freep->type = type;
421 #endif /* DIAGNOSTIC */
422 #ifdef KMEMSTATS
423 	kup->ku_freecnt++;
424 	if (kup->ku_freecnt >= kbp->kb_elmpercl) {
425 		if (kup->ku_freecnt > kbp->kb_elmpercl)
426 			panic("free: multiple frees");
427 		else if (kbp->kb_totalfree > kbp->kb_highwat)
428 			kbp->kb_couldfree++;
429 	}
430 	kbp->kb_totalfree++;
431 	ksp->ks_memuse -= size;
432 	if (ksp->ks_memuse + size >= ksp->ks_limit &&
433 	    ksp->ks_memuse < ksp->ks_limit)
434 		wakeup(ksp);
435 	ksp->ks_inuse--;
436 #endif
437 	if (kbp->kb_next == NULL)
438 		kbp->kb_next = addr;
439 	else
440 		((struct freelist *)kbp->kb_last)->next = addr;
441 	freep->next = NULL;
442 	kbp->kb_last = addr;
443 	splx(s);
444 }
445 
446 /*
447  * Compute the number of pages that kmem_map will map, that is,
448  * the size of the kernel malloc arena.
449  */
450 void
451 kmeminit_nkmempages(void)
452 {
453 	u_int npages;
454 
455 	if (nkmempages != 0) {
456 		/*
457 		 * It's already been set (by us being here before, or
458 		 * by patching or kernel config options), bail out now.
459 		 */
460 		return;
461 	}
462 
463 	/*
464 	 * We can't initialize these variables at compilation time, since
465 	 * the page size may not be known (on sparc GENERIC kernels, for
466 	 * example). But we still want the MD code to be able to provide
467 	 * better values.
468 	 */
469 	if (nkmempages_min == 0)
470 		nkmempages_min = NKMEMPAGES_MIN;
471 	if (nkmempages_max == 0)
472 		nkmempages_max = NKMEMPAGES_MAX;
473 
474 	/*
475 	 * We use the following (simple) formula:
476 	 *
477 	 *	- Starting point is physical memory / 4.
478 	 *
479 	 *	- Clamp it down to nkmempages_max.
480 	 *
481 	 *	- Round it up to nkmempages_min.
482 	 */
483 	npages = physmem / 4;
484 
485 	if (npages > nkmempages_max)
486 		npages = nkmempages_max;
487 
488 	if (npages < nkmempages_min)
489 		npages = nkmempages_min;
490 
491 	nkmempages = npages;
492 }
493 
494 /*
495  * Initialize the kernel memory allocator
496  */
497 void
498 kmeminit(void)
499 {
500 	vaddr_t base, limit;
501 #ifdef KMEMSTATS
502 	long indx;
503 #endif
504 
505 #ifdef DIAGNOSTIC
506 	if (sizeof(struct freelist) > (1 << MINBUCKET))
507 		panic("kmeminit: minbucket too small/struct freelist too big");
508 #endif
509 
510 	/*
511 	 * Compute the number of kmem_map pages, if we have not
512 	 * done so already.
513 	 */
514 	kmeminit_nkmempages();
515 	base = vm_map_min(kernel_map);
516 	kmem_map = uvm_km_suballoc(kernel_map, &base, &limit,
517 	    (vsize_t)(nkmempages * PAGE_SIZE), VM_MAP_INTRSAFE, FALSE,
518 	    &kmem_map_store.vmi_map);
519 	kmembase = (char *)base;
520 	kmemlimit = (char *)limit;
521 	kmemusage = (struct kmemusage *) uvm_km_zalloc(kernel_map,
522 		(vsize_t)(nkmempages * sizeof(struct kmemusage)));
523 #ifdef KMEMSTATS
524 	for (indx = 0; indx < MINBUCKET + 16; indx++) {
525 		if (1 << indx >= PAGE_SIZE)
526 			bucket[indx].kb_elmpercl = 1;
527 		else
528 			bucket[indx].kb_elmpercl = PAGE_SIZE / (1 << indx);
529 		bucket[indx].kb_highwat = 5 * bucket[indx].kb_elmpercl;
530 	}
531 	for (indx = 0; indx < M_LAST; indx++)
532 		kmemstats[indx].ks_limit = nkmempages * PAGE_SIZE * 6 / 10;
533 #endif
534 #ifdef MALLOC_DEBUG
535 	debug_malloc_init();
536 #endif
537 }
538 
539 /*
540  * Return kernel malloc statistics information.
541  */
542 int
543 sysctl_malloc(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
544     size_t newlen, struct proc *p)
545 {
546 	struct kmembuckets kb;
547 	int i, siz;
548 
549 	if (namelen != 2 && name[0] != KERN_MALLOC_BUCKETS &&
550 	    name[0] != KERN_MALLOC_KMEMNAMES)
551 		return (ENOTDIR);		/* overloaded */
552 
553 	switch (name[0]) {
554 	case KERN_MALLOC_BUCKETS:
555 		/* Initialize the first time */
556 		if (buckstring_init == 0) {
557 			buckstring_init = 1;
558 			bzero(buckstring, sizeof(buckstring));
559 			for (siz = 0, i = MINBUCKET; i < MINBUCKET + 16; i++) {
560 				snprintf(buckstring + siz,
561 				    sizeof buckstring - siz,
562 				    "%d,", (u_int)(1<<i));
563 				siz += strlen(buckstring + siz);
564 			}
565 			/* Remove trailing comma */
566 			if (siz)
567 				buckstring[siz - 1] = '\0';
568 		}
569 		return (sysctl_rdstring(oldp, oldlenp, newp, buckstring));
570 
571 	case KERN_MALLOC_BUCKET:
572 		bcopy(&bucket[BUCKETINDX(name[1])], &kb, sizeof(kb));
573 		kb.kb_next = kb.kb_last = 0;
574 		return (sysctl_rdstruct(oldp, oldlenp, newp, &kb, sizeof(kb)));
575 	case KERN_MALLOC_KMEMSTATS:
576 #ifdef KMEMSTATS
577 		if ((name[1] < 0) || (name[1] >= M_LAST))
578 			return (EINVAL);
579 		return (sysctl_rdstruct(oldp, oldlenp, newp,
580 		    &kmemstats[name[1]], sizeof(struct kmemstats)));
581 #else
582 		return (EOPNOTSUPP);
583 #endif
584 	case KERN_MALLOC_KMEMNAMES:
585 #if defined(KMEMSTATS) || defined(DIAGNOSTIC) || defined(FFS_SOFTUPDATES)
586 		if (memall == NULL) {
587 			int totlen;
588 
589 			i = rw_enter(&sysctl_kmemlock, RW_WRITE|RW_INTR);
590 			if (i)
591 				return (i);
592 
593 			/* Figure out how large a buffer we need */
594 			for (totlen = 0, i = 0; i < M_LAST; i++) {
595 				if (memname[i])
596 					totlen += strlen(memname[i]);
597 				totlen++;
598 			}
599 			memall = malloc(totlen + M_LAST, M_SYSCTL, M_WAITOK);
600 			bzero(memall, totlen + M_LAST);
601 			for (siz = 0, i = 0; i < M_LAST; i++) {
602 				snprintf(memall + siz,
603 				    totlen + M_LAST - siz,
604 				    "%s,", memname[i] ? memname[i] : "");
605 				siz += strlen(memall + siz);
606 			}
607 			/* Remove trailing comma */
608 			if (siz)
609 				memall[siz - 1] = '\0';
610 
611 			/* Now, convert all spaces to underscores */
612 			for (i = 0; i < totlen; i++)
613 				if (memall[i] == ' ')
614 					memall[i] = '_';
615 			rw_exit_write(&sysctl_kmemlock);
616 		}
617 		return (sysctl_rdstring(oldp, oldlenp, newp, memall));
618 #else
619 		return (EOPNOTSUPP);
620 #endif
621 	default:
622 		return (EOPNOTSUPP);
623 	}
624 	/* NOTREACHED */
625 }
626 
627 /*
628  * Round up a size to how much malloc would actually allocate.
629  */
630 size_t
631 malloc_roundup(size_t sz)
632 {
633 	if (sz > MAXALLOCSAVE)
634 		return round_page(sz);
635 
636 	return (1 << BUCKETINDX(sz));
637 }
638 
639 #if defined(DDB)
640 #include <machine/db_machdep.h>
641 #include <ddb/db_interface.h>
642 #include <ddb/db_output.h>
643 
644 void
645 malloc_printit(int (*pr)(const char *, ...))
646 {
647 #ifdef KMEMSTATS
648 	struct kmemstats *km;
649 	int i;
650 
651 	(*pr)("%15s %5s  %6s  %7s  %6s %9s %8s %8s\n",
652 	    "Type", "InUse", "MemUse", "HighUse", "Limit", "Requests",
653 	    "Type Lim", "Kern Lim");
654 	for (i = 0, km = kmemstats; i < M_LAST; i++, km++) {
655 		if (!km->ks_calls || !memname[i])
656 			continue;
657 
658 		(*pr)("%15s %5ld %6ldK %7ldK %6ldK %9ld %8d %8d\n",
659 		    memname[i], km->ks_inuse, km->ks_memuse / 1024,
660 		    km->ks_maxused / 1024, km->ks_limit / 1024,
661 		    km->ks_calls, km->ks_limblocks, km->ks_mapblocks);
662 	}
663 #else
664 	(*pr)("No KMEMSTATS compiled in\n");
665 #endif
666 }
667 #endif /* DDB */
668