xref: /freebsd/sys/kern/kern_malloc.c (revision d2387d42b8da231a5b95cbc313825fb2aadf26f6)
1 /*
2  * Copyright (c) 1987, 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)kern_malloc.c	8.3 (Berkeley) 1/4/94
34  */
35 
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38 
39 #include "opt_vm.h"
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/lock.h>
45 #include <sys/malloc.h>
46 #include <sys/mbuf.h>
47 #include <sys/mutex.h>
48 #include <sys/vmmeter.h>
49 #include <sys/proc.h>
50 #include <sys/sysctl.h>
51 #include <sys/time.h>
52 
53 #include <vm/vm.h>
54 #include <vm/pmap.h>
55 #include <vm/vm_param.h>
56 #include <vm/vm_kern.h>
57 #include <vm/vm_extern.h>
58 #include <vm/vm_map.h>
59 #include <vm/vm_page.h>
60 #include <vm/uma.h>
61 #include <vm/uma_int.h>
62 #include <vm/uma_dbg.h>
63 
64 #if defined(INVARIANTS) && defined(__i386__)
65 #include <machine/cpu.h>
66 #endif
67 
68 /*
69  * When realloc() is called, if the new size is sufficiently smaller than
70  * the old size, realloc() will allocate a new, smaller block to avoid
71  * wasting memory. 'Sufficiently smaller' is defined as: newsize <=
72  * oldsize / 2^n, where REALLOC_FRACTION defines the value of 'n'.
73  */
74 #ifndef REALLOC_FRACTION
75 #define	REALLOC_FRACTION	1	/* new block if <= half the size */
76 #endif
77 
78 MALLOC_DEFINE(M_CACHE, "cache", "Various Dynamically allocated caches");
79 MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory");
80 MALLOC_DEFINE(M_TEMP, "temp", "misc temporary data buffers");
81 
82 MALLOC_DEFINE(M_IP6OPT, "ip6opt", "IPv6 options");
83 MALLOC_DEFINE(M_IP6NDP, "ip6ndp", "IPv6 Neighbor Discovery");
84 
85 static void kmeminit(void *);
86 SYSINIT(kmem, SI_SUB_KMEM, SI_ORDER_FIRST, kmeminit, NULL)
87 
88 static MALLOC_DEFINE(M_FREE, "free", "should be on free list");
89 
90 static struct malloc_type *kmemstatistics;
91 static char *kmembase;
92 static char *kmemlimit;
93 
94 #define KMEM_ZSHIFT	4
95 #define KMEM_ZBASE	16
96 #define KMEM_ZMASK	(KMEM_ZBASE - 1)
97 
98 #define KMEM_ZMAX	PAGE_SIZE
99 #define KMEM_ZSIZE	(KMEM_ZMAX >> KMEM_ZSHIFT)
100 static u_int8_t kmemsize[KMEM_ZSIZE + 1];
101 
102 /* These won't be powers of two for long */
103 struct {
104 	int kz_size;
105 	char *kz_name;
106 	uma_zone_t kz_zone;
107 } kmemzones[] = {
108 	{16, "16", NULL},
109 	{32, "32", NULL},
110 	{64, "64", NULL},
111 	{128, "128", NULL},
112 	{256, "256", NULL},
113 	{512, "512", NULL},
114 	{1024, "1024", NULL},
115 	{2048, "2048", NULL},
116 	{4096, "4096", NULL},
117 #if PAGE_SIZE > 4096
118 	{8192, "8192", NULL},
119 #if PAGE_SIZE > 8192
120 	{16384, "16384", NULL},
121 #if PAGE_SIZE > 16384
122 	{32768, "32768", NULL},
123 #if PAGE_SIZE > 32768
124 	{65536, "65536", NULL},
125 #if PAGE_SIZE > 65536
126 #error	"Unsupported PAGE_SIZE"
127 #endif	/* 65536 */
128 #endif	/* 32768 */
129 #endif	/* 16384 */
130 #endif	/* 8192 */
131 #endif	/* 4096 */
132 	{0, NULL},
133 };
134 
135 u_int vm_kmem_size;
136 SYSCTL_UINT(_vm, OID_AUTO, kmem_size, CTLFLAG_RD, &vm_kmem_size, 0,
137     "Size of kernel memory");
138 
139 /*
140  * The malloc_mtx protects the kmemstatistics linked list.
141  */
142 
143 struct mtx malloc_mtx;
144 
145 #ifdef MALLOC_PROFILE
146 uint64_t krequests[KMEM_ZSIZE + 1];
147 
148 static int sysctl_kern_mprof(SYSCTL_HANDLER_ARGS);
149 #endif
150 
151 static int sysctl_kern_malloc(SYSCTL_HANDLER_ARGS);
152 
153 /* time_uptime of last malloc(9) failure */
154 static time_t t_malloc_fail;
155 
156 #ifdef MALLOC_MAKE_FAILURES
157 /*
158  * Causes malloc failures every (n) mallocs with M_NOWAIT.  If set to 0,
159  * doesn't cause failures.
160  */
161 SYSCTL_NODE(_debug, OID_AUTO, malloc, CTLFLAG_RD, 0,
162     "Kernel malloc debugging options");
163 
164 static int malloc_failure_rate;
165 static int malloc_nowait_count;
166 static int malloc_failure_count;
167 SYSCTL_INT(_debug_malloc, OID_AUTO, failure_rate, CTLFLAG_RW,
168     &malloc_failure_rate, 0, "Every (n) mallocs with M_NOWAIT will fail");
169 TUNABLE_INT("debug.malloc.failure_rate", &malloc_failure_rate);
170 SYSCTL_INT(_debug_malloc, OID_AUTO, failure_count, CTLFLAG_RD,
171     &malloc_failure_count, 0, "Number of imposed M_NOWAIT malloc failures");
172 #endif
173 
174 int
175 malloc_last_fail(void)
176 {
177 
178 	return (time_uptime - t_malloc_fail);
179 }
180 
181 /*
182  *	malloc:
183  *
184  *	Allocate a block of memory.
185  *
186  *	If M_NOWAIT is set, this routine will not block and return NULL if
187  *	the allocation fails.
188  */
189 void *
190 malloc(size, type, flags)
191 	unsigned long size;
192 	struct malloc_type *type;
193 	int flags;
194 {
195 	int indx;
196 	caddr_t va;
197 	uma_zone_t zone;
198 #ifdef DIAGNOSTIC
199 	unsigned long osize = size;
200 #endif
201 	register struct malloc_type *ksp = type;
202 
203 #ifdef INVARIANTS
204 	/*
205 	 * To make sure that WAITOK or NOWAIT is set, but not more than
206 	 * one, and check against the API botches that are common.
207 	 */
208 	indx = flags & (M_WAITOK | M_NOWAIT | M_DONTWAIT | M_TRYWAIT);
209 	if (indx != M_NOWAIT && indx != M_WAITOK) {
210 		static	struct timeval lasterr;
211 		static	int curerr, once;
212 		if (once == 0 && ppsratecheck(&lasterr, &curerr, 1)) {
213 			printf("Bad malloc flags: %x\n", indx);
214 			backtrace();
215 			flags |= M_WAITOK;
216 			once++;
217 		}
218 	}
219 #endif
220 #if 0
221 	if (size == 0)
222 		Debugger("zero size malloc");
223 #endif
224 #ifdef MALLOC_MAKE_FAILURES
225 	if ((flags & M_NOWAIT) && (malloc_failure_rate != 0)) {
226 		atomic_add_int(&malloc_nowait_count, 1);
227 		if ((malloc_nowait_count % malloc_failure_rate) == 0) {
228 			atomic_add_int(&malloc_failure_count, 1);
229 			t_malloc_fail = time_uptime;
230 			return (NULL);
231 		}
232 	}
233 #endif
234 	if (flags & M_WAITOK)
235 		KASSERT(curthread->td_intr_nesting_level == 0,
236 		   ("malloc(M_WAITOK) in interrupt context"));
237 	if (size <= KMEM_ZMAX) {
238 		if (size & KMEM_ZMASK)
239 			size = (size & ~KMEM_ZMASK) + KMEM_ZBASE;
240 		indx = kmemsize[size >> KMEM_ZSHIFT];
241 		zone = kmemzones[indx].kz_zone;
242 #ifdef MALLOC_PROFILE
243 		krequests[size >> KMEM_ZSHIFT]++;
244 #endif
245 		va = uma_zalloc(zone, flags);
246 		mtx_lock(&ksp->ks_mtx);
247 		if (va == NULL)
248 			goto out;
249 
250 		ksp->ks_size |= 1 << indx;
251 		size = zone->uz_size;
252 	} else {
253 		size = roundup(size, PAGE_SIZE);
254 		zone = NULL;
255 		va = uma_large_malloc(size, flags);
256 		mtx_lock(&ksp->ks_mtx);
257 		if (va == NULL)
258 			goto out;
259 	}
260 	ksp->ks_memuse += size;
261 	ksp->ks_inuse++;
262 out:
263 	ksp->ks_calls++;
264 	if (ksp->ks_memuse > ksp->ks_maxused)
265 		ksp->ks_maxused = ksp->ks_memuse;
266 
267 	mtx_unlock(&ksp->ks_mtx);
268 	if (flags & M_WAITOK)
269 		KASSERT(va != NULL, ("malloc(M_WAITOK) returned NULL"));
270 	else if (va == NULL)
271 		t_malloc_fail = time_uptime;
272 #ifdef DIAGNOSTIC
273 	if (va != NULL && !(flags & M_ZERO)) {
274 		memset(va, 0x70, osize);
275 	}
276 #endif
277 	return ((void *) va);
278 }
279 
280 /*
281  *	free:
282  *
283  *	Free a block of memory allocated by malloc.
284  *
285  *	This routine may not block.
286  */
287 void
288 free(addr, type)
289 	void *addr;
290 	struct malloc_type *type;
291 {
292 	register struct malloc_type *ksp = type;
293 	uma_slab_t slab;
294 	u_long size;
295 
296 	/* free(NULL, ...) does nothing */
297 	if (addr == NULL)
298 		return;
299 
300 	KASSERT(ksp->ks_memuse > 0,
301 		("malloc(9)/free(9) confusion.\n%s",
302 		 "Probably freeing with wrong type, but maybe not here."));
303 	size = 0;
304 
305 	slab = vtoslab((vm_offset_t)addr & (~UMA_SLAB_MASK));
306 
307 	if (slab == NULL)
308 		panic("free: address %p(%p) has not been allocated.\n",
309 		    addr, (void *)((u_long)addr & (~UMA_SLAB_MASK)));
310 
311 
312 	if (!(slab->us_flags & UMA_SLAB_MALLOC)) {
313 #ifdef INVARIANTS
314 		struct malloc_type **mtp = addr;
315 #endif
316 		size = slab->us_zone->uz_size;
317 #ifdef INVARIANTS
318 		/*
319 		 * Cache a pointer to the malloc_type that most recently freed
320 		 * this memory here.  This way we know who is most likely to
321 		 * have stepped on it later.
322 		 *
323 		 * This code assumes that size is a multiple of 8 bytes for
324 		 * 64 bit machines
325 		 */
326 		mtp = (struct malloc_type **)
327 		    ((unsigned long)mtp & ~UMA_ALIGN_PTR);
328 		mtp += (size - sizeof(struct malloc_type *)) /
329 		    sizeof(struct malloc_type *);
330 		*mtp = type;
331 #endif
332 		uma_zfree_arg(slab->us_zone, addr, slab);
333 	} else {
334 		size = slab->us_size;
335 		uma_large_free(slab);
336 	}
337 	mtx_lock(&ksp->ks_mtx);
338 	KASSERT(size <= ksp->ks_memuse,
339 		("malloc(9)/free(9) confusion.\n%s",
340 		 "Probably freeing with wrong type, but maybe not here."));
341 	ksp->ks_memuse -= size;
342 	ksp->ks_inuse--;
343 	mtx_unlock(&ksp->ks_mtx);
344 }
345 
346 /*
347  *	realloc: change the size of a memory block
348  */
349 void *
350 realloc(addr, size, type, flags)
351 	void *addr;
352 	unsigned long size;
353 	struct malloc_type *type;
354 	int flags;
355 {
356 	uma_slab_t slab;
357 	unsigned long alloc;
358 	void *newaddr;
359 
360 	/* realloc(NULL, ...) is equivalent to malloc(...) */
361 	if (addr == NULL)
362 		return (malloc(size, type, flags));
363 
364 	slab = vtoslab((vm_offset_t)addr & ~(UMA_SLAB_MASK));
365 
366 	/* Sanity check */
367 	KASSERT(slab != NULL,
368 	    ("realloc: address %p out of range", (void *)addr));
369 
370 	/* Get the size of the original block */
371 	if (slab->us_zone)
372 		alloc = slab->us_zone->uz_size;
373 	else
374 		alloc = slab->us_size;
375 
376 	/* Reuse the original block if appropriate */
377 	if (size <= alloc
378 	    && (size > (alloc >> REALLOC_FRACTION) || alloc == MINALLOCSIZE))
379 		return (addr);
380 
381 	/* Allocate a new, bigger (or smaller) block */
382 	if ((newaddr = malloc(size, type, flags)) == NULL)
383 		return (NULL);
384 
385 	/* Copy over original contents */
386 	bcopy(addr, newaddr, min(size, alloc));
387 	free(addr, type);
388 	return (newaddr);
389 }
390 
391 /*
392  *	reallocf: same as realloc() but free memory on failure.
393  */
394 void *
395 reallocf(addr, size, type, flags)
396 	void *addr;
397 	unsigned long size;
398 	struct malloc_type *type;
399 	int flags;
400 {
401 	void *mem;
402 
403 	if ((mem = realloc(addr, size, type, flags)) == NULL)
404 		free(addr, type);
405 	return (mem);
406 }
407 
408 /*
409  * Initialize the kernel memory allocator
410  */
411 /* ARGSUSED*/
412 static void
413 kmeminit(dummy)
414 	void *dummy;
415 {
416 	u_int8_t indx;
417 	u_long npg;
418 	u_long mem_size;
419 	int i;
420 
421 	mtx_init(&malloc_mtx, "malloc", NULL, MTX_DEF);
422 
423 	/*
424 	 * Try to auto-tune the kernel memory size, so that it is
425 	 * more applicable for a wider range of machine sizes.
426 	 * On an X86, a VM_KMEM_SIZE_SCALE value of 4 is good, while
427 	 * a VM_KMEM_SIZE of 12MB is a fair compromise.  The
428 	 * VM_KMEM_SIZE_MAX is dependent on the maximum KVA space
429 	 * available, and on an X86 with a total KVA space of 256MB,
430 	 * try to keep VM_KMEM_SIZE_MAX at 80MB or below.
431 	 *
432 	 * Note that the kmem_map is also used by the zone allocator,
433 	 * so make sure that there is enough space.
434 	 */
435 	vm_kmem_size = VM_KMEM_SIZE;
436 	mem_size = cnt.v_page_count;
437 
438 #if defined(VM_KMEM_SIZE_SCALE)
439 	if ((mem_size / VM_KMEM_SIZE_SCALE) > (vm_kmem_size / PAGE_SIZE))
440 		vm_kmem_size = (mem_size / VM_KMEM_SIZE_SCALE) * PAGE_SIZE;
441 #endif
442 
443 #if defined(VM_KMEM_SIZE_MAX)
444 	if (vm_kmem_size >= VM_KMEM_SIZE_MAX)
445 		vm_kmem_size = VM_KMEM_SIZE_MAX;
446 #endif
447 
448 	/* Allow final override from the kernel environment */
449 #ifndef BURN_BRIDGES
450 	if (TUNABLE_INT_FETCH("kern.vm.kmem.size", &vm_kmem_size) != 0)
451 		printf("kern.vm.kmem.size is now called vm.kmem_size!\n");
452 #endif
453 	TUNABLE_INT_FETCH("vm.kmem_size", &vm_kmem_size);
454 
455 	/*
456 	 * Limit kmem virtual size to twice the physical memory.
457 	 * This allows for kmem map sparseness, but limits the size
458 	 * to something sane. Be careful to not overflow the 32bit
459 	 * ints while doing the check.
460 	 */
461 	if (((vm_kmem_size / 2) / PAGE_SIZE) > cnt.v_page_count)
462 		vm_kmem_size = 2 * cnt.v_page_count * PAGE_SIZE;
463 
464 	/*
465 	 * Tune settings based on the kernel map's size at this time.
466 	 */
467 	init_param3(vm_kmem_size / PAGE_SIZE);
468 
469 	/*
470 	 * In mbuf_init(), we set up submaps for mbufs and clusters, in which
471 	 * case we rounddown() (nmbufs * MSIZE) and (nmbclusters * MCLBYTES),
472 	 * respectively. Mathematically, this means that what we do here may
473 	 * amount to slightly more address space than we need for the submaps,
474 	 * but it never hurts to have an extra page in kmem_map.
475 	 */
476 	npg = (nmbufs*MSIZE + nmbclusters*MCLBYTES + vm_kmem_size) / PAGE_SIZE;
477 
478 	kmem_map = kmem_suballoc(kernel_map, (vm_offset_t *)&kmembase,
479 		(vm_offset_t *)&kmemlimit, (vm_size_t)(npg * PAGE_SIZE));
480 	kmem_map->system_map = 1;
481 
482 	uma_startup2();
483 
484 	for (i = 0, indx = 0; kmemzones[indx].kz_size != 0; indx++) {
485 		int size = kmemzones[indx].kz_size;
486 		char *name = kmemzones[indx].kz_name;
487 
488 		kmemzones[indx].kz_zone = uma_zcreate(name, size,
489 #ifdef INVARIANTS
490 		    mtrash_ctor, mtrash_dtor, mtrash_init, mtrash_fini,
491 #else
492 		    NULL, NULL, NULL, NULL,
493 #endif
494 		    UMA_ALIGN_PTR, UMA_ZONE_MALLOC);
495 
496 		for (;i <= size; i+= KMEM_ZBASE)
497 			kmemsize[i >> KMEM_ZSHIFT] = indx;
498 
499 	}
500 }
501 
502 void
503 malloc_init(data)
504 	void *data;
505 {
506 	struct malloc_type *type = (struct malloc_type *)data;
507 
508 	mtx_lock(&malloc_mtx);
509 	if (type->ks_magic != M_MAGIC)
510 		panic("malloc type lacks magic");
511 
512 	if (cnt.v_page_count == 0)
513 		panic("malloc_init not allowed before vm init");
514 
515 	if (type->ks_next != NULL)
516 		return;
517 
518 	type->ks_next = kmemstatistics;
519 	kmemstatistics = type;
520 	mtx_init(&type->ks_mtx, type->ks_shortdesc, "Malloc Stats", MTX_DEF);
521 	mtx_unlock(&malloc_mtx);
522 }
523 
524 void
525 malloc_uninit(data)
526 	void *data;
527 {
528 	struct malloc_type *type = (struct malloc_type *)data;
529 	struct malloc_type *t;
530 
531 	mtx_lock(&malloc_mtx);
532 	mtx_lock(&type->ks_mtx);
533 	if (type->ks_magic != M_MAGIC)
534 		panic("malloc type lacks magic");
535 
536 	if (cnt.v_page_count == 0)
537 		panic("malloc_uninit not allowed before vm init");
538 
539 	if (type == kmemstatistics)
540 		kmemstatistics = type->ks_next;
541 	else {
542 		for (t = kmemstatistics; t->ks_next != NULL; t = t->ks_next) {
543 			if (t->ks_next == type) {
544 				t->ks_next = type->ks_next;
545 				break;
546 			}
547 		}
548 	}
549 	type->ks_next = NULL;
550 	mtx_destroy(&type->ks_mtx);
551 	mtx_unlock(&malloc_mtx);
552 }
553 
554 static int
555 sysctl_kern_malloc(SYSCTL_HANDLER_ARGS)
556 {
557 	struct malloc_type *type;
558 	int linesize = 128;
559 	int curline;
560 	int bufsize;
561 	int first;
562 	int error;
563 	char *buf;
564 	char *p;
565 	int cnt;
566 	int len;
567 	int i;
568 
569 	cnt = 0;
570 
571 	mtx_lock(&malloc_mtx);
572 	for (type = kmemstatistics; type != NULL; type = type->ks_next)
573 		cnt++;
574 
575 	mtx_unlock(&malloc_mtx);
576 	bufsize = linesize * (cnt + 1);
577 	p = buf = (char *)malloc(bufsize, M_TEMP, M_WAITOK|M_ZERO);
578 	mtx_lock(&malloc_mtx);
579 
580 	len = snprintf(p, linesize,
581 	    "\n        Type  InUse MemUse HighUse Requests  Size(s)\n");
582 	p += len;
583 
584 	for (type = kmemstatistics; cnt != 0 && type != NULL;
585 	    type = type->ks_next, cnt--) {
586 		if (type->ks_calls == 0)
587 			continue;
588 
589 		curline = linesize - 2;	/* Leave room for the \n */
590 		len = snprintf(p, curline, "%13s%6lu%6luK%7luK%9llu",
591 			type->ks_shortdesc,
592 			type->ks_inuse,
593 			(type->ks_memuse + 1023) / 1024,
594 			(type->ks_maxused + 1023) / 1024,
595 			(long long unsigned)type->ks_calls);
596 		curline -= len;
597 		p += len;
598 
599 		first = 1;
600 		for (i = 0; i < sizeof(kmemzones) / sizeof(kmemzones[0]) - 1;
601 		    i++) {
602 			if (type->ks_size & (1 << i)) {
603 				if (first)
604 					len = snprintf(p, curline, "  ");
605 				else
606 					len = snprintf(p, curline, ",");
607 				curline -= len;
608 				p += len;
609 
610 				len = snprintf(p, curline,
611 				    "%s", kmemzones[i].kz_name);
612 				curline -= len;
613 				p += len;
614 
615 				first = 0;
616 			}
617 		}
618 
619 		len = snprintf(p, 2, "\n");
620 		p += len;
621 	}
622 
623 	mtx_unlock(&malloc_mtx);
624 	error = SYSCTL_OUT(req, buf, p - buf);
625 
626 	free(buf, M_TEMP);
627 	return (error);
628 }
629 
630 SYSCTL_OID(_kern, OID_AUTO, malloc, CTLTYPE_STRING|CTLFLAG_RD,
631     NULL, 0, sysctl_kern_malloc, "A", "Malloc Stats");
632 
633 #ifdef MALLOC_PROFILE
634 
635 static int
636 sysctl_kern_mprof(SYSCTL_HANDLER_ARGS)
637 {
638 	int linesize = 64;
639 	uint64_t count;
640 	uint64_t waste;
641 	uint64_t mem;
642 	int bufsize;
643 	int error;
644 	char *buf;
645 	int rsize;
646 	int size;
647 	char *p;
648 	int len;
649 	int i;
650 
651 	bufsize = linesize * (KMEM_ZSIZE + 1);
652 	bufsize += 128; 	/* For the stats line */
653 	bufsize += 128; 	/* For the banner line */
654 	waste = 0;
655 	mem = 0;
656 
657 	p = buf = (char *)malloc(bufsize, M_TEMP, M_WAITOK|M_ZERO);
658 	len = snprintf(p, bufsize,
659 	    "\n  Size                    Requests  Real Size\n");
660 	bufsize -= len;
661 	p += len;
662 
663 	for (i = 0; i < KMEM_ZSIZE; i++) {
664 		size = i << KMEM_ZSHIFT;
665 		rsize = kmemzones[kmemsize[i]].kz_size;
666 		count = (long long unsigned)krequests[i];
667 
668 		len = snprintf(p, bufsize, "%6d%28llu%11d\n",
669 		    size, (unsigned long long)count, rsize);
670 		bufsize -= len;
671 		p += len;
672 
673 		if ((rsize * count) > (size * count))
674 			waste += (rsize * count) - (size * count);
675 		mem += (rsize * count);
676 	}
677 
678 	len = snprintf(p, bufsize,
679 	    "\nTotal memory used:\t%30llu\nTotal Memory wasted:\t%30llu\n",
680 	    (unsigned long long)mem, (unsigned long long)waste);
681 	p += len;
682 
683 	error = SYSCTL_OUT(req, buf, p - buf);
684 
685 	free(buf, M_TEMP);
686 	return (error);
687 }
688 
689 SYSCTL_OID(_kern, OID_AUTO, mprof, CTLTYPE_STRING|CTLFLAG_RD,
690     NULL, 0, sysctl_kern_mprof, "A", "Malloc Profiling");
691 #endif /* MALLOC_PROFILE */
692