xref: /freebsd/sys/kern/kern_malloc.c (revision 847a2a17167996abbc61d81db3de86b92cab3fde)
19454b2d8SWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1987, 1991, 1993
363a7e0a3SRobert Watson  *	The Regents of the University of California.
463a7e0a3SRobert Watson  * Copyright (c) 2005 Robert N. M. Watson
563a7e0a3SRobert Watson  * All rights reserved.
6df8bae1dSRodney W. Grimes  *
7df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
8df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
9df8bae1dSRodney W. Grimes  * are met:
10df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
12df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
13df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
14df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
16df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
17df8bae1dSRodney W. Grimes  *    without specific prior written permission.
18df8bae1dSRodney W. Grimes  *
19df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
30df8bae1dSRodney W. Grimes  *
31df8bae1dSRodney W. Grimes  *	@(#)kern_malloc.c	8.3 (Berkeley) 1/4/94
32df8bae1dSRodney W. Grimes  */
33df8bae1dSRodney W. Grimes 
34677b542eSDavid E. O'Brien #include <sys/cdefs.h>
35677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
36677b542eSDavid E. O'Brien 
37909ed16cSRobert Watson #include "opt_ddb.h"
388a58a9f6SJohn Dyson #include "opt_vm.h"
398a58a9f6SJohn Dyson 
40df8bae1dSRodney W. Grimes #include <sys/param.h>
4126f9a767SRodney W. Grimes #include <sys/systm.h>
422d50560aSMarcel Moolenaar #include <sys/kdb.h>
43df8bae1dSRodney W. Grimes #include <sys/kernel.h>
44fb919e4dSMark Murray #include <sys/lock.h>
45df8bae1dSRodney W. Grimes #include <sys/malloc.h>
4654e7152cSDavid Greenman #include <sys/mbuf.h>
47eec258d2SJohn Baldwin #include <sys/mutex.h>
48efeaf95aSDavid Greenman #include <sys/vmmeter.h>
49a448b62aSJake Burkholder #include <sys/proc.h>
5063a7e0a3SRobert Watson #include <sys/sbuf.h>
516f267175SJeff Roberson #include <sys/sysctl.h>
521fb14a47SPoul-Henning Kamp #include <sys/time.h>
539a02e8c6SJason Evans 
54df8bae1dSRodney W. Grimes #include <vm/vm.h>
5599571dc3SJeff Roberson #include <vm/pmap.h>
56efeaf95aSDavid Greenman #include <vm/vm_param.h>
57df8bae1dSRodney W. Grimes #include <vm/vm_kern.h>
58efeaf95aSDavid Greenman #include <vm/vm_extern.h>
593075778bSJohn Dyson #include <vm/vm_map.h>
6099571dc3SJeff Roberson #include <vm/vm_page.h>
618355f576SJeff Roberson #include <vm/uma.h>
628355f576SJeff Roberson #include <vm/uma_int.h>
638efc4effSJeff Roberson #include <vm/uma_dbg.h>
64df8bae1dSRodney W. Grimes 
65e4eb384bSBosko Milekic #ifdef DEBUG_MEMGUARD
66e4eb384bSBosko Milekic #include <vm/memguard.h>
67e4eb384bSBosko Milekic #endif
68847a2a17SPawel Jakub Dawidek #ifdef DEBUG_REDZONE
69847a2a17SPawel Jakub Dawidek #include <vm/redzone.h>
70847a2a17SPawel Jakub Dawidek #endif
71e4eb384bSBosko Milekic 
72984982d6SPoul-Henning Kamp #if defined(INVARIANTS) && defined(__i386__)
73984982d6SPoul-Henning Kamp #include <machine/cpu.h>
74984982d6SPoul-Henning Kamp #endif
75984982d6SPoul-Henning Kamp 
76909ed16cSRobert Watson #include <ddb/ddb.h>
77909ed16cSRobert Watson 
7844a8ff31SArchie Cobbs /*
7944a8ff31SArchie Cobbs  * When realloc() is called, if the new size is sufficiently smaller than
8044a8ff31SArchie Cobbs  * the old size, realloc() will allocate a new, smaller block to avoid
8144a8ff31SArchie Cobbs  * wasting memory. 'Sufficiently smaller' is defined as: newsize <=
8244a8ff31SArchie Cobbs  * oldsize / 2^n, where REALLOC_FRACTION defines the value of 'n'.
8344a8ff31SArchie Cobbs  */
8444a8ff31SArchie Cobbs #ifndef REALLOC_FRACTION
8544a8ff31SArchie Cobbs #define	REALLOC_FRACTION	1	/* new block if <= half the size */
8644a8ff31SArchie Cobbs #endif
8744a8ff31SArchie Cobbs 
883b6fb885SPoul-Henning Kamp MALLOC_DEFINE(M_CACHE, "cache", "Various Dynamically allocated caches");
899ef246c6SBruce Evans MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory");
909ef246c6SBruce Evans MALLOC_DEFINE(M_TEMP, "temp", "misc temporary data buffers");
919ef246c6SBruce Evans 
9282cd038dSYoshinobu Inoue MALLOC_DEFINE(M_IP6OPT, "ip6opt", "IPv6 options");
9382cd038dSYoshinobu Inoue MALLOC_DEFINE(M_IP6NDP, "ip6ndp", "IPv6 Neighbor Discovery");
9482cd038dSYoshinobu Inoue 
954d77a549SAlfred Perlstein static void kmeminit(void *);
962b14f991SJulian Elischer SYSINIT(kmem, SI_SUB_KMEM, SI_ORDER_FIRST, kmeminit, NULL)
972b14f991SJulian Elischer 
98a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_FREE, "free", "should be on free list");
99a1c995b6SPoul-Henning Kamp 
100db669378SPeter Wemm static struct malloc_type *kmemstatistics;
101254c6cb3SPoul-Henning Kamp static char *kmembase;
102043a2f3bSBruce Evans static char *kmemlimit;
103cd814b26SRobert Watson static int kmemcount;
1041f6889a1SMatthew Dillon 
1058355f576SJeff Roberson #define KMEM_ZSHIFT	4
1068355f576SJeff Roberson #define KMEM_ZBASE	16
1078355f576SJeff Roberson #define KMEM_ZMASK	(KMEM_ZBASE - 1)
1088355f576SJeff Roberson 
1099fb535deSJeff Roberson #define KMEM_ZMAX	PAGE_SIZE
1108355f576SJeff Roberson #define KMEM_ZSIZE	(KMEM_ZMAX >> KMEM_ZSHIFT)
1116f267175SJeff Roberson static u_int8_t kmemsize[KMEM_ZSIZE + 1];
1126f267175SJeff Roberson 
1138355f576SJeff Roberson /* These won't be powers of two for long */
1148355f576SJeff Roberson struct {
1156f267175SJeff Roberson 	int kz_size;
1166f267175SJeff Roberson 	char *kz_name;
1176f267175SJeff Roberson 	uma_zone_t kz_zone;
1186f267175SJeff Roberson } kmemzones[] = {
1196f267175SJeff Roberson 	{16, "16", NULL},
1206f267175SJeff Roberson 	{32, "32", NULL},
1216f267175SJeff Roberson 	{64, "64", NULL},
1226f267175SJeff Roberson 	{128, "128", NULL},
1236f267175SJeff Roberson 	{256, "256", NULL},
1246f267175SJeff Roberson 	{512, "512", NULL},
1256f267175SJeff Roberson 	{1024, "1024", NULL},
1266f267175SJeff Roberson 	{2048, "2048", NULL},
1276f267175SJeff Roberson 	{4096, "4096", NULL},
1289fb535deSJeff Roberson #if PAGE_SIZE > 4096
1296f267175SJeff Roberson 	{8192, "8192", NULL},
1309fb535deSJeff Roberson #if PAGE_SIZE > 8192
13143a7c4e9SRobert Watson 	{16384, "16384", NULL},
1329fb535deSJeff Roberson #if PAGE_SIZE > 16384
133bd796eb2SRobert Watson 	{32768, "32768", NULL},
1349fb535deSJeff Roberson #if PAGE_SIZE > 32768
135bd796eb2SRobert Watson 	{65536, "65536", NULL},
1369fb535deSJeff Roberson #if PAGE_SIZE > 65536
1379fb535deSJeff Roberson #error	"Unsupported PAGE_SIZE"
1389fb535deSJeff Roberson #endif	/* 65536 */
1399fb535deSJeff Roberson #endif	/* 32768 */
1409fb535deSJeff Roberson #endif	/* 16384 */
1419fb535deSJeff Roberson #endif	/* 8192 */
1429fb535deSJeff Roberson #endif	/* 4096 */
1438355f576SJeff Roberson 	{0, NULL},
1448355f576SJeff Roberson };
1458355f576SJeff Roberson 
14663a7e0a3SRobert Watson static uma_zone_t mt_zone;
14763a7e0a3SRobert Watson 
1486f267175SJeff Roberson u_int vm_kmem_size;
14984344f9fSDag-Erling Smørgrav SYSCTL_UINT(_vm, OID_AUTO, kmem_size, CTLFLAG_RD, &vm_kmem_size, 0,
15084344f9fSDag-Erling Smørgrav     "Size of kernel memory");
1515a34a9f0SJeff Roberson 
152479439b4SDag-Erling Smørgrav u_int vm_kmem_size_max;
153479439b4SDag-Erling Smørgrav SYSCTL_UINT(_vm, OID_AUTO, kmem_size_max, CTLFLAG_RD, &vm_kmem_size_max, 0,
154479439b4SDag-Erling Smørgrav     "Maximum size of kernel memory");
155479439b4SDag-Erling Smørgrav 
156479439b4SDag-Erling Smørgrav u_int vm_kmem_size_scale;
157479439b4SDag-Erling Smørgrav SYSCTL_UINT(_vm, OID_AUTO, kmem_size_scale, CTLFLAG_RD, &vm_kmem_size_scale, 0,
158479439b4SDag-Erling Smørgrav     "Scale factor for kernel memory size");
159479439b4SDag-Erling Smørgrav 
1605a34a9f0SJeff Roberson /*
16199571dc3SJeff Roberson  * The malloc_mtx protects the kmemstatistics linked list.
1625a34a9f0SJeff Roberson  */
1635a34a9f0SJeff Roberson 
1645a34a9f0SJeff Roberson struct mtx malloc_mtx;
16569ef67f9SJason Evans 
1665e914b96SJeff Roberson #ifdef MALLOC_PROFILE
1675e914b96SJeff Roberson uint64_t krequests[KMEM_ZSIZE + 1];
1686f267175SJeff Roberson 
1695e914b96SJeff Roberson static int sysctl_kern_mprof(SYSCTL_HANDLER_ARGS);
1705e914b96SJeff Roberson #endif
1715e914b96SJeff Roberson 
1725e914b96SJeff Roberson static int sysctl_kern_malloc(SYSCTL_HANDLER_ARGS);
173cd814b26SRobert Watson static int sysctl_kern_malloc_stats(SYSCTL_HANDLER_ARGS);
174df8bae1dSRodney W. Grimes 
1751fb14a47SPoul-Henning Kamp /* time_uptime of last malloc(9) failure */
1761fb14a47SPoul-Henning Kamp static time_t t_malloc_fail;
1771fb14a47SPoul-Henning Kamp 
178eae870cdSRobert Watson #ifdef MALLOC_MAKE_FAILURES
179eae870cdSRobert Watson /*
180eae870cdSRobert Watson  * Causes malloc failures every (n) mallocs with M_NOWAIT.  If set to 0,
181eae870cdSRobert Watson  * doesn't cause failures.
182eae870cdSRobert Watson  */
183eae870cdSRobert Watson SYSCTL_NODE(_debug, OID_AUTO, malloc, CTLFLAG_RD, 0,
184eae870cdSRobert Watson     "Kernel malloc debugging options");
185eae870cdSRobert Watson 
186eae870cdSRobert Watson static int malloc_failure_rate;
187eae870cdSRobert Watson static int malloc_nowait_count;
188eae870cdSRobert Watson static int malloc_failure_count;
189eae870cdSRobert Watson SYSCTL_INT(_debug_malloc, OID_AUTO, failure_rate, CTLFLAG_RW,
190eae870cdSRobert Watson     &malloc_failure_rate, 0, "Every (n) mallocs with M_NOWAIT will fail");
191f2538508SRobert Watson TUNABLE_INT("debug.malloc.failure_rate", &malloc_failure_rate);
192eae870cdSRobert Watson SYSCTL_INT(_debug_malloc, OID_AUTO, failure_count, CTLFLAG_RD,
193eae870cdSRobert Watson     &malloc_failure_count, 0, "Number of imposed M_NOWAIT malloc failures");
194eae870cdSRobert Watson #endif
195eae870cdSRobert Watson 
1961fb14a47SPoul-Henning Kamp int
1971fb14a47SPoul-Henning Kamp malloc_last_fail(void)
1981fb14a47SPoul-Henning Kamp {
1991fb14a47SPoul-Henning Kamp 
2001fb14a47SPoul-Henning Kamp 	return (time_uptime - t_malloc_fail);
2011fb14a47SPoul-Henning Kamp }
2021fb14a47SPoul-Henning Kamp 
203df8bae1dSRodney W. Grimes /*
2044362fadaSBrian Feldman  * Add this to the informational malloc_type bucket.
2054362fadaSBrian Feldman  */
2064362fadaSBrian Feldman static void
20763a7e0a3SRobert Watson malloc_type_zone_allocated(struct malloc_type *mtp, unsigned long size,
2084362fadaSBrian Feldman     int zindx)
2094362fadaSBrian Feldman {
21063a7e0a3SRobert Watson 	struct malloc_type_internal *mtip;
21163a7e0a3SRobert Watson 	struct malloc_type_stats *mtsp;
21263a7e0a3SRobert Watson 
21363a7e0a3SRobert Watson 	critical_enter();
21463a7e0a3SRobert Watson 	mtip = mtp->ks_handle;
21563a7e0a3SRobert Watson 	mtsp = &mtip->mti_stats[curcpu];
21673864adbSPawel Jakub Dawidek 	if (size > 0) {
21763a7e0a3SRobert Watson 		mtsp->mts_memalloced += size;
21863a7e0a3SRobert Watson 		mtsp->mts_numallocs++;
21973864adbSPawel Jakub Dawidek 	}
2204362fadaSBrian Feldman 	if (zindx != -1)
22163a7e0a3SRobert Watson 		mtsp->mts_size |= 1 << zindx;
22263a7e0a3SRobert Watson 	critical_exit();
2234362fadaSBrian Feldman }
2244362fadaSBrian Feldman 
2254362fadaSBrian Feldman void
22663a7e0a3SRobert Watson malloc_type_allocated(struct malloc_type *mtp, unsigned long size)
2274362fadaSBrian Feldman {
22863a7e0a3SRobert Watson 
22973864adbSPawel Jakub Dawidek 	if (size > 0)
23063a7e0a3SRobert Watson 		malloc_type_zone_allocated(mtp, size, -1);
2314362fadaSBrian Feldman }
2324362fadaSBrian Feldman 
2334362fadaSBrian Feldman /*
2344362fadaSBrian Feldman  * Remove this allocation from the informational malloc_type bucket.
2354362fadaSBrian Feldman  */
2364362fadaSBrian Feldman void
23763a7e0a3SRobert Watson malloc_type_freed(struct malloc_type *mtp, unsigned long size)
2384362fadaSBrian Feldman {
23963a7e0a3SRobert Watson 	struct malloc_type_internal *mtip;
24063a7e0a3SRobert Watson 	struct malloc_type_stats *mtsp;
24163a7e0a3SRobert Watson 
24263a7e0a3SRobert Watson 	critical_enter();
24363a7e0a3SRobert Watson 	mtip = mtp->ks_handle;
24463a7e0a3SRobert Watson 	mtsp = &mtip->mti_stats[curcpu];
24563a7e0a3SRobert Watson 	mtsp->mts_memfreed += size;
24663a7e0a3SRobert Watson 	mtsp->mts_numfrees++;
24763a7e0a3SRobert Watson 	critical_exit();
2484362fadaSBrian Feldman }
2494362fadaSBrian Feldman 
2504362fadaSBrian Feldman /*
2511c7c3c6aSMatthew Dillon  *	malloc:
2521c7c3c6aSMatthew Dillon  *
2531c7c3c6aSMatthew Dillon  *	Allocate a block of memory.
2541c7c3c6aSMatthew Dillon  *
2551c7c3c6aSMatthew Dillon  *	If M_NOWAIT is set, this routine will not block and return NULL if
2561c7c3c6aSMatthew Dillon  *	the allocation fails.
257df8bae1dSRodney W. Grimes  */
258df8bae1dSRodney W. Grimes void *
25963a7e0a3SRobert Watson malloc(unsigned long size, struct malloc_type *mtp, int flags)
260df8bae1dSRodney W. Grimes {
2616f267175SJeff Roberson 	int indx;
2628355f576SJeff Roberson 	caddr_t va;
2638355f576SJeff Roberson 	uma_zone_t zone;
264099a0e58SBosko Milekic 	uma_keg_t keg;
265847a2a17SPawel Jakub Dawidek #if defined(DIAGNOSTIC) || defined(DEBUG_REDZONE)
2664db4f5c8SPoul-Henning Kamp 	unsigned long osize = size;
2674db4f5c8SPoul-Henning Kamp #endif
268df8bae1dSRodney W. Grimes 
269194a0abfSPoul-Henning Kamp #ifdef INVARIANTS
270d3c11994SPoul-Henning Kamp 	/*
27123198357SRuslan Ermilov 	 * Check that exactly one of M_WAITOK or M_NOWAIT is specified.
272d3c11994SPoul-Henning Kamp 	 */
27323198357SRuslan Ermilov 	indx = flags & (M_WAITOK | M_NOWAIT);
274d3c11994SPoul-Henning Kamp 	if (indx != M_NOWAIT && indx != M_WAITOK) {
275d3c11994SPoul-Henning Kamp 		static	struct timeval lasterr;
276d3c11994SPoul-Henning Kamp 		static	int curerr, once;
277d3c11994SPoul-Henning Kamp 		if (once == 0 && ppsratecheck(&lasterr, &curerr, 1)) {
278d3c11994SPoul-Henning Kamp 			printf("Bad malloc flags: %x\n", indx);
2792d50560aSMarcel Moolenaar 			kdb_backtrace();
280d3c11994SPoul-Henning Kamp 			flags |= M_WAITOK;
281d3c11994SPoul-Henning Kamp 			once++;
282d3c11994SPoul-Henning Kamp 		}
283d3c11994SPoul-Henning Kamp 	}
284194a0abfSPoul-Henning Kamp #endif
285708da94eSPoul-Henning Kamp #if 0
286708da94eSPoul-Henning Kamp 	if (size == 0)
2872d50560aSMarcel Moolenaar 		kdb_enter("zero size malloc");
288708da94eSPoul-Henning Kamp #endif
289eae870cdSRobert Watson #ifdef MALLOC_MAKE_FAILURES
290eae870cdSRobert Watson 	if ((flags & M_NOWAIT) && (malloc_failure_rate != 0)) {
291eae870cdSRobert Watson 		atomic_add_int(&malloc_nowait_count, 1);
292eae870cdSRobert Watson 		if ((malloc_nowait_count % malloc_failure_rate) == 0) {
293eae870cdSRobert Watson 			atomic_add_int(&malloc_failure_count, 1);
2943f6ee876SPoul-Henning Kamp 			t_malloc_fail = time_uptime;
295eae870cdSRobert Watson 			return (NULL);
296eae870cdSRobert Watson 		}
297eae870cdSRobert Watson 	}
298eae870cdSRobert Watson #endif
299d3c11994SPoul-Henning Kamp 	if (flags & M_WAITOK)
300b40ce416SJulian Elischer 		KASSERT(curthread->td_intr_nesting_level == 0,
301a163d034SWarner Losh 		   ("malloc(M_WAITOK) in interrupt context"));
302e4eb384bSBosko Milekic 
303e4eb384bSBosko Milekic #ifdef DEBUG_MEMGUARD
304d362c40dSPawel Jakub Dawidek 	if (memguard_cmp(mtp))
305e4eb384bSBosko Milekic 		return memguard_alloc(size, flags);
306e4eb384bSBosko Milekic #endif
307e4eb384bSBosko Milekic 
308847a2a17SPawel Jakub Dawidek #ifdef DEBUG_REDZONE
309847a2a17SPawel Jakub Dawidek 	size = redzone_size_ntor(size);
310847a2a17SPawel Jakub Dawidek #endif
311847a2a17SPawel Jakub Dawidek 
3128355f576SJeff Roberson 	if (size <= KMEM_ZMAX) {
3136f267175SJeff Roberson 		if (size & KMEM_ZMASK)
3146f267175SJeff Roberson 			size = (size & ~KMEM_ZMASK) + KMEM_ZBASE;
3156f267175SJeff Roberson 		indx = kmemsize[size >> KMEM_ZSHIFT];
3166f267175SJeff Roberson 		zone = kmemzones[indx].kz_zone;
317099a0e58SBosko Milekic 		keg = zone->uz_keg;
3186f267175SJeff Roberson #ifdef MALLOC_PROFILE
3196f267175SJeff Roberson 		krequests[size >> KMEM_ZSHIFT]++;
3206f267175SJeff Roberson #endif
3218355f576SJeff Roberson 		va = uma_zalloc(zone, flags);
3224362fadaSBrian Feldman 		if (va != NULL)
323099a0e58SBosko Milekic 			size = keg->uk_size;
32463a7e0a3SRobert Watson 		malloc_type_zone_allocated(mtp, va == NULL ? 0 : size, indx);
3258355f576SJeff Roberson 	} else {
3266f267175SJeff Roberson 		size = roundup(size, PAGE_SIZE);
3278355f576SJeff Roberson 		zone = NULL;
328099a0e58SBosko Milekic 		keg = NULL;
3298355f576SJeff Roberson 		va = uma_large_malloc(size, flags);
33063a7e0a3SRobert Watson 		malloc_type_allocated(mtp, va == NULL ? 0 : size);
331df8bae1dSRodney W. Grimes 	}
3321282e9acSPoul-Henning Kamp 	if (flags & M_WAITOK)
333a163d034SWarner Losh 		KASSERT(va != NULL, ("malloc(M_WAITOK) returned NULL"));
3341282e9acSPoul-Henning Kamp 	else if (va == NULL)
3351fb14a47SPoul-Henning Kamp 		t_malloc_fail = time_uptime;
3364db4f5c8SPoul-Henning Kamp #ifdef DIAGNOSTIC
3371282e9acSPoul-Henning Kamp 	if (va != NULL && !(flags & M_ZERO)) {
3384db4f5c8SPoul-Henning Kamp 		memset(va, 0x70, osize);
3394db4f5c8SPoul-Henning Kamp 	}
3404db4f5c8SPoul-Henning Kamp #endif
341847a2a17SPawel Jakub Dawidek #ifdef DEBUG_REDZONE
342847a2a17SPawel Jakub Dawidek 	if (va != NULL)
343847a2a17SPawel Jakub Dawidek 		va = redzone_setup(va, osize);
344847a2a17SPawel Jakub Dawidek #endif
345df8bae1dSRodney W. Grimes 	return ((void *) va);
346df8bae1dSRodney W. Grimes }
347df8bae1dSRodney W. Grimes 
348df8bae1dSRodney W. Grimes /*
3491c7c3c6aSMatthew Dillon  *	free:
3501c7c3c6aSMatthew Dillon  *
351df8bae1dSRodney W. Grimes  *	Free a block of memory allocated by malloc.
3521c7c3c6aSMatthew Dillon  *
3531c7c3c6aSMatthew Dillon  *	This routine may not block.
354df8bae1dSRodney W. Grimes  */
355df8bae1dSRodney W. Grimes void
35663a7e0a3SRobert Watson free(void *addr, struct malloc_type *mtp)
357df8bae1dSRodney W. Grimes {
35899571dc3SJeff Roberson 	uma_slab_t slab;
35999571dc3SJeff Roberson 	u_long size;
360254c6cb3SPoul-Henning Kamp 
36144a8ff31SArchie Cobbs 	/* free(NULL, ...) does nothing */
36244a8ff31SArchie Cobbs 	if (addr == NULL)
36344a8ff31SArchie Cobbs 		return;
36444a8ff31SArchie Cobbs 
365e4eb384bSBosko Milekic #ifdef DEBUG_MEMGUARD
366d362c40dSPawel Jakub Dawidek 	if (memguard_cmp(mtp)) {
367e4eb384bSBosko Milekic 		memguard_free(addr);
368e4eb384bSBosko Milekic 		return;
369e4eb384bSBosko Milekic 	}
370e4eb384bSBosko Milekic #endif
371e4eb384bSBosko Milekic 
372847a2a17SPawel Jakub Dawidek #ifdef DEBUG_REDZONE
373847a2a17SPawel Jakub Dawidek 	redzone_check(addr);
374847a2a17SPawel Jakub Dawidek 	addr = redzone_addr_ntor(addr);
375847a2a17SPawel Jakub Dawidek #endif
376847a2a17SPawel Jakub Dawidek 
3778355f576SJeff Roberson 	size = 0;
37869ef67f9SJason Evans 
37999571dc3SJeff Roberson 	slab = vtoslab((vm_offset_t)addr & (~UMA_SLAB_MASK));
3808355f576SJeff Roberson 
3818355f576SJeff Roberson 	if (slab == NULL)
3826f267175SJeff Roberson 		panic("free: address %p(%p) has not been allocated.\n",
38399571dc3SJeff Roberson 		    addr, (void *)((u_long)addr & (~UMA_SLAB_MASK)));
38499571dc3SJeff Roberson 
3858355f576SJeff Roberson 
3868355f576SJeff Roberson 	if (!(slab->us_flags & UMA_SLAB_MALLOC)) {
3878f70816cSJeff Roberson #ifdef INVARIANTS
38863a7e0a3SRobert Watson 		struct malloc_type **mtpp = addr;
3898f70816cSJeff Roberson #endif
390099a0e58SBosko Milekic 		size = slab->us_keg->uk_size;
3918f70816cSJeff Roberson #ifdef INVARIANTS
3928f70816cSJeff Roberson 		/*
3938f70816cSJeff Roberson 		 * Cache a pointer to the malloc_type that most recently freed
3948f70816cSJeff Roberson 		 * this memory here.  This way we know who is most likely to
3958f70816cSJeff Roberson 		 * have stepped on it later.
3968f70816cSJeff Roberson 		 *
3978f70816cSJeff Roberson 		 * This code assumes that size is a multiple of 8 bytes for
3988f70816cSJeff Roberson 		 * 64 bit machines
3998f70816cSJeff Roberson 		 */
40063a7e0a3SRobert Watson 		mtpp = (struct malloc_type **)
40163a7e0a3SRobert Watson 		    ((unsigned long)mtpp & ~UMA_ALIGN_PTR);
40263a7e0a3SRobert Watson 		mtpp += (size - sizeof(struct malloc_type *)) /
4038f70816cSJeff Roberson 		    sizeof(struct malloc_type *);
40463a7e0a3SRobert Watson 		*mtpp = mtp;
4058f70816cSJeff Roberson #endif
406099a0e58SBosko Milekic 		uma_zfree_arg(LIST_FIRST(&slab->us_keg->uk_zones), addr, slab);
40714bf02f8SJohn Dyson 	} else {
4088355f576SJeff Roberson 		size = slab->us_size;
4098355f576SJeff Roberson 		uma_large_free(slab);
41014bf02f8SJohn Dyson 	}
41163a7e0a3SRobert Watson 	malloc_type_freed(mtp, size);
412df8bae1dSRodney W. Grimes }
413df8bae1dSRodney W. Grimes 
414df8bae1dSRodney W. Grimes /*
41544a8ff31SArchie Cobbs  *	realloc: change the size of a memory block
41644a8ff31SArchie Cobbs  */
41744a8ff31SArchie Cobbs void *
41863a7e0a3SRobert Watson realloc(void *addr, unsigned long size, struct malloc_type *mtp, int flags)
41944a8ff31SArchie Cobbs {
4208355f576SJeff Roberson 	uma_slab_t slab;
42144a8ff31SArchie Cobbs 	unsigned long alloc;
42244a8ff31SArchie Cobbs 	void *newaddr;
42344a8ff31SArchie Cobbs 
42444a8ff31SArchie Cobbs 	/* realloc(NULL, ...) is equivalent to malloc(...) */
42544a8ff31SArchie Cobbs 	if (addr == NULL)
42663a7e0a3SRobert Watson 		return (malloc(size, mtp, flags));
42763a7e0a3SRobert Watson 
42863a7e0a3SRobert Watson 	/*
42963a7e0a3SRobert Watson 	 * XXX: Should report free of old memory and alloc of new memory to
43063a7e0a3SRobert Watson 	 * per-CPU stats.
43163a7e0a3SRobert Watson 	 */
43244a8ff31SArchie Cobbs 
433e4eb384bSBosko Milekic #ifdef DEBUG_MEMGUARD
434d362c40dSPawel Jakub Dawidek if (memguard_cmp(mtp)) {
435e4eb384bSBosko Milekic 	slab = NULL;
436e4eb384bSBosko Milekic 	alloc = size;
437e4eb384bSBosko Milekic } else {
438e4eb384bSBosko Milekic #endif
439e4eb384bSBosko Milekic 
440847a2a17SPawel Jakub Dawidek #ifdef DEBUG_REDZONE
441847a2a17SPawel Jakub Dawidek 	slab = NULL;
442847a2a17SPawel Jakub Dawidek 	alloc = redzone_get_size(addr);
443847a2a17SPawel Jakub Dawidek #else
44499571dc3SJeff Roberson 	slab = vtoslab((vm_offset_t)addr & ~(UMA_SLAB_MASK));
4458355f576SJeff Roberson 
44644a8ff31SArchie Cobbs 	/* Sanity check */
4478355f576SJeff Roberson 	KASSERT(slab != NULL,
44844a8ff31SArchie Cobbs 	    ("realloc: address %p out of range", (void *)addr));
44944a8ff31SArchie Cobbs 
45044a8ff31SArchie Cobbs 	/* Get the size of the original block */
451619f2841SPawel Jakub Dawidek 	if (!(slab->us_flags & UMA_SLAB_MALLOC))
452099a0e58SBosko Milekic 		alloc = slab->us_keg->uk_size;
4538355f576SJeff Roberson 	else
4548355f576SJeff Roberson 		alloc = slab->us_size;
45544a8ff31SArchie Cobbs 
45644a8ff31SArchie Cobbs 	/* Reuse the original block if appropriate */
45744a8ff31SArchie Cobbs 	if (size <= alloc
45844a8ff31SArchie Cobbs 	    && (size > (alloc >> REALLOC_FRACTION) || alloc == MINALLOCSIZE))
45944a8ff31SArchie Cobbs 		return (addr);
460847a2a17SPawel Jakub Dawidek #endif /* !DEBUG_REDZONE */
46144a8ff31SArchie Cobbs 
462e4eb384bSBosko Milekic #ifdef DEBUG_MEMGUARD
463e4eb384bSBosko Milekic }
464e4eb384bSBosko Milekic #endif
465e4eb384bSBosko Milekic 
46644a8ff31SArchie Cobbs 	/* Allocate a new, bigger (or smaller) block */
46763a7e0a3SRobert Watson 	if ((newaddr = malloc(size, mtp, flags)) == NULL)
46844a8ff31SArchie Cobbs 		return (NULL);
46944a8ff31SArchie Cobbs 
47044a8ff31SArchie Cobbs 	/* Copy over original contents */
47144a8ff31SArchie Cobbs 	bcopy(addr, newaddr, min(size, alloc));
47263a7e0a3SRobert Watson 	free(addr, mtp);
47344a8ff31SArchie Cobbs 	return (newaddr);
47444a8ff31SArchie Cobbs }
47544a8ff31SArchie Cobbs 
47644a8ff31SArchie Cobbs /*
47744a8ff31SArchie Cobbs  *	reallocf: same as realloc() but free memory on failure.
47844a8ff31SArchie Cobbs  */
47944a8ff31SArchie Cobbs void *
48063a7e0a3SRobert Watson reallocf(void *addr, unsigned long size, struct malloc_type *mtp, int flags)
48144a8ff31SArchie Cobbs {
48244a8ff31SArchie Cobbs 	void *mem;
48344a8ff31SArchie Cobbs 
48463a7e0a3SRobert Watson 	if ((mem = realloc(addr, size, mtp, flags)) == NULL)
48563a7e0a3SRobert Watson 		free(addr, mtp);
48644a8ff31SArchie Cobbs 	return (mem);
48744a8ff31SArchie Cobbs }
48844a8ff31SArchie Cobbs 
48944a8ff31SArchie Cobbs /*
490df8bae1dSRodney W. Grimes  * Initialize the kernel memory allocator
491df8bae1dSRodney W. Grimes  */
4922b14f991SJulian Elischer /* ARGSUSED*/
4932b14f991SJulian Elischer static void
49487efd4d5SRobert Watson kmeminit(void *dummy)
495df8bae1dSRodney W. Grimes {
4966f267175SJeff Roberson 	u_int8_t indx;
49727b8623fSDavid Greenman 	u_long mem_size;
4988355f576SJeff Roberson 	int i;
4998a58a9f6SJohn Dyson 
5006008862bSJohn Baldwin 	mtx_init(&malloc_mtx, "malloc", NULL, MTX_DEF);
50169ef67f9SJason Evans 
5028a58a9f6SJohn Dyson 	/*
5038a58a9f6SJohn Dyson 	 * Try to auto-tune the kernel memory size, so that it is
5048a58a9f6SJohn Dyson 	 * more applicable for a wider range of machine sizes.
5058a58a9f6SJohn Dyson 	 * On an X86, a VM_KMEM_SIZE_SCALE value of 4 is good, while
5068a58a9f6SJohn Dyson 	 * a VM_KMEM_SIZE of 12MB is a fair compromise.  The
5078a58a9f6SJohn Dyson 	 * VM_KMEM_SIZE_MAX is dependent on the maximum KVA space
5088a58a9f6SJohn Dyson 	 * available, and on an X86 with a total KVA space of 256MB,
5098a58a9f6SJohn Dyson 	 * try to keep VM_KMEM_SIZE_MAX at 80MB or below.
5108a58a9f6SJohn Dyson 	 *
5118a58a9f6SJohn Dyson 	 * Note that the kmem_map is also used by the zone allocator,
5128a58a9f6SJohn Dyson 	 * so make sure that there is enough space.
5138a58a9f6SJohn Dyson 	 */
514099a0e58SBosko Milekic 	vm_kmem_size = VM_KMEM_SIZE + nmbclusters * PAGE_SIZE;
5151795d0cdSPaul Saab 	mem_size = cnt.v_page_count;
5168a58a9f6SJohn Dyson 
5178a58a9f6SJohn Dyson #if defined(VM_KMEM_SIZE_SCALE)
518479439b4SDag-Erling Smørgrav 	vm_kmem_size_scale = VM_KMEM_SIZE_SCALE;
5198a58a9f6SJohn Dyson #endif
520479439b4SDag-Erling Smørgrav 	TUNABLE_INT_FETCH("vm.kmem_size_scale", &vm_kmem_size_scale);
521479439b4SDag-Erling Smørgrav 	if (vm_kmem_size_scale > 0 &&
522479439b4SDag-Erling Smørgrav 	    (mem_size / vm_kmem_size_scale) > (vm_kmem_size / PAGE_SIZE))
523479439b4SDag-Erling Smørgrav 		vm_kmem_size = (mem_size / vm_kmem_size_scale) * PAGE_SIZE;
5248a58a9f6SJohn Dyson 
5258a58a9f6SJohn Dyson #if defined(VM_KMEM_SIZE_MAX)
526479439b4SDag-Erling Smørgrav 	vm_kmem_size_max = VM_KMEM_SIZE_MAX;
5278a58a9f6SJohn Dyson #endif
528479439b4SDag-Erling Smørgrav 	TUNABLE_INT_FETCH("vm.kmem_size_max", &vm_kmem_size_max);
529479439b4SDag-Erling Smørgrav 	if (vm_kmem_size_max > 0 && vm_kmem_size >= vm_kmem_size_max)
530479439b4SDag-Erling Smørgrav 		vm_kmem_size = vm_kmem_size_max;
5318a58a9f6SJohn Dyson 
5328de6e8e1SMike Smith 	/* Allow final override from the kernel environment */
53384344f9fSDag-Erling Smørgrav #ifndef BURN_BRIDGES
53484344f9fSDag-Erling Smørgrav 	if (TUNABLE_INT_FETCH("kern.vm.kmem.size", &vm_kmem_size) != 0)
53584344f9fSDag-Erling Smørgrav 		printf("kern.vm.kmem.size is now called vm.kmem_size!\n");
53684344f9fSDag-Erling Smørgrav #endif
53784344f9fSDag-Erling Smørgrav 	TUNABLE_INT_FETCH("vm.kmem_size", &vm_kmem_size);
5388de6e8e1SMike Smith 
53927b8623fSDavid Greenman 	/*
54027b8623fSDavid Greenman 	 * Limit kmem virtual size to twice the physical memory.
54127b8623fSDavid Greenman 	 * This allows for kmem map sparseness, but limits the size
54227b8623fSDavid Greenman 	 * to something sane. Be careful to not overflow the 32bit
54327b8623fSDavid Greenman 	 * ints while doing the check.
54427b8623fSDavid Greenman 	 */
5451795d0cdSPaul Saab 	if (((vm_kmem_size / 2) / PAGE_SIZE) > cnt.v_page_count)
54627b8623fSDavid Greenman 		vm_kmem_size = 2 * cnt.v_page_count * PAGE_SIZE;
5478a58a9f6SJohn Dyson 
54808442f8aSBosko Milekic 	/*
549347194c1SMike Silbersack 	 * Tune settings based on the kernel map's size at this time.
550347194c1SMike Silbersack 	 */
551347194c1SMike Silbersack 	init_param3(vm_kmem_size / PAGE_SIZE);
552347194c1SMike Silbersack 
553df8bae1dSRodney W. Grimes 	kmem_map = kmem_suballoc(kernel_map, (vm_offset_t *)&kmembase,
554099a0e58SBosko Milekic 		(vm_offset_t *)&kmemlimit, vm_kmem_size);
5553075778bSJohn Dyson 	kmem_map->system_map = 1;
5568355f576SJeff Roberson 
557e4eb384bSBosko Milekic #ifdef DEBUG_MEMGUARD
558e4eb384bSBosko Milekic 	/*
559e4eb384bSBosko Milekic 	 * Initialize MemGuard if support compiled in.  MemGuard is a
560e4eb384bSBosko Milekic 	 * replacement allocator used for detecting tamper-after-free
561e4eb384bSBosko Milekic 	 * scenarios as they occur.  It is only used for debugging.
562e4eb384bSBosko Milekic 	 */
563e4eb384bSBosko Milekic 	vm_memguard_divisor = 10;
564d362c40dSPawel Jakub Dawidek 	TUNABLE_INT_FETCH("vm.memguard.divisor", &vm_memguard_divisor);
565e4eb384bSBosko Milekic 
566e4eb384bSBosko Milekic 	/* Pick a conservative value if provided value sucks. */
567e4eb384bSBosko Milekic 	if ((vm_memguard_divisor <= 0) ||
568e4eb384bSBosko Milekic 	    ((vm_kmem_size / vm_memguard_divisor) == 0))
569e4eb384bSBosko Milekic 		vm_memguard_divisor = 10;
570e4eb384bSBosko Milekic 	memguard_init(kmem_map, vm_kmem_size / vm_memguard_divisor);
571e4eb384bSBosko Milekic #endif
572e4eb384bSBosko Milekic 
57399571dc3SJeff Roberson 	uma_startup2();
5748355f576SJeff Roberson 
57563a7e0a3SRobert Watson 	mt_zone = uma_zcreate("mt_zone", sizeof(struct malloc_type_internal),
57663a7e0a3SRobert Watson #ifdef INVARIANTS
57763a7e0a3SRobert Watson 	    mtrash_ctor, mtrash_dtor, mtrash_init, mtrash_fini,
57863a7e0a3SRobert Watson #else
57963a7e0a3SRobert Watson 	    NULL, NULL, NULL, NULL,
58063a7e0a3SRobert Watson #endif
58163a7e0a3SRobert Watson 	    UMA_ALIGN_PTR, UMA_ZONE_MALLOC);
5826f267175SJeff Roberson 	for (i = 0, indx = 0; kmemzones[indx].kz_size != 0; indx++) {
5836f267175SJeff Roberson 		int size = kmemzones[indx].kz_size;
5846f267175SJeff Roberson 		char *name = kmemzones[indx].kz_name;
5858355f576SJeff Roberson 
5868efc4effSJeff Roberson 		kmemzones[indx].kz_zone = uma_zcreate(name, size,
5878efc4effSJeff Roberson #ifdef INVARIANTS
5888f70816cSJeff Roberson 		    mtrash_ctor, mtrash_dtor, mtrash_init, mtrash_fini,
5898efc4effSJeff Roberson #else
5908efc4effSJeff Roberson 		    NULL, NULL, NULL, NULL,
5918efc4effSJeff Roberson #endif
5928efc4effSJeff Roberson 		    UMA_ALIGN_PTR, UMA_ZONE_MALLOC);
5936f267175SJeff Roberson 
5948355f576SJeff Roberson 		for (;i <= size; i+= KMEM_ZBASE)
5956f267175SJeff Roberson 			kmemsize[i >> KMEM_ZSHIFT] = indx;
5968355f576SJeff Roberson 
597df8bae1dSRodney W. Grimes 	}
598254c6cb3SPoul-Henning Kamp }
599254c6cb3SPoul-Henning Kamp 
600db669378SPeter Wemm void
60187efd4d5SRobert Watson malloc_init(void *data)
602254c6cb3SPoul-Henning Kamp {
60363a7e0a3SRobert Watson 	struct malloc_type_internal *mtip;
60463a7e0a3SRobert Watson 	struct malloc_type *mtp;
60563a7e0a3SRobert Watson 
60663a7e0a3SRobert Watson 	KASSERT(cnt.v_page_count != 0, ("malloc_register before vm_init"));
60763a7e0a3SRobert Watson 
60863a7e0a3SRobert Watson 	mtp = data;
60963a7e0a3SRobert Watson 	mtip = uma_zalloc(mt_zone, M_WAITOK | M_ZERO);
61063a7e0a3SRobert Watson 	mtp->ks_handle = mtip;
611254c6cb3SPoul-Henning Kamp 
6126f267175SJeff Roberson 	mtx_lock(&malloc_mtx);
61363a7e0a3SRobert Watson 	mtp->ks_next = kmemstatistics;
61463a7e0a3SRobert Watson 	kmemstatistics = mtp;
615cd814b26SRobert Watson 	kmemcount++;
6166f267175SJeff Roberson 	mtx_unlock(&malloc_mtx);
617df8bae1dSRodney W. Grimes }
618db669378SPeter Wemm 
619db669378SPeter Wemm void
62087efd4d5SRobert Watson malloc_uninit(void *data)
621db669378SPeter Wemm {
62263a7e0a3SRobert Watson 	struct malloc_type_internal *mtip;
6232a143d5bSPawel Jakub Dawidek 	struct malloc_type_stats *mtsp;
62463a7e0a3SRobert Watson 	struct malloc_type *mtp, *temp;
6252a143d5bSPawel Jakub Dawidek 	long temp_allocs, temp_bytes;
6262a143d5bSPawel Jakub Dawidek 	int i;
627db669378SPeter Wemm 
62863a7e0a3SRobert Watson 	mtp = data;
62963a7e0a3SRobert Watson 	KASSERT(mtp->ks_handle != NULL, ("malloc_deregister: cookie NULL"));
6306f267175SJeff Roberson 	mtx_lock(&malloc_mtx);
63163a7e0a3SRobert Watson 	mtip = mtp->ks_handle;
63263a7e0a3SRobert Watson 	mtp->ks_handle = NULL;
63363a7e0a3SRobert Watson 	if (mtp != kmemstatistics) {
63463a7e0a3SRobert Watson 		for (temp = kmemstatistics; temp != NULL;
63563a7e0a3SRobert Watson 		    temp = temp->ks_next) {
63663a7e0a3SRobert Watson 			if (temp->ks_next == mtp)
63763a7e0a3SRobert Watson 				temp->ks_next = mtp->ks_next;
638db669378SPeter Wemm 		}
63963a7e0a3SRobert Watson 	} else
64063a7e0a3SRobert Watson 		kmemstatistics = mtp->ks_next;
641cd814b26SRobert Watson 	kmemcount--;
6426f267175SJeff Roberson 	mtx_unlock(&malloc_mtx);
6432a143d5bSPawel Jakub Dawidek 
6442a143d5bSPawel Jakub Dawidek 	/*
6452a143d5bSPawel Jakub Dawidek 	 * Look for memory leaks.
6462a143d5bSPawel Jakub Dawidek 	 */
6472a143d5bSPawel Jakub Dawidek 	temp_allocs = temp_bytes = 0;
6482a143d5bSPawel Jakub Dawidek 	for (i = 0; i < MAXCPU; i++) {
6492a143d5bSPawel Jakub Dawidek 		mtsp = &mtip->mti_stats[i];
6502a143d5bSPawel Jakub Dawidek 		temp_allocs += mtsp->mts_numallocs;
6512a143d5bSPawel Jakub Dawidek 		temp_allocs -= mtsp->mts_numfrees;
6522a143d5bSPawel Jakub Dawidek 		temp_bytes += mtsp->mts_memalloced;
6532a143d5bSPawel Jakub Dawidek 		temp_bytes -= mtsp->mts_memfreed;
6542a143d5bSPawel Jakub Dawidek 	}
6552a143d5bSPawel Jakub Dawidek 	if (temp_allocs > 0 || temp_bytes > 0) {
6562a143d5bSPawel Jakub Dawidek 		printf("Warning: memory type %s leaked memory on destroy "
6572a143d5bSPawel Jakub Dawidek 		    "(%ld allocations, %ld bytes leaked).\n", mtp->ks_shortdesc,
6582a143d5bSPawel Jakub Dawidek 		    temp_allocs, temp_bytes);
6592a143d5bSPawel Jakub Dawidek 	}
6602a143d5bSPawel Jakub Dawidek 
6618c61b219SJoseph Koshy 	uma_zfree(mt_zone, mtip);
662db669378SPeter Wemm }
6636f267175SJeff Roberson 
664d362c40dSPawel Jakub Dawidek struct malloc_type *
665d362c40dSPawel Jakub Dawidek malloc_desc2type(const char *desc)
666d362c40dSPawel Jakub Dawidek {
667d362c40dSPawel Jakub Dawidek 	struct malloc_type *mtp;
668d362c40dSPawel Jakub Dawidek 
669d362c40dSPawel Jakub Dawidek 	mtx_assert(&malloc_mtx, MA_OWNED);
670d362c40dSPawel Jakub Dawidek 	for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) {
671d362c40dSPawel Jakub Dawidek 		if (strcmp(mtp->ks_shortdesc, desc) == 0)
672d362c40dSPawel Jakub Dawidek 			return (mtp);
673d362c40dSPawel Jakub Dawidek 	}
674d362c40dSPawel Jakub Dawidek 	return (NULL);
675d362c40dSPawel Jakub Dawidek }
676d362c40dSPawel Jakub Dawidek 
6776f267175SJeff Roberson static int
6786f267175SJeff Roberson sysctl_kern_malloc(SYSCTL_HANDLER_ARGS)
6796f267175SJeff Roberson {
68063a7e0a3SRobert Watson 	struct malloc_type_stats mts_local, *mtsp;
68163a7e0a3SRobert Watson 	struct malloc_type_internal *mtip;
68263a7e0a3SRobert Watson 	struct malloc_type *mtp;
68363a7e0a3SRobert Watson 	struct sbuf sbuf;
68463a7e0a3SRobert Watson 	long temp_allocs, temp_bytes;
6856f267175SJeff Roberson 	int linesize = 128;
6866f267175SJeff Roberson 	int bufsize;
6876f267175SJeff Roberson 	int first;
6886f267175SJeff Roberson 	int error;
6896f267175SJeff Roberson 	char *buf;
6906f267175SJeff Roberson 	int cnt;
6916f267175SJeff Roberson 	int i;
6926f267175SJeff Roberson 
6936f267175SJeff Roberson 	cnt = 0;
6946f267175SJeff Roberson 
69563a7e0a3SRobert Watson 	/* Guess at how much room is needed. */
6966f267175SJeff Roberson 	mtx_lock(&malloc_mtx);
697cd814b26SRobert Watson 	cnt = kmemcount;
6985a34a9f0SJeff Roberson 	mtx_unlock(&malloc_mtx);
69963a7e0a3SRobert Watson 
7006f267175SJeff Roberson 	bufsize = linesize * (cnt + 1);
70163a7e0a3SRobert Watson 	buf = malloc(bufsize, M_TEMP, M_WAITOK|M_ZERO);
70263a7e0a3SRobert Watson 	sbuf_new(&sbuf, buf, bufsize, SBUF_FIXEDLEN);
70363a7e0a3SRobert Watson 
7045a34a9f0SJeff Roberson 	mtx_lock(&malloc_mtx);
70563a7e0a3SRobert Watson 	sbuf_printf(&sbuf,
7066f267175SJeff Roberson 	    "\n        Type  InUse MemUse HighUse Requests  Size(s)\n");
70763a7e0a3SRobert Watson 	for (mtp = kmemstatistics; cnt != 0 && mtp != NULL;
70863a7e0a3SRobert Watson 	    mtp = mtp->ks_next, cnt--) {
70963a7e0a3SRobert Watson 		mtip = mtp->ks_handle;
71063a7e0a3SRobert Watson 		bzero(&mts_local, sizeof(mts_local));
71163a7e0a3SRobert Watson 		for (i = 0; i < MAXCPU; i++) {
71263a7e0a3SRobert Watson 			mtsp = &mtip->mti_stats[i];
71363a7e0a3SRobert Watson 			mts_local.mts_memalloced += mtsp->mts_memalloced;
71463a7e0a3SRobert Watson 			mts_local.mts_memfreed += mtsp->mts_memfreed;
71563a7e0a3SRobert Watson 			mts_local.mts_numallocs += mtsp->mts_numallocs;
71663a7e0a3SRobert Watson 			mts_local.mts_numfrees += mtsp->mts_numfrees;
71763a7e0a3SRobert Watson 			mts_local.mts_size |= mtsp->mts_size;
71863a7e0a3SRobert Watson 		}
71963a7e0a3SRobert Watson 		if (mts_local.mts_numallocs == 0)
7206f267175SJeff Roberson 			continue;
7216f267175SJeff Roberson 
72263a7e0a3SRobert Watson 		/*
72363a7e0a3SRobert Watson 		 * Due to races in per-CPU statistics gather, it's possible to
72463a7e0a3SRobert Watson 		 * get a slightly negative number here.  If we do, approximate
72563a7e0a3SRobert Watson 		 * with 0.
72663a7e0a3SRobert Watson 		 */
72763a7e0a3SRobert Watson 		if (mts_local.mts_numallocs > mts_local.mts_numfrees)
72863a7e0a3SRobert Watson 			temp_allocs = mts_local.mts_numallocs -
72963a7e0a3SRobert Watson 			    mts_local.mts_numfrees;
73063a7e0a3SRobert Watson 		else
73163a7e0a3SRobert Watson 			temp_allocs = 0;
73263a7e0a3SRobert Watson 
73363a7e0a3SRobert Watson 		/*
73463a7e0a3SRobert Watson 		 * Ditto for bytes allocated.
73563a7e0a3SRobert Watson 		 */
73663a7e0a3SRobert Watson 		if (mts_local.mts_memalloced > mts_local.mts_memfreed)
73763a7e0a3SRobert Watson 			temp_bytes = mts_local.mts_memalloced -
73863a7e0a3SRobert Watson 			    mts_local.mts_memfreed;
73963a7e0a3SRobert Watson 		else
74063a7e0a3SRobert Watson 			temp_bytes = 0;
74163a7e0a3SRobert Watson 
74263a7e0a3SRobert Watson 		/*
743cd814b26SRobert Watson 		 * High-waterwark is no longer easily available, so we just
744cd814b26SRobert Watson 		 * print '-' for that column.
74563a7e0a3SRobert Watson 		 */
746cd814b26SRobert Watson 		sbuf_printf(&sbuf, "%13s%6lu%6luK       -%9llu",
74763a7e0a3SRobert Watson 		    mtp->ks_shortdesc,
74863a7e0a3SRobert Watson 		    temp_allocs,
74963a7e0a3SRobert Watson 		    (temp_bytes + 1023) / 1024,
7504f8721d2SRobert Watson 		    (unsigned long long)mts_local.mts_numallocs);
7516f267175SJeff Roberson 
7526f267175SJeff Roberson 		first = 1;
753280759e7SRobert Drehmel 		for (i = 0; i < sizeof(kmemzones) / sizeof(kmemzones[0]) - 1;
754280759e7SRobert Drehmel 		    i++) {
75563a7e0a3SRobert Watson 			if (mts_local.mts_size & (1 << i)) {
7566f267175SJeff Roberson 				if (first)
75763a7e0a3SRobert Watson 					sbuf_printf(&sbuf, "  ");
7586f267175SJeff Roberson 				else
75963a7e0a3SRobert Watson 					sbuf_printf(&sbuf, ",");
76063a7e0a3SRobert Watson 				sbuf_printf(&sbuf, "%s",
76163a7e0a3SRobert Watson 				    kmemzones[i].kz_name);
7626f267175SJeff Roberson 				first = 0;
7636f267175SJeff Roberson 			}
764280759e7SRobert Drehmel 		}
76563a7e0a3SRobert Watson 		sbuf_printf(&sbuf, "\n");
7666f267175SJeff Roberson 	}
76763a7e0a3SRobert Watson 	sbuf_finish(&sbuf);
7686f267175SJeff Roberson 	mtx_unlock(&malloc_mtx);
7696f267175SJeff Roberson 
77063a7e0a3SRobert Watson 	error = SYSCTL_OUT(req, sbuf_data(&sbuf), sbuf_len(&sbuf));
77163a7e0a3SRobert Watson 
77263a7e0a3SRobert Watson 	sbuf_delete(&sbuf);
7736f267175SJeff Roberson 	free(buf, M_TEMP);
7746f267175SJeff Roberson 	return (error);
7756f267175SJeff Roberson }
7766f267175SJeff Roberson 
7776f267175SJeff Roberson SYSCTL_OID(_kern, OID_AUTO, malloc, CTLTYPE_STRING|CTLFLAG_RD,
7786f267175SJeff Roberson     NULL, 0, sysctl_kern_malloc, "A", "Malloc Stats");
7795e914b96SJeff Roberson 
780cd814b26SRobert Watson static int
781cd814b26SRobert Watson sysctl_kern_malloc_stats(SYSCTL_HANDLER_ARGS)
782cd814b26SRobert Watson {
783cd814b26SRobert Watson 	struct malloc_type_stream_header mtsh;
784cd814b26SRobert Watson 	struct malloc_type_internal *mtip;
785cd814b26SRobert Watson 	struct malloc_type_header mth;
786cd814b26SRobert Watson 	struct malloc_type *mtp;
787cd814b26SRobert Watson 	int buflen, count, error, i;
788cd814b26SRobert Watson 	struct sbuf sbuf;
789cd814b26SRobert Watson 	char *buffer;
790cd814b26SRobert Watson 
791cd814b26SRobert Watson 	mtx_lock(&malloc_mtx);
792cd814b26SRobert Watson restart:
793cd814b26SRobert Watson 	mtx_assert(&malloc_mtx, MA_OWNED);
794cd814b26SRobert Watson 	count = kmemcount;
795cd814b26SRobert Watson 	mtx_unlock(&malloc_mtx);
796cd814b26SRobert Watson 	buflen = sizeof(mtsh) + count * (sizeof(mth) +
797cd814b26SRobert Watson 	    sizeof(struct malloc_type_stats) * MAXCPU) + 1;
798cd814b26SRobert Watson 	buffer = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
799cd814b26SRobert Watson 	mtx_lock(&malloc_mtx);
800cd814b26SRobert Watson 	if (count < kmemcount) {
801cd814b26SRobert Watson 		free(buffer, M_TEMP);
802cd814b26SRobert Watson 		goto restart;
803cd814b26SRobert Watson 	}
804cd814b26SRobert Watson 
805cd814b26SRobert Watson 	sbuf_new(&sbuf, buffer, buflen, SBUF_FIXEDLEN);
806cd814b26SRobert Watson 
807cd814b26SRobert Watson 	/*
808cd814b26SRobert Watson 	 * Insert stream header.
809cd814b26SRobert Watson 	 */
810cd814b26SRobert Watson 	bzero(&mtsh, sizeof(mtsh));
811cd814b26SRobert Watson 	mtsh.mtsh_version = MALLOC_TYPE_STREAM_VERSION;
812cd814b26SRobert Watson 	mtsh.mtsh_maxcpus = MAXCPU;
813cd814b26SRobert Watson 	mtsh.mtsh_count = kmemcount;
814cd814b26SRobert Watson 	if (sbuf_bcat(&sbuf, &mtsh, sizeof(mtsh)) < 0) {
815cd814b26SRobert Watson 		mtx_unlock(&malloc_mtx);
816cd814b26SRobert Watson 		error = ENOMEM;
817cd814b26SRobert Watson 		goto out;
818cd814b26SRobert Watson 	}
819cd814b26SRobert Watson 
820cd814b26SRobert Watson 	/*
821cd814b26SRobert Watson 	 * Insert alternating sequence of type headers and type statistics.
822cd814b26SRobert Watson 	 */
823cd814b26SRobert Watson 	for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) {
824cd814b26SRobert Watson 		mtip = (struct malloc_type_internal *)mtp->ks_handle;
825cd814b26SRobert Watson 
826cd814b26SRobert Watson 		/*
827cd814b26SRobert Watson 		 * Insert type header.
828cd814b26SRobert Watson 		 */
829cd814b26SRobert Watson 		bzero(&mth, sizeof(mth));
830cd814b26SRobert Watson 		strlcpy(mth.mth_name, mtp->ks_shortdesc, MALLOC_MAX_NAME);
831cd814b26SRobert Watson 		if (sbuf_bcat(&sbuf, &mth, sizeof(mth)) < 0) {
832cd814b26SRobert Watson 			mtx_unlock(&malloc_mtx);
833cd814b26SRobert Watson 			error = ENOMEM;
834cd814b26SRobert Watson 			goto out;
835cd814b26SRobert Watson 		}
836cd814b26SRobert Watson 
837cd814b26SRobert Watson 		/*
838cd814b26SRobert Watson 		 * Insert type statistics for each CPU.
839cd814b26SRobert Watson 		 */
840cd814b26SRobert Watson 		for (i = 0; i < MAXCPU; i++) {
841cd814b26SRobert Watson 			if (sbuf_bcat(&sbuf, &mtip->mti_stats[i],
842cd814b26SRobert Watson 			    sizeof(mtip->mti_stats[i])) < 0) {
843cd814b26SRobert Watson 				mtx_unlock(&malloc_mtx);
844cd814b26SRobert Watson 				error = ENOMEM;
845cd814b26SRobert Watson 				goto out;
846cd814b26SRobert Watson 			}
847cd814b26SRobert Watson 		}
848cd814b26SRobert Watson 	}
849cd814b26SRobert Watson 	mtx_unlock(&malloc_mtx);
850cd814b26SRobert Watson 	sbuf_finish(&sbuf);
851cd814b26SRobert Watson 	error = SYSCTL_OUT(req, sbuf_data(&sbuf), sbuf_len(&sbuf));
852cd814b26SRobert Watson out:
853cd814b26SRobert Watson 	sbuf_delete(&sbuf);
854cd814b26SRobert Watson 	free(buffer, M_TEMP);
855cd814b26SRobert Watson 	return (error);
856cd814b26SRobert Watson }
857cd814b26SRobert Watson 
858cd814b26SRobert Watson SYSCTL_PROC(_kern, OID_AUTO, malloc_stats, CTLFLAG_RD|CTLTYPE_STRUCT,
859cd814b26SRobert Watson     0, 0, sysctl_kern_malloc_stats, "s,malloc_type_ustats",
860cd814b26SRobert Watson     "Return malloc types");
861cd814b26SRobert Watson 
862cd814b26SRobert Watson SYSCTL_INT(_kern, OID_AUTO, malloc_count, CTLFLAG_RD, &kmemcount, 0,
863cd814b26SRobert Watson     "Count of kernel malloc types");
864cd814b26SRobert Watson 
865909ed16cSRobert Watson #ifdef DDB
866909ed16cSRobert Watson DB_SHOW_COMMAND(malloc, db_show_malloc)
867909ed16cSRobert Watson {
868909ed16cSRobert Watson 	struct malloc_type_internal *mtip;
869909ed16cSRobert Watson 	struct malloc_type *mtp;
870909ed16cSRobert Watson 	u_int64_t allocs, frees;
871909ed16cSRobert Watson 	int i;
872909ed16cSRobert Watson 
873909ed16cSRobert Watson 	db_printf("%18s %12s %12s %12s\n", "Type", "Allocs", "Frees",
874909ed16cSRobert Watson 	    "Used");
875909ed16cSRobert Watson 	for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) {
876909ed16cSRobert Watson 		mtip = (struct malloc_type_internal *)mtp->ks_handle;
877909ed16cSRobert Watson 		allocs = 0;
878909ed16cSRobert Watson 		frees = 0;
879909ed16cSRobert Watson 		for (i = 0; i < MAXCPU; i++) {
880909ed16cSRobert Watson 			allocs += mtip->mti_stats[i].mts_numallocs;
881909ed16cSRobert Watson 			frees += mtip->mti_stats[i].mts_numfrees;
882909ed16cSRobert Watson 		}
88364a266f9SRobert Watson 		db_printf("%18s %12ju %12ju %12ju\n", mtp->ks_shortdesc,
884909ed16cSRobert Watson 		    allocs, frees, allocs - frees);
885909ed16cSRobert Watson 	}
886909ed16cSRobert Watson }
887909ed16cSRobert Watson #endif
888909ed16cSRobert Watson 
8895e914b96SJeff Roberson #ifdef MALLOC_PROFILE
8905e914b96SJeff Roberson 
8915e914b96SJeff Roberson static int
8925e914b96SJeff Roberson sysctl_kern_mprof(SYSCTL_HANDLER_ARGS)
8935e914b96SJeff Roberson {
8945e914b96SJeff Roberson 	int linesize = 64;
89563a7e0a3SRobert Watson 	struct sbuf sbuf;
8965e914b96SJeff Roberson 	uint64_t count;
8975e914b96SJeff Roberson 	uint64_t waste;
8985e914b96SJeff Roberson 	uint64_t mem;
8995e914b96SJeff Roberson 	int bufsize;
9005e914b96SJeff Roberson 	int error;
9015e914b96SJeff Roberson 	char *buf;
9025e914b96SJeff Roberson 	int rsize;
9035e914b96SJeff Roberson 	int size;
9045e914b96SJeff Roberson 	int i;
9055e914b96SJeff Roberson 
9065e914b96SJeff Roberson 	bufsize = linesize * (KMEM_ZSIZE + 1);
9075e914b96SJeff Roberson 	bufsize += 128; 	/* For the stats line */
9085e914b96SJeff Roberson 	bufsize += 128; 	/* For the banner line */
9095e914b96SJeff Roberson 	waste = 0;
9105e914b96SJeff Roberson 	mem = 0;
9115e914b96SJeff Roberson 
91263a7e0a3SRobert Watson 	buf = malloc(bufsize, M_TEMP, M_WAITOK|M_ZERO);
91363a7e0a3SRobert Watson 	sbuf_new(&sbuf, buf, bufsize, SBUF_FIXEDLEN);
91463a7e0a3SRobert Watson 	sbuf_printf(&sbuf,
9155e914b96SJeff Roberson 	    "\n  Size                    Requests  Real Size\n");
9165e914b96SJeff Roberson 	for (i = 0; i < KMEM_ZSIZE; i++) {
9175e914b96SJeff Roberson 		size = i << KMEM_ZSHIFT;
9185e914b96SJeff Roberson 		rsize = kmemzones[kmemsize[i]].kz_size;
9195e914b96SJeff Roberson 		count = (long long unsigned)krequests[i];
9205e914b96SJeff Roberson 
92163a7e0a3SRobert Watson 		sbuf_printf(&sbuf, "%6d%28llu%11d\n", size,
92263a7e0a3SRobert Watson 		    (unsigned long long)count, rsize);
9235e914b96SJeff Roberson 
9245e914b96SJeff Roberson 		if ((rsize * count) > (size * count))
9255e914b96SJeff Roberson 			waste += (rsize * count) - (size * count);
9265e914b96SJeff Roberson 		mem += (rsize * count);
9275e914b96SJeff Roberson 	}
92863a7e0a3SRobert Watson 	sbuf_printf(&sbuf,
9295e914b96SJeff Roberson 	    "\nTotal memory used:\t%30llu\nTotal Memory wasted:\t%30llu\n",
9305e914b96SJeff Roberson 	    (unsigned long long)mem, (unsigned long long)waste);
93163a7e0a3SRobert Watson 	sbuf_finish(&sbuf);
9325e914b96SJeff Roberson 
93363a7e0a3SRobert Watson 	error = SYSCTL_OUT(req, sbuf_data(&sbuf), sbuf_len(&sbuf));
9345e914b96SJeff Roberson 
93563a7e0a3SRobert Watson 	sbuf_delete(&sbuf);
9365e914b96SJeff Roberson 	free(buf, M_TEMP);
9375e914b96SJeff Roberson 	return (error);
9385e914b96SJeff Roberson }
9395e914b96SJeff Roberson 
9405e914b96SJeff Roberson SYSCTL_OID(_kern, OID_AUTO, mprof, CTLTYPE_STRING|CTLFLAG_RD,
9415e914b96SJeff Roberson     NULL, 0, sysctl_kern_mprof, "A", "Malloc Profiling");
9425e914b96SJeff Roberson #endif /* MALLOC_PROFILE */
943