xref: /openbsd-src/sys/kern/kern_malloc.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: kern_malloc.c,v 1.79 2009/02/22 19:57:59 miod 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 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	0
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("sysctlklk");
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 #ifdef DEADBEEF0
105 #define WEIRD_ADDR	((unsigned) DEADBEEF0)
106 #else
107 #define WEIRD_ADDR	((unsigned) 0xdeadbeef)
108 #endif
109 #define MAX_COPY	32
110 
111 /*
112  * Normally the freelist structure is used only to hold the list pointer
113  * for free objects.  However, when running with diagnostics, the first
114  * 8 bytes of the structure is unused except for diagnostic information,
115  * and the free list pointer is at offset 8 in the structure.  Since the
116  * first 8 bytes is the portion of the structure most often modified, this
117  * helps to detect memory reuse problems and avoid free list corruption.
118  */
119 struct freelist {
120 	int32_t	spare0;
121 	int16_t	type;
122 	int16_t	spare1;
123 	caddr_t	next;
124 };
125 #else /* !DIAGNOSTIC */
126 struct freelist {
127 	caddr_t	next;
128 };
129 #endif /* DIAGNOSTIC */
130 
131 #ifndef SMALL_KERNEL
132 struct timeval malloc_errintvl = { 5, 0 };
133 struct timeval malloc_lasterr;
134 #endif
135 
136 /*
137  * Allocate a block of memory
138  */
139 void *
140 malloc(unsigned long size, int type, int flags)
141 {
142 	struct kmembuckets *kbp;
143 	struct kmemusage *kup;
144 	struct freelist *freep;
145 	long indx, npg, allocsize;
146 	int s;
147 	caddr_t va, cp, savedlist;
148 #ifdef DIAGNOSTIC
149 	int32_t *end, *lp;
150 	int copysize;
151 	char *savedtype;
152 #endif
153 #ifdef KMEMSTATS
154 	struct kmemstats *ksp = &kmemstats[type];
155 
156 	if (((unsigned long)type) >= M_LAST)
157 		panic("malloc - bogus type");
158 #endif
159 
160 #ifdef MALLOC_DEBUG
161 	if (debug_malloc(size, type, flags, (void **)&va)) {
162 		if ((flags & M_ZERO) && va != NULL)
163 			memset(va, 0, size);
164 		return (va);
165 	}
166 #endif
167 
168 	if (size > 65535 * PAGE_SIZE) {
169 		if (flags & M_CANFAIL) {
170 #ifndef SMALL_KERNEL
171 			if (ratecheck(&malloc_lasterr, &malloc_errintvl))
172 				printf("malloc(): allocation too large, "
173 				    "type = %d, size = %lu\n", type, size);
174 #endif
175 			return (NULL);
176 		} else
177 			panic("malloc: allocation too large");
178 	}
179 
180 	indx = BUCKETINDX(size);
181 	kbp = &bucket[indx];
182 	s = splvm();
183 #ifdef KMEMSTATS
184 	while (ksp->ks_memuse >= ksp->ks_limit) {
185 		if (flags & M_NOWAIT) {
186 			splx(s);
187 			return (NULL);
188 		}
189 		if (ksp->ks_limblocks < 65535)
190 			ksp->ks_limblocks++;
191 		tsleep(ksp, PSWP+2, memname[type], 0);
192 	}
193 	ksp->ks_size |= 1 << indx;
194 #endif
195 #ifdef DIAGNOSTIC
196 	copysize = 1 << indx < MAX_COPY ? 1 << indx : MAX_COPY;
197 #endif
198 	if (kbp->kb_next == NULL) {
199 		if (size > MAXALLOCSAVE)
200 			allocsize = round_page(size);
201 		else
202 			allocsize = 1 << indx;
203 		npg = atop(round_page(allocsize));
204 		va = (caddr_t) uvm_km_kmemalloc(kmem_map, NULL,
205 		    (vsize_t)ptoa(npg),
206 		    ((flags & M_NOWAIT) ? UVM_KMF_NOWAIT : 0) |
207 		    ((flags & M_CANFAIL) ? UVM_KMF_CANFAIL : 0));
208 		if (va == NULL) {
209 			/*
210 			 * Kmem_malloc() can return NULL, even if it can
211 			 * wait, if there is no map space available, because
212 			 * it can't fix that problem.  Neither can we,
213 			 * right now.  (We should release pages which
214 			 * are completely free and which are in buckets
215 			 * with too many free elements.)
216 			 */
217 			if ((flags & (M_NOWAIT|M_CANFAIL)) == 0)
218 				panic("malloc: out of space in kmem_map");
219 			splx(s);
220 			return (NULL);
221 		}
222 #ifdef KMEMSTATS
223 		kbp->kb_total += kbp->kb_elmpercl;
224 #endif
225 		kup = btokup(va);
226 		kup->ku_indx = indx;
227 		if (allocsize > MAXALLOCSAVE) {
228 			kup->ku_pagecnt = npg;
229 #ifdef KMEMSTATS
230 			ksp->ks_memuse += allocsize;
231 #endif
232 			goto out;
233 		}
234 #ifdef KMEMSTATS
235 		kup->ku_freecnt = kbp->kb_elmpercl;
236 		kbp->kb_totalfree += kbp->kb_elmpercl;
237 #endif
238 		/*
239 		 * Just in case we blocked while allocating memory,
240 		 * and someone else also allocated memory for this
241 		 * bucket, don't assume the list is still empty.
242 		 */
243 		savedlist = kbp->kb_next;
244 		kbp->kb_next = cp = va + (npg * PAGE_SIZE) - allocsize;
245 		for (;;) {
246 			freep = (struct freelist *)cp;
247 #ifdef DIAGNOSTIC
248 			/*
249 			 * Copy in known text to detect modification
250 			 * after freeing.
251 			 */
252 			end = (int32_t *)&cp[copysize];
253 			for (lp = (int32_t *)cp; lp < end; lp++)
254 				*lp = WEIRD_ADDR;
255 			freep->type = M_FREE;
256 #endif /* DIAGNOSTIC */
257 			if (cp <= va)
258 				break;
259 			cp -= allocsize;
260 			freep->next = cp;
261 		}
262 		freep->next = savedlist;
263 		if (savedlist == NULL)
264 			kbp->kb_last = (caddr_t)freep;
265 	}
266 	va = kbp->kb_next;
267 	kbp->kb_next = ((struct freelist *)va)->next;
268 #ifdef DIAGNOSTIC
269 	freep = (struct freelist *)va;
270 	savedtype = (unsigned)freep->type < M_LAST ?
271 		memname[freep->type] : "???";
272 	if (kbp->kb_next) {
273 		int rv;
274 		vaddr_t addr = (vaddr_t)kbp->kb_next;
275 
276 		vm_map_lock(kmem_map);
277 		rv = uvm_map_checkprot(kmem_map, addr,
278 		    addr + sizeof(struct freelist), VM_PROT_WRITE);
279 		vm_map_unlock(kmem_map);
280 
281 		if (!rv)  {
282 		printf("%s %d of object %p size 0x%lx %s %s (invalid addr %p)\n",
283 			"Data modified on freelist: word",
284 			(int32_t *)&kbp->kb_next - (int32_t *)kbp, va, size,
285 			"previous type", savedtype, kbp->kb_next);
286 		kbp->kb_next = NULL;
287 		}
288 	}
289 
290 	/* Fill the fields that we've used with WEIRD_ADDR */
291 #if BYTE_ORDER == BIG_ENDIAN
292 	freep->type = WEIRD_ADDR >> 16;
293 #endif
294 #if BYTE_ORDER == LITTLE_ENDIAN
295 	freep->type = (short)WEIRD_ADDR;
296 #endif
297 	end = (int32_t *)&freep->next +
298 	    (sizeof(freep->next) / sizeof(int32_t));
299 	for (lp = (int32_t *)&freep->next; lp < end; lp++)
300 		*lp = WEIRD_ADDR;
301 
302 	/* and check that the data hasn't been modified. */
303 	end = (int32_t *)&va[copysize];
304 	for (lp = (int32_t *)va; lp < end; lp++) {
305 		if (*lp == WEIRD_ADDR)
306 			continue;
307 		printf("%s %d of object %p size 0x%lx %s %s (0x%x != 0x%x)\n",
308 			"Data modified on freelist: word", lp - (int32_t *)va,
309 			va, size, "previous type", savedtype, *lp, WEIRD_ADDR);
310 		break;
311 	}
312 
313 	freep->spare0 = 0;
314 #endif /* DIAGNOSTIC */
315 #ifdef KMEMSTATS
316 	kup = btokup(va);
317 	if (kup->ku_indx != indx)
318 		panic("malloc: wrong bucket");
319 	if (kup->ku_freecnt == 0)
320 		panic("malloc: lost data");
321 	kup->ku_freecnt--;
322 	kbp->kb_totalfree--;
323 	ksp->ks_memuse += 1 << indx;
324 out:
325 	kbp->kb_calls++;
326 	ksp->ks_inuse++;
327 	ksp->ks_calls++;
328 	if (ksp->ks_memuse > ksp->ks_maxused)
329 		ksp->ks_maxused = ksp->ks_memuse;
330 #else
331 out:
332 #endif
333 	splx(s);
334 
335 	if ((flags & M_ZERO) && va != NULL)
336 		memset(va, 0, size);
337 	return (va);
338 }
339 
340 /*
341  * Free a block of memory allocated by malloc.
342  */
343 void
344 free(void *addr, int type)
345 {
346 	struct kmembuckets *kbp;
347 	struct kmemusage *kup;
348 	struct freelist *freep;
349 	long size;
350 	int s;
351 #ifdef DIAGNOSTIC
352 	caddr_t cp;
353 	int32_t *end, *lp;
354 	long alloc, copysize;
355 #endif
356 #ifdef KMEMSTATS
357 	struct kmemstats *ksp = &kmemstats[type];
358 #endif
359 
360 #ifdef MALLOC_DEBUG
361 	if (debug_free(addr, type))
362 		return;
363 #endif
364 
365 #ifdef DIAGNOSTIC
366 	if (addr < (void *)kmembase || addr >= (void *)kmemlimit)
367 		panic("free: non-malloced addr %p type %s", addr,
368 		    memname[type]);
369 #endif
370 
371 	kup = btokup(addr);
372 	size = 1 << kup->ku_indx;
373 	kbp = &bucket[kup->ku_indx];
374 	s = splvm();
375 #ifdef DIAGNOSTIC
376 	/*
377 	 * Check for returns of data that do not point to the
378 	 * beginning of the allocation.
379 	 */
380 	if (size > PAGE_SIZE)
381 		alloc = addrmask[BUCKETINDX(PAGE_SIZE)];
382 	else
383 		alloc = addrmask[kup->ku_indx];
384 	if (((u_long)addr & alloc) != 0)
385 		panic("free: unaligned addr %p, size %ld, type %s, mask %ld",
386 			addr, size, memname[type], alloc);
387 #endif /* DIAGNOSTIC */
388 	if (size > MAXALLOCSAVE) {
389 		uvm_km_free(kmem_map, (vaddr_t)addr, ptoa(kup->ku_pagecnt));
390 #ifdef KMEMSTATS
391 		size = kup->ku_pagecnt << PGSHIFT;
392 		ksp->ks_memuse -= size;
393 		kup->ku_indx = 0;
394 		kup->ku_pagecnt = 0;
395 		if (ksp->ks_memuse + size >= ksp->ks_limit &&
396 		    ksp->ks_memuse < ksp->ks_limit)
397 			wakeup(ksp);
398 		ksp->ks_inuse--;
399 		kbp->kb_total -= 1;
400 #endif
401 		splx(s);
402 		return;
403 	}
404 	freep = (struct freelist *)addr;
405 #ifdef DIAGNOSTIC
406 	/*
407 	 * Check for multiple frees. Use a quick check to see if
408 	 * it looks free before laboriously searching the freelist.
409 	 */
410 	if (freep->spare0 == WEIRD_ADDR) {
411 		for (cp = kbp->kb_next; cp;
412 		    cp = ((struct freelist *)cp)->next) {
413 			if (addr != cp)
414 				continue;
415 			printf("multiply freed item %p\n", addr);
416 			panic("free: duplicated free");
417 		}
418 	}
419 	/*
420 	 * Copy in known text to detect modification after freeing
421 	 * and to make it look free. Also, save the type being freed
422 	 * so we can list likely culprit if modification is detected
423 	 * when the object is reallocated.
424 	 */
425 	copysize = size < MAX_COPY ? size : MAX_COPY;
426 	end = (int32_t *)&((caddr_t)addr)[copysize];
427 	for (lp = (int32_t *)addr; lp < end; lp++)
428 		*lp = WEIRD_ADDR;
429 	freep->type = type;
430 #endif /* DIAGNOSTIC */
431 #ifdef KMEMSTATS
432 	kup->ku_freecnt++;
433 	if (kup->ku_freecnt >= kbp->kb_elmpercl) {
434 		if (kup->ku_freecnt > kbp->kb_elmpercl)
435 			panic("free: multiple frees");
436 		else if (kbp->kb_totalfree > kbp->kb_highwat)
437 			kbp->kb_couldfree++;
438 	}
439 	kbp->kb_totalfree++;
440 	ksp->ks_memuse -= size;
441 	if (ksp->ks_memuse + size >= ksp->ks_limit &&
442 	    ksp->ks_memuse < ksp->ks_limit)
443 		wakeup(ksp);
444 	ksp->ks_inuse--;
445 #endif
446 	if (kbp->kb_next == NULL)
447 		kbp->kb_next = addr;
448 	else
449 		((struct freelist *)kbp->kb_last)->next = addr;
450 	freep->next = NULL;
451 	kbp->kb_last = addr;
452 	splx(s);
453 }
454 
455 /*
456  * Compute the number of pages that kmem_map will map, that is,
457  * the size of the kernel malloc arena.
458  */
459 void
460 kmeminit_nkmempages(void)
461 {
462 	u_int npages;
463 
464 	if (nkmempages != 0) {
465 		/*
466 		 * It's already been set (by us being here before, or
467 		 * by patching or kernel config options), bail out now.
468 		 */
469 		return;
470 	}
471 
472 	/*
473 	 * We can't initialize these variables at compilation time, since
474 	 * the page size may not be known (on sparc GENERIC kernels, for
475 	 * example). But we still want the MD code to be able to provide
476 	 * better values.
477 	 */
478 	if (nkmempages_min == 0)
479 		nkmempages_min = NKMEMPAGES_MIN;
480 	if (nkmempages_max == 0)
481 		nkmempages_max = NKMEMPAGES_MAX;
482 
483 	/*
484 	 * We use the following (simple) formula:
485 	 *
486 	 *	- Starting point is physical memory / 4.
487 	 *
488 	 *	- Clamp it down to nkmempages_max.
489 	 *
490 	 *	- Round it up to nkmempages_min.
491 	 */
492 	npages = physmem / 4;
493 
494 	if (npages > nkmempages_max)
495 		npages = nkmempages_max;
496 
497 	if (npages < nkmempages_min)
498 		npages = nkmempages_min;
499 
500 	nkmempages = npages;
501 }
502 
503 /*
504  * Initialize the kernel memory allocator
505  */
506 void
507 kmeminit(void)
508 {
509 	vaddr_t base, limit;
510 #ifdef KMEMSTATS
511 	long indx;
512 #endif
513 
514 #ifdef DIAGNOSTIC
515 	if (sizeof(struct freelist) > (1 << MINBUCKET))
516 		panic("kmeminit: minbucket too small/struct freelist too big");
517 #endif
518 
519 	/*
520 	 * Compute the number of kmem_map pages, if we have not
521 	 * done so already.
522 	 */
523 	kmeminit_nkmempages();
524 	base = vm_map_min(kernel_map);
525 	kmem_map = uvm_km_suballoc(kernel_map, &base, &limit,
526 	    (vsize_t)(nkmempages * PAGE_SIZE), VM_MAP_INTRSAFE, FALSE,
527 	    &kmem_map_store);
528 	kmembase = (char *)base;
529 	kmemlimit = (char *)limit;
530 	kmemusage = (struct kmemusage *) uvm_km_zalloc(kernel_map,
531 		(vsize_t)(nkmempages * sizeof(struct kmemusage)));
532 #ifdef KMEMSTATS
533 	for (indx = 0; indx < MINBUCKET + 16; indx++) {
534 		if (1 << indx >= PAGE_SIZE)
535 			bucket[indx].kb_elmpercl = 1;
536 		else
537 			bucket[indx].kb_elmpercl = PAGE_SIZE / (1 << indx);
538 		bucket[indx].kb_highwat = 5 * bucket[indx].kb_elmpercl;
539 	}
540 	for (indx = 0; indx < M_LAST; indx++)
541 		kmemstats[indx].ks_limit = nkmempages * PAGE_SIZE * 6 / 10;
542 #endif
543 #ifdef MALLOC_DEBUG
544 	debug_malloc_init();
545 #endif
546 }
547 
548 /*
549  * Return kernel malloc statistics information.
550  */
551 int
552 sysctl_malloc(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
553     size_t newlen, struct proc *p)
554 {
555 	struct kmembuckets kb;
556 	int i, siz;
557 
558 	if (namelen != 2 && name[0] != KERN_MALLOC_BUCKETS &&
559 	    name[0] != KERN_MALLOC_KMEMNAMES)
560 		return (ENOTDIR);		/* overloaded */
561 
562 	switch (name[0]) {
563 	case KERN_MALLOC_BUCKETS:
564 		/* Initialize the first time */
565 		if (buckstring_init == 0) {
566 			buckstring_init = 1;
567 			bzero(buckstring, sizeof(buckstring));
568 			for (siz = 0, i = MINBUCKET; i < MINBUCKET + 16; i++) {
569 				snprintf(buckstring + siz,
570 				    sizeof buckstring - siz,
571 				    "%d,", (u_int)(1<<i));
572 				siz += strlen(buckstring + siz);
573 			}
574 			/* Remove trailing comma */
575 			if (siz)
576 				buckstring[siz - 1] = '\0';
577 		}
578 		return (sysctl_rdstring(oldp, oldlenp, newp, buckstring));
579 
580 	case KERN_MALLOC_BUCKET:
581 		bcopy(&bucket[BUCKETINDX(name[1])], &kb, sizeof(kb));
582 		kb.kb_next = kb.kb_last = 0;
583 		return (sysctl_rdstruct(oldp, oldlenp, newp, &kb, sizeof(kb)));
584 	case KERN_MALLOC_KMEMSTATS:
585 #ifdef KMEMSTATS
586 		if ((name[1] < 0) || (name[1] >= M_LAST))
587 			return (EINVAL);
588 		return (sysctl_rdstruct(oldp, oldlenp, newp,
589 		    &kmemstats[name[1]], sizeof(struct kmemstats)));
590 #else
591 		return (EOPNOTSUPP);
592 #endif
593 	case KERN_MALLOC_KMEMNAMES:
594 #if defined(KMEMSTATS) || defined(DIAGNOSTIC) || defined(FFS_SOFTUPDATES)
595 		if (memall == NULL) {
596 			int totlen;
597 
598 			i = rw_enter(&sysctl_kmemlock, RW_WRITE|RW_INTR);
599 			if (i)
600 				return (i);
601 
602 			/* Figure out how large a buffer we need */
603 			for (totlen = 0, i = 0; i < M_LAST; i++) {
604 				if (memname[i])
605 					totlen += strlen(memname[i]);
606 				totlen++;
607 			}
608 			memall = malloc(totlen + M_LAST, M_SYSCTL,
609 			    M_WAITOK|M_ZERO);
610 			for (siz = 0, i = 0; i < M_LAST; i++) {
611 				snprintf(memall + siz,
612 				    totlen + M_LAST - siz,
613 				    "%s,", memname[i] ? memname[i] : "");
614 				siz += strlen(memall + siz);
615 			}
616 			/* Remove trailing comma */
617 			if (siz)
618 				memall[siz - 1] = '\0';
619 
620 			/* Now, convert all spaces to underscores */
621 			for (i = 0; i < totlen; i++)
622 				if (memall[i] == ' ')
623 					memall[i] = '_';
624 			rw_exit_write(&sysctl_kmemlock);
625 		}
626 		return (sysctl_rdstring(oldp, oldlenp, newp, memall));
627 #else
628 		return (EOPNOTSUPP);
629 #endif
630 	default:
631 		return (EOPNOTSUPP);
632 	}
633 	/* NOTREACHED */
634 }
635 
636 /*
637  * Round up a size to how much malloc would actually allocate.
638  */
639 size_t
640 malloc_roundup(size_t sz)
641 {
642 	if (sz > MAXALLOCSAVE)
643 		return round_page(sz);
644 
645 	return (1 << BUCKETINDX(sz));
646 }
647 
648 #if defined(DDB)
649 #include <machine/db_machdep.h>
650 #include <ddb/db_interface.h>
651 #include <ddb/db_output.h>
652 
653 void
654 malloc_printit(int (*pr)(const char *, ...))
655 {
656 #ifdef KMEMSTATS
657 	struct kmemstats *km;
658 	int i;
659 
660 	(*pr)("%15s %5s  %6s  %7s  %6s %9s %8s %8s\n",
661 	    "Type", "InUse", "MemUse", "HighUse", "Limit", "Requests",
662 	    "Type Lim", "Kern Lim");
663 	for (i = 0, km = kmemstats; i < M_LAST; i++, km++) {
664 		if (!km->ks_calls || !memname[i])
665 			continue;
666 
667 		(*pr)("%15s %5ld %6ldK %7ldK %6ldK %9ld %8d %8d\n",
668 		    memname[i], km->ks_inuse, km->ks_memuse / 1024,
669 		    km->ks_maxused / 1024, km->ks_limit / 1024,
670 		    km->ks_calls, km->ks_limblocks, km->ks_mapblocks);
671 	}
672 #else
673 	(*pr)("No KMEMSTATS compiled in\n");
674 #endif
675 }
676 #endif /* DDB */
677