xref: /freebsd/sys/kern/kern_malloc.c (revision 8efc4eff000ffdefa1e2aec2db6c3394ab96dc93)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1987, 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
14df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
15df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
16df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
17df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
18df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19df8bae1dSRodney W. Grimes  *    without specific prior written permission.
20df8bae1dSRodney W. Grimes  *
21df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
32df8bae1dSRodney W. Grimes  *
33df8bae1dSRodney W. Grimes  *	@(#)kern_malloc.c	8.3 (Berkeley) 1/4/94
34c3aac50fSPeter Wemm  * $FreeBSD$
35df8bae1dSRodney W. Grimes  */
36df8bae1dSRodney W. Grimes 
378a58a9f6SJohn Dyson #include "opt_vm.h"
388a58a9f6SJohn Dyson 
39df8bae1dSRodney W. Grimes #include <sys/param.h>
4026f9a767SRodney W. Grimes #include <sys/systm.h>
41df8bae1dSRodney W. Grimes #include <sys/kernel.h>
42fb919e4dSMark Murray #include <sys/lock.h>
43df8bae1dSRodney W. Grimes #include <sys/malloc.h>
4454e7152cSDavid Greenman #include <sys/mbuf.h>
45eec258d2SJohn Baldwin #include <sys/mutex.h>
46efeaf95aSDavid Greenman #include <sys/vmmeter.h>
47a448b62aSJake Burkholder #include <sys/proc.h>
486f267175SJeff Roberson #include <sys/sysctl.h>
499a02e8c6SJason Evans 
50df8bae1dSRodney W. Grimes #include <vm/vm.h>
51efeaf95aSDavid Greenman #include <vm/vm_param.h>
52df8bae1dSRodney W. Grimes #include <vm/vm_kern.h>
53efeaf95aSDavid Greenman #include <vm/vm_extern.h>
543075778bSJohn Dyson #include <vm/pmap.h>
553075778bSJohn Dyson #include <vm/vm_map.h>
568355f576SJeff Roberson #include <vm/uma.h>
578355f576SJeff Roberson #include <vm/uma_int.h>
588efc4effSJeff Roberson #include <vm/uma_dbg.h>
59df8bae1dSRodney W. Grimes 
60984982d6SPoul-Henning Kamp #if defined(INVARIANTS) && defined(__i386__)
61984982d6SPoul-Henning Kamp #include <machine/cpu.h>
62984982d6SPoul-Henning Kamp #endif
63984982d6SPoul-Henning Kamp 
6444a8ff31SArchie Cobbs /*
6544a8ff31SArchie Cobbs  * When realloc() is called, if the new size is sufficiently smaller than
6644a8ff31SArchie Cobbs  * the old size, realloc() will allocate a new, smaller block to avoid
6744a8ff31SArchie Cobbs  * wasting memory. 'Sufficiently smaller' is defined as: newsize <=
6844a8ff31SArchie Cobbs  * oldsize / 2^n, where REALLOC_FRACTION defines the value of 'n'.
6944a8ff31SArchie Cobbs  */
7044a8ff31SArchie Cobbs #ifndef REALLOC_FRACTION
7144a8ff31SArchie Cobbs #define	REALLOC_FRACTION	1	/* new block if <= half the size */
7244a8ff31SArchie Cobbs #endif
7344a8ff31SArchie Cobbs 
743b6fb885SPoul-Henning Kamp MALLOC_DEFINE(M_CACHE, "cache", "Various Dynamically allocated caches");
759ef246c6SBruce Evans MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory");
769ef246c6SBruce Evans MALLOC_DEFINE(M_TEMP, "temp", "misc temporary data buffers");
779ef246c6SBruce Evans 
7882cd038dSYoshinobu Inoue MALLOC_DEFINE(M_IP6OPT, "ip6opt", "IPv6 options");
7982cd038dSYoshinobu Inoue MALLOC_DEFINE(M_IP6NDP, "ip6ndp", "IPv6 Neighbor Discovery");
8082cd038dSYoshinobu Inoue 
814d77a549SAlfred Perlstein static void kmeminit(void *);
822b14f991SJulian Elischer SYSINIT(kmem, SI_SUB_KMEM, SI_ORDER_FIRST, kmeminit, NULL)
832b14f991SJulian Elischer 
84a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_FREE, "free", "should be on free list");
85a1c995b6SPoul-Henning Kamp 
86db669378SPeter Wemm static struct malloc_type *kmemstatistics;
87254c6cb3SPoul-Henning Kamp static char *kmembase;
88043a2f3bSBruce Evans static char *kmemlimit;
891f6889a1SMatthew Dillon 
908355f576SJeff Roberson #define KMEM_ZSHIFT	4
918355f576SJeff Roberson #define KMEM_ZBASE	16
928355f576SJeff Roberson #define KMEM_ZMASK	(KMEM_ZBASE - 1)
938355f576SJeff Roberson 
94bd796eb2SRobert Watson #define KMEM_ZMAX	65536
958355f576SJeff Roberson #define KMEM_ZSIZE	(KMEM_ZMAX >> KMEM_ZSHIFT)
966f267175SJeff Roberson static u_int8_t kmemsize[KMEM_ZSIZE + 1];
976f267175SJeff Roberson 
988355f576SJeff Roberson /* These won't be powers of two for long */
998355f576SJeff Roberson struct {
1006f267175SJeff Roberson 	int kz_size;
1016f267175SJeff Roberson 	char *kz_name;
1026f267175SJeff Roberson 	uma_zone_t kz_zone;
1036f267175SJeff Roberson } kmemzones[] = {
1046f267175SJeff Roberson 	{16, "16", NULL},
1056f267175SJeff Roberson 	{32, "32", NULL},
1066f267175SJeff Roberson 	{64, "64", NULL},
1076f267175SJeff Roberson 	{128, "128", NULL},
1086f267175SJeff Roberson 	{256, "256", NULL},
1096f267175SJeff Roberson 	{512, "512", NULL},
1106f267175SJeff Roberson 	{1024, "1024", NULL},
1116f267175SJeff Roberson 	{2048, "2048", NULL},
1126f267175SJeff Roberson 	{4096, "4096", NULL},
1136f267175SJeff Roberson 	{8192, "8192", NULL},
11443a7c4e9SRobert Watson 	{16384, "16384", NULL},
115bd796eb2SRobert Watson 	{32768, "32768", NULL},
116bd796eb2SRobert Watson 	{65536, "65536", NULL},
1178355f576SJeff Roberson 	{0, NULL},
1188355f576SJeff Roberson };
1198355f576SJeff Roberson 
1206f267175SJeff Roberson u_int vm_kmem_size;
121d1c1b841SJason Evans static struct mtx malloc_mtx;
12269ef67f9SJason Evans 
1235e914b96SJeff Roberson #ifdef MALLOC_PROFILE
1245e914b96SJeff Roberson uint64_t krequests[KMEM_ZSIZE + 1];
1256f267175SJeff Roberson 
1265e914b96SJeff Roberson static int sysctl_kern_mprof(SYSCTL_HANDLER_ARGS);
1275e914b96SJeff Roberson #endif
1285e914b96SJeff Roberson 
1295e914b96SJeff Roberson static int sysctl_kern_malloc(SYSCTL_HANDLER_ARGS);
130df8bae1dSRodney W. Grimes 
131df8bae1dSRodney W. Grimes /*
1321c7c3c6aSMatthew Dillon  *	malloc:
1331c7c3c6aSMatthew Dillon  *
1341c7c3c6aSMatthew Dillon  *	Allocate a block of memory.
1351c7c3c6aSMatthew Dillon  *
1361c7c3c6aSMatthew Dillon  *	If M_NOWAIT is set, this routine will not block and return NULL if
1371c7c3c6aSMatthew Dillon  *	the allocation fails.
138df8bae1dSRodney W. Grimes  */
139df8bae1dSRodney W. Grimes void *
140df8bae1dSRodney W. Grimes malloc(size, type, flags)
141df8bae1dSRodney W. Grimes 	unsigned long size;
14260a513e9SPoul-Henning Kamp 	struct malloc_type *type;
143254c6cb3SPoul-Henning Kamp 	int flags;
144df8bae1dSRodney W. Grimes {
1456f267175SJeff Roberson 	int indx;
1468355f576SJeff Roberson 	caddr_t va;
1478355f576SJeff Roberson 	uma_zone_t zone;
14860a513e9SPoul-Henning Kamp 	register struct malloc_type *ksp = type;
149df8bae1dSRodney W. Grimes 
150708da94eSPoul-Henning Kamp #if 0
151708da94eSPoul-Henning Kamp 	if (size == 0)
152708da94eSPoul-Henning Kamp 		Debugger("zero size malloc");
153708da94eSPoul-Henning Kamp #endif
1540fee3d35SPeter Wemm #if defined(INVARIANTS)
155984982d6SPoul-Henning Kamp 	if (flags == M_WAITOK)
156b40ce416SJulian Elischer 		KASSERT(curthread->td_intr_nesting_level == 0,
157984982d6SPoul-Henning Kamp 		   ("malloc(M_WAITOK) in interrupt context"));
158984982d6SPoul-Henning Kamp #endif
1598355f576SJeff Roberson 	if (size <= KMEM_ZMAX) {
1606f267175SJeff Roberson 		if (size & KMEM_ZMASK)
1616f267175SJeff Roberson 			size = (size & ~KMEM_ZMASK) + KMEM_ZBASE;
1626f267175SJeff Roberson 		indx = kmemsize[size >> KMEM_ZSHIFT];
1636f267175SJeff Roberson 		zone = kmemzones[indx].kz_zone;
1646f267175SJeff Roberson #ifdef MALLOC_PROFILE
1656f267175SJeff Roberson 		krequests[size >> KMEM_ZSHIFT]++;
1666f267175SJeff Roberson #endif
1678355f576SJeff Roberson 		va = uma_zalloc(zone, flags);
1686f267175SJeff Roberson 		if (va == NULL)
169df8bae1dSRodney W. Grimes 			goto out;
1706f267175SJeff Roberson 
1716f267175SJeff Roberson 		ksp->ks_size |= 1 << indx;
1726f267175SJeff Roberson 		size = zone->uz_size;
1738355f576SJeff Roberson 	} else {
1746f267175SJeff Roberson 		size = roundup(size, PAGE_SIZE);
1758355f576SJeff Roberson 		zone = NULL;
1768355f576SJeff Roberson 		va = uma_large_malloc(size, flags);
1776f267175SJeff Roberson 		if (va == NULL)
1788355f576SJeff Roberson 			goto out;
179df8bae1dSRodney W. Grimes 	}
1806f267175SJeff Roberson 	ksp->ks_memuse += size;
181df8bae1dSRodney W. Grimes 	ksp->ks_inuse++;
1828355f576SJeff Roberson out:
183df8bae1dSRodney W. Grimes 	ksp->ks_calls++;
184df8bae1dSRodney W. Grimes 	if (ksp->ks_memuse > ksp->ks_maxused)
185df8bae1dSRodney W. Grimes 		ksp->ks_maxused = ksp->ks_memuse;
1866f267175SJeff Roberson 
187df8bae1dSRodney W. Grimes 	return ((void *) va);
188df8bae1dSRodney W. Grimes }
189df8bae1dSRodney W. Grimes 
190df8bae1dSRodney W. Grimes /*
1911c7c3c6aSMatthew Dillon  *	free:
1921c7c3c6aSMatthew Dillon  *
193df8bae1dSRodney W. Grimes  *	Free a block of memory allocated by malloc.
1941c7c3c6aSMatthew Dillon  *
1951c7c3c6aSMatthew Dillon  *	This routine may not block.
196df8bae1dSRodney W. Grimes  */
197df8bae1dSRodney W. Grimes void
198df8bae1dSRodney W. Grimes free(addr, type)
199df8bae1dSRodney W. Grimes 	void *addr;
20060a513e9SPoul-Henning Kamp 	struct malloc_type *type;
201df8bae1dSRodney W. Grimes {
2028355f576SJeff Roberson 	uma_slab_t slab;
2038355f576SJeff Roberson 	void *mem;
2048355f576SJeff Roberson 	u_long size;
20560a513e9SPoul-Henning Kamp 	register struct malloc_type *ksp = type;
206254c6cb3SPoul-Henning Kamp 
20744a8ff31SArchie Cobbs 	/* free(NULL, ...) does nothing */
20844a8ff31SArchie Cobbs 	if (addr == NULL)
20944a8ff31SArchie Cobbs 		return;
21044a8ff31SArchie Cobbs 
211708da94eSPoul-Henning Kamp 	if ((u_long)addr & 3) {	/* XXX: Jeff: find better value for 3 */
212708da94eSPoul-Henning Kamp 		printf("free(9)'ing unaligned pointer %p\n", addr);
213708da94eSPoul-Henning Kamp 		Debugger("Don't do that...");
214708da94eSPoul-Henning Kamp 		return;
215708da94eSPoul-Henning Kamp 	}
216708da94eSPoul-Henning Kamp 
2178355f576SJeff Roberson 	size = 0;
21869ef67f9SJason Evans 
2198355f576SJeff Roberson 	mem = (void *)((u_long)addr & (~UMA_SLAB_MASK));
2208355f576SJeff Roberson 	slab = hash_sfind(mallochash, mem);
2218355f576SJeff Roberson 
2228355f576SJeff Roberson 	if (slab == NULL)
2236f267175SJeff Roberson 		panic("free: address %p(%p) has not been allocated.\n",
2246f267175SJeff Roberson 		    addr, mem);
2258355f576SJeff Roberson 
2268355f576SJeff Roberson 	if (!(slab->us_flags & UMA_SLAB_MALLOC)) {
2278355f576SJeff Roberson 		size = slab->us_zone->uz_size;
2288355f576SJeff Roberson 		uma_zfree_arg(slab->us_zone, addr, slab);
22914bf02f8SJohn Dyson 	} else {
2308355f576SJeff Roberson 		size = slab->us_size;
2318355f576SJeff Roberson 		uma_large_free(slab);
23214bf02f8SJohn Dyson 	}
2338355f576SJeff Roberson 	ksp->ks_memuse -= size;
2348355f576SJeff Roberson 	ksp->ks_inuse--;
235df8bae1dSRodney W. Grimes }
236df8bae1dSRodney W. Grimes 
237df8bae1dSRodney W. Grimes /*
23844a8ff31SArchie Cobbs  *	realloc: change the size of a memory block
23944a8ff31SArchie Cobbs  */
24044a8ff31SArchie Cobbs void *
24144a8ff31SArchie Cobbs realloc(addr, size, type, flags)
24244a8ff31SArchie Cobbs 	void *addr;
24344a8ff31SArchie Cobbs 	unsigned long size;
24444a8ff31SArchie Cobbs 	struct malloc_type *type;
24544a8ff31SArchie Cobbs 	int flags;
24644a8ff31SArchie Cobbs {
2478355f576SJeff Roberson 	uma_slab_t slab;
24844a8ff31SArchie Cobbs 	unsigned long alloc;
24944a8ff31SArchie Cobbs 	void *newaddr;
25044a8ff31SArchie Cobbs 
25144a8ff31SArchie Cobbs 	/* realloc(NULL, ...) is equivalent to malloc(...) */
25244a8ff31SArchie Cobbs 	if (addr == NULL)
25344a8ff31SArchie Cobbs 		return (malloc(size, type, flags));
25444a8ff31SArchie Cobbs 
2558355f576SJeff Roberson 	slab = hash_sfind(mallochash,
2568355f576SJeff Roberson 	    (void *)((u_long)addr & ~(UMA_SLAB_MASK)));
2578355f576SJeff Roberson 
25844a8ff31SArchie Cobbs 	/* Sanity check */
2598355f576SJeff Roberson 	KASSERT(slab != NULL,
26044a8ff31SArchie Cobbs 	    ("realloc: address %p out of range", (void *)addr));
26144a8ff31SArchie Cobbs 
26244a8ff31SArchie Cobbs 	/* Get the size of the original block */
2638355f576SJeff Roberson 	if (slab->us_zone)
2648355f576SJeff Roberson 		alloc = slab->us_zone->uz_size;
2658355f576SJeff Roberson 	else
2668355f576SJeff Roberson 		alloc = slab->us_size;
26744a8ff31SArchie Cobbs 
26844a8ff31SArchie Cobbs 	/* Reuse the original block if appropriate */
26944a8ff31SArchie Cobbs 	if (size <= alloc
27044a8ff31SArchie Cobbs 	    && (size > (alloc >> REALLOC_FRACTION) || alloc == MINALLOCSIZE))
27144a8ff31SArchie Cobbs 		return (addr);
27244a8ff31SArchie Cobbs 
27344a8ff31SArchie Cobbs 	/* Allocate a new, bigger (or smaller) block */
27444a8ff31SArchie Cobbs 	if ((newaddr = malloc(size, type, flags)) == NULL)
27544a8ff31SArchie Cobbs 		return (NULL);
27644a8ff31SArchie Cobbs 
27744a8ff31SArchie Cobbs 	/* Copy over original contents */
27844a8ff31SArchie Cobbs 	bcopy(addr, newaddr, min(size, alloc));
27944a8ff31SArchie Cobbs 	free(addr, type);
28044a8ff31SArchie Cobbs 	return (newaddr);
28144a8ff31SArchie Cobbs }
28244a8ff31SArchie Cobbs 
28344a8ff31SArchie Cobbs /*
28444a8ff31SArchie Cobbs  *	reallocf: same as realloc() but free memory on failure.
28544a8ff31SArchie Cobbs  */
28644a8ff31SArchie Cobbs void *
28744a8ff31SArchie Cobbs reallocf(addr, size, type, flags)
28844a8ff31SArchie Cobbs 	void *addr;
28944a8ff31SArchie Cobbs 	unsigned long size;
29044a8ff31SArchie Cobbs 	struct malloc_type *type;
29144a8ff31SArchie Cobbs 	int flags;
29244a8ff31SArchie Cobbs {
29344a8ff31SArchie Cobbs 	void *mem;
29444a8ff31SArchie Cobbs 
29544a8ff31SArchie Cobbs 	if ((mem = realloc(addr, size, type, flags)) == NULL)
29644a8ff31SArchie Cobbs 		free(addr, type);
29744a8ff31SArchie Cobbs 	return (mem);
29844a8ff31SArchie Cobbs }
29944a8ff31SArchie Cobbs 
30044a8ff31SArchie Cobbs /*
301df8bae1dSRodney W. Grimes  * Initialize the kernel memory allocator
302df8bae1dSRodney W. Grimes  */
3032b14f991SJulian Elischer /* ARGSUSED*/
3042b14f991SJulian Elischer static void
305d841aaa7SBruce Evans kmeminit(dummy)
306d841aaa7SBruce Evans 	void *dummy;
307df8bae1dSRodney W. Grimes {
3086f267175SJeff Roberson 	u_int8_t indx;
30927b8623fSDavid Greenman 	u_long npg;
31027b8623fSDavid Greenman 	u_long mem_size;
3118355f576SJeff Roberson 	void *hashmem;
3128355f576SJeff Roberson 	u_long hashsize;
3138355f576SJeff Roberson 	int highbit;
3148355f576SJeff Roberson 	int bits;
3158355f576SJeff Roberson 	int i;
3168a58a9f6SJohn Dyson 
3176008862bSJohn Baldwin 	mtx_init(&malloc_mtx, "malloc", NULL, MTX_DEF);
31869ef67f9SJason Evans 
3198a58a9f6SJohn Dyson 	/*
3208a58a9f6SJohn Dyson 	 * Try to auto-tune the kernel memory size, so that it is
3218a58a9f6SJohn Dyson 	 * more applicable for a wider range of machine sizes.
3228a58a9f6SJohn Dyson 	 * On an X86, a VM_KMEM_SIZE_SCALE value of 4 is good, while
3238a58a9f6SJohn Dyson 	 * a VM_KMEM_SIZE of 12MB is a fair compromise.  The
3248a58a9f6SJohn Dyson 	 * VM_KMEM_SIZE_MAX is dependent on the maximum KVA space
3258a58a9f6SJohn Dyson 	 * available, and on an X86 with a total KVA space of 256MB,
3268a58a9f6SJohn Dyson 	 * try to keep VM_KMEM_SIZE_MAX at 80MB or below.
3278a58a9f6SJohn Dyson 	 *
3288a58a9f6SJohn Dyson 	 * Note that the kmem_map is also used by the zone allocator,
3298a58a9f6SJohn Dyson 	 * so make sure that there is enough space.
3308a58a9f6SJohn Dyson 	 */
33181930014SPeter Wemm 	vm_kmem_size = VM_KMEM_SIZE;
3328a58a9f6SJohn Dyson 	mem_size = cnt.v_page_count * PAGE_SIZE;
3338a58a9f6SJohn Dyson 
3348a58a9f6SJohn Dyson #if defined(VM_KMEM_SIZE_SCALE)
33581930014SPeter Wemm 	if ((mem_size / VM_KMEM_SIZE_SCALE) > vm_kmem_size)
33681930014SPeter Wemm 		vm_kmem_size = mem_size / VM_KMEM_SIZE_SCALE;
3378a58a9f6SJohn Dyson #endif
3388a58a9f6SJohn Dyson 
3398a58a9f6SJohn Dyson #if defined(VM_KMEM_SIZE_MAX)
34081930014SPeter Wemm 	if (vm_kmem_size >= VM_KMEM_SIZE_MAX)
34181930014SPeter Wemm 		vm_kmem_size = VM_KMEM_SIZE_MAX;
3428a58a9f6SJohn Dyson #endif
3438a58a9f6SJohn Dyson 
3448de6e8e1SMike Smith 	/* Allow final override from the kernel environment */
34509786698SPeter Wemm 	TUNABLE_INT_FETCH("kern.vm.kmem.size", &vm_kmem_size);
3468de6e8e1SMike Smith 
34727b8623fSDavid Greenman 	/*
34827b8623fSDavid Greenman 	 * Limit kmem virtual size to twice the physical memory.
34927b8623fSDavid Greenman 	 * This allows for kmem map sparseness, but limits the size
35027b8623fSDavid Greenman 	 * to something sane. Be careful to not overflow the 32bit
35127b8623fSDavid Greenman 	 * ints while doing the check.
35227b8623fSDavid Greenman 	 */
35327b8623fSDavid Greenman 	if ((vm_kmem_size / 2) > (cnt.v_page_count * PAGE_SIZE))
35427b8623fSDavid Greenman 		vm_kmem_size = 2 * cnt.v_page_count * PAGE_SIZE;
3558a58a9f6SJohn Dyson 
35608442f8aSBosko Milekic 	/*
357ba3e8826SBosko Milekic 	 * In mbuf_init(), we set up submaps for mbufs and clusters, in which
35808442f8aSBosko Milekic 	 * case we rounddown() (nmbufs * MSIZE) and (nmbclusters * MCLBYTES),
35908442f8aSBosko Milekic 	 * respectively. Mathematically, this means that what we do here may
36008442f8aSBosko Milekic 	 * amount to slightly more address space than we need for the submaps,
36108442f8aSBosko Milekic 	 * but it never hurts to have an extra page in kmem_map.
36208442f8aSBosko Milekic 	 */
363d04d50d1SBosko Milekic 	npg = (nmbufs * MSIZE + nmbclusters * MCLBYTES + nmbcnt *
36408442f8aSBosko Milekic 	    sizeof(u_int) + vm_kmem_size) / PAGE_SIZE;
3650d94caffSDavid Greenman 
366df8bae1dSRodney W. Grimes 	kmem_map = kmem_suballoc(kernel_map, (vm_offset_t *)&kmembase,
3672d8acc0fSJohn Dyson 		(vm_offset_t *)&kmemlimit, (vm_size_t)(npg * PAGE_SIZE));
3683075778bSJohn Dyson 	kmem_map->system_map = 1;
3698355f576SJeff Roberson 
3708355f576SJeff Roberson 	hashsize = npg * sizeof(void *);
3718355f576SJeff Roberson 
3728355f576SJeff Roberson 	highbit = 0;
3738355f576SJeff Roberson 	bits = 0;
3748355f576SJeff Roberson 	/* The hash size must be a power of two */
3758355f576SJeff Roberson 	for (i = 0; i < 8 * sizeof(hashsize); i++)
3768355f576SJeff Roberson 		if (hashsize & (1 << i)) {
3778355f576SJeff Roberson 			highbit = i;
3788355f576SJeff Roberson 			bits++;
3798355f576SJeff Roberson 		}
3808355f576SJeff Roberson 	if (bits > 1)
3818355f576SJeff Roberson 		hashsize = 1 << (highbit);
3828355f576SJeff Roberson 
3838355f576SJeff Roberson 	hashmem = (void *)kmem_alloc(kernel_map, (vm_size_t)hashsize);
3848355f576SJeff Roberson 	uma_startup2(hashmem, hashsize / sizeof(void *));
3858355f576SJeff Roberson 
3866f267175SJeff Roberson 	for (i = 0, indx = 0; kmemzones[indx].kz_size != 0; indx++) {
3876f267175SJeff Roberson 		int size = kmemzones[indx].kz_size;
3886f267175SJeff Roberson 		char *name = kmemzones[indx].kz_name;
3898355f576SJeff Roberson 
3908efc4effSJeff Roberson 		kmemzones[indx].kz_zone = uma_zcreate(name, size,
3918efc4effSJeff Roberson #ifdef INVARIANTS
3928efc4effSJeff Roberson 		    trash_ctor, trash_dtor, trash_init, trash_fini,
3938efc4effSJeff Roberson #else
3948efc4effSJeff Roberson 		    NULL, NULL, NULL, NULL,
3958efc4effSJeff Roberson #endif
3968efc4effSJeff Roberson 		    UMA_ALIGN_PTR, UMA_ZONE_MALLOC);
3976f267175SJeff Roberson 
3988355f576SJeff Roberson 		for (;i <= size; i+= KMEM_ZBASE)
3996f267175SJeff Roberson 			kmemsize[i >> KMEM_ZSHIFT] = indx;
4008355f576SJeff Roberson 
401df8bae1dSRodney W. Grimes 	}
402254c6cb3SPoul-Henning Kamp }
403254c6cb3SPoul-Henning Kamp 
404db669378SPeter Wemm void
405db669378SPeter Wemm malloc_init(data)
406db669378SPeter Wemm 	void *data;
407254c6cb3SPoul-Henning Kamp {
408db669378SPeter Wemm 	struct malloc_type *type = (struct malloc_type *)data;
409254c6cb3SPoul-Henning Kamp 
4106f267175SJeff Roberson 	mtx_lock(&malloc_mtx);
411d1bbc7ecSPoul-Henning Kamp 	if (type->ks_magic != M_MAGIC)
412d1bbc7ecSPoul-Henning Kamp 		panic("malloc type lacks magic");
413d1bbc7ecSPoul-Henning Kamp 
414d4060a87SJohn Dyson 	if (cnt.v_page_count == 0)
415d4060a87SJohn Dyson 		panic("malloc_init not allowed before vm init");
416d4060a87SJohn Dyson 
4176f267175SJeff Roberson 	if (type->ks_next != NULL)
4186f267175SJeff Roberson 		return;
4196f267175SJeff Roberson 
420254c6cb3SPoul-Henning Kamp 	type->ks_next = kmemstatistics;
421254c6cb3SPoul-Henning Kamp 	kmemstatistics = type;
4226f267175SJeff Roberson 	mtx_unlock(&malloc_mtx);
423df8bae1dSRodney W. Grimes }
424db669378SPeter Wemm 
425db669378SPeter Wemm void
426db669378SPeter Wemm malloc_uninit(data)
427db669378SPeter Wemm 	void *data;
428db669378SPeter Wemm {
429db669378SPeter Wemm 	struct malloc_type *type = (struct malloc_type *)data;
430db669378SPeter Wemm 	struct malloc_type *t;
431db669378SPeter Wemm 
4326f267175SJeff Roberson 	mtx_lock(&malloc_mtx);
433db669378SPeter Wemm 	if (type->ks_magic != M_MAGIC)
434db669378SPeter Wemm 		panic("malloc type lacks magic");
435db669378SPeter Wemm 
436db669378SPeter Wemm 	if (cnt.v_page_count == 0)
437db669378SPeter Wemm 		panic("malloc_uninit not allowed before vm init");
438db669378SPeter Wemm 
439db669378SPeter Wemm 	if (type == kmemstatistics)
440db669378SPeter Wemm 		kmemstatistics = type->ks_next;
441db669378SPeter Wemm 	else {
442db669378SPeter Wemm 		for (t = kmemstatistics; t->ks_next != NULL; t = t->ks_next) {
443db669378SPeter Wemm 			if (t->ks_next == type) {
444db669378SPeter Wemm 				t->ks_next = type->ks_next;
445db669378SPeter Wemm 				break;
446db669378SPeter Wemm 			}
447db669378SPeter Wemm 		}
448db669378SPeter Wemm 	}
449ce45b512SBruce Evans 	type->ks_next = NULL;
4506f267175SJeff Roberson 	mtx_unlock(&malloc_mtx);
451db669378SPeter Wemm }
4526f267175SJeff Roberson 
4536f267175SJeff Roberson static int
4546f267175SJeff Roberson sysctl_kern_malloc(SYSCTL_HANDLER_ARGS)
4556f267175SJeff Roberson {
4566f267175SJeff Roberson 	struct malloc_type *type;
4576f267175SJeff Roberson 	int linesize = 128;
4586f267175SJeff Roberson 	int curline;
4596f267175SJeff Roberson 	int bufsize;
4606f267175SJeff Roberson 	int first;
4616f267175SJeff Roberson 	int error;
4626f267175SJeff Roberson 	char *buf;
4636f267175SJeff Roberson 	char *p;
4646f267175SJeff Roberson 	int cnt;
4656f267175SJeff Roberson 	int len;
4666f267175SJeff Roberson 	int i;
4676f267175SJeff Roberson 
4686f267175SJeff Roberson 	cnt = 0;
4696f267175SJeff Roberson 
4706f267175SJeff Roberson 	mtx_lock(&malloc_mtx);
4716f267175SJeff Roberson 	for (type = kmemstatistics; type != NULL; type = type->ks_next)
4726f267175SJeff Roberson 		cnt++;
4736f267175SJeff Roberson 
4746f267175SJeff Roberson 	bufsize = linesize * (cnt + 1);
4756f267175SJeff Roberson 	p = buf = (char *)malloc(bufsize, M_TEMP, M_WAITOK|M_ZERO);
4766f267175SJeff Roberson 
4776f267175SJeff Roberson 	len = snprintf(p, linesize,
4786f267175SJeff Roberson 	    "\n        Type  InUse MemUse HighUse Requests  Size(s)\n");
4796f267175SJeff Roberson 	p += len;
4806f267175SJeff Roberson 
4816f267175SJeff Roberson 	for (type = kmemstatistics; cnt != 0 && type != NULL;
4826f267175SJeff Roberson 	    type = type->ks_next, cnt--) {
4836f267175SJeff Roberson 		if (type->ks_calls == 0)
4846f267175SJeff Roberson 			continue;
4856f267175SJeff Roberson 
4866f267175SJeff Roberson 		curline = linesize - 2;	/* Leave room for the \n */
4876f267175SJeff Roberson 		len = snprintf(p, curline, "%13s%6ld%6ldK%7ldK%9llu",
4886f267175SJeff Roberson 			type->ks_shortdesc,
4896f267175SJeff Roberson 			type->ks_inuse,
4906f267175SJeff Roberson 			(type->ks_memuse + 1023) / 1024,
4916f267175SJeff Roberson 			(type->ks_maxused + 1023) / 1024,
4926f267175SJeff Roberson 			(long long unsigned)type->ks_calls);
4936f267175SJeff Roberson 		curline -= len;
4946f267175SJeff Roberson 		p += len;
4956f267175SJeff Roberson 
4966f267175SJeff Roberson 		first = 1;
4975e914b96SJeff Roberson 		for (i = 0; i < 8 * sizeof(type->ks_size); i++)
4986f267175SJeff Roberson 			if (type->ks_size & (1 << i)) {
4996f267175SJeff Roberson 				if (first)
5006f267175SJeff Roberson 					len = snprintf(p, curline, "  ");
5016f267175SJeff Roberson 				else
5026f267175SJeff Roberson 					len = snprintf(p, curline, ",");
5036f267175SJeff Roberson 				curline -= len;
5046f267175SJeff Roberson 				p += len;
5056f267175SJeff Roberson 
5066f267175SJeff Roberson 				len = snprintf(p, curline,
5076f267175SJeff Roberson 				    "%s", kmemzones[i].kz_name);
5086f267175SJeff Roberson 				curline -= len;
5096f267175SJeff Roberson 				p += len;
5106f267175SJeff Roberson 
5116f267175SJeff Roberson 				first = 0;
5126f267175SJeff Roberson 			}
5136f267175SJeff Roberson 
5146f267175SJeff Roberson 		len = snprintf(p, 2, "\n");
5156f267175SJeff Roberson 		p += len;
5166f267175SJeff Roberson 	}
5176f267175SJeff Roberson 
5186f267175SJeff Roberson 	mtx_unlock(&malloc_mtx);
5196f267175SJeff Roberson 	error = SYSCTL_OUT(req, buf, p - buf);
5206f267175SJeff Roberson 
5216f267175SJeff Roberson 	free(buf, M_TEMP);
5226f267175SJeff Roberson 	return (error);
5236f267175SJeff Roberson }
5246f267175SJeff Roberson 
5256f267175SJeff Roberson SYSCTL_OID(_kern, OID_AUTO, malloc, CTLTYPE_STRING|CTLFLAG_RD,
5266f267175SJeff Roberson     NULL, 0, sysctl_kern_malloc, "A", "Malloc Stats");
5275e914b96SJeff Roberson 
5285e914b96SJeff Roberson #ifdef MALLOC_PROFILE
5295e914b96SJeff Roberson 
5305e914b96SJeff Roberson static int
5315e914b96SJeff Roberson sysctl_kern_mprof(SYSCTL_HANDLER_ARGS)
5325e914b96SJeff Roberson {
5335e914b96SJeff Roberson 	int linesize = 64;
5345e914b96SJeff Roberson 	uint64_t count;
5355e914b96SJeff Roberson 	uint64_t waste;
5365e914b96SJeff Roberson 	uint64_t mem;
5375e914b96SJeff Roberson 	int bufsize;
5385e914b96SJeff Roberson 	int error;
5395e914b96SJeff Roberson 	char *buf;
5405e914b96SJeff Roberson 	int rsize;
5415e914b96SJeff Roberson 	int size;
5425e914b96SJeff Roberson 	char *p;
5435e914b96SJeff Roberson 	int len;
5445e914b96SJeff Roberson 	int i;
5455e914b96SJeff Roberson 
5465e914b96SJeff Roberson 	bufsize = linesize * (KMEM_ZSIZE + 1);
5475e914b96SJeff Roberson 	bufsize += 128; 	/* For the stats line */
5485e914b96SJeff Roberson 	bufsize += 128; 	/* For the banner line */
5495e914b96SJeff Roberson 	waste = 0;
5505e914b96SJeff Roberson 	mem = 0;
5515e914b96SJeff Roberson 
5525e914b96SJeff Roberson 	p = buf = (char *)malloc(bufsize, M_TEMP, M_WAITOK|M_ZERO);
5535e914b96SJeff Roberson 	len = snprintf(p, bufsize,
5545e914b96SJeff Roberson 	    "\n  Size                    Requests  Real Size\n");
5555e914b96SJeff Roberson 	bufsize -= len;
5565e914b96SJeff Roberson 	p += len;
5575e914b96SJeff Roberson 
5585e914b96SJeff Roberson 	for (i = 0; i < KMEM_ZSIZE; i++) {
5595e914b96SJeff Roberson 		size = i << KMEM_ZSHIFT;
5605e914b96SJeff Roberson 		rsize = kmemzones[kmemsize[i]].kz_size;
5615e914b96SJeff Roberson 		count = (long long unsigned)krequests[i];
5625e914b96SJeff Roberson 
5635e914b96SJeff Roberson 		len = snprintf(p, bufsize, "%6d%28llu%11d\n",
5645e914b96SJeff Roberson 		    size, (unsigned long long)count, rsize);
5655e914b96SJeff Roberson 		bufsize -= len;
5665e914b96SJeff Roberson 		p += len;
5675e914b96SJeff Roberson 
5685e914b96SJeff Roberson 		if ((rsize * count) > (size * count))
5695e914b96SJeff Roberson 			waste += (rsize * count) - (size * count);
5705e914b96SJeff Roberson 		mem += (rsize * count);
5715e914b96SJeff Roberson 	}
5725e914b96SJeff Roberson 
5735e914b96SJeff Roberson 	len = snprintf(p, bufsize,
5745e914b96SJeff Roberson 	    "\nTotal memory used:\t%30llu\nTotal Memory wasted:\t%30llu\n",
5755e914b96SJeff Roberson 	    (unsigned long long)mem, (unsigned long long)waste);
5765e914b96SJeff Roberson 	p += len;
5775e914b96SJeff Roberson 
5785e914b96SJeff Roberson 	error = SYSCTL_OUT(req, buf, p - buf);
5795e914b96SJeff Roberson 
5805e914b96SJeff Roberson 	free(buf, M_TEMP);
5815e914b96SJeff Roberson 	return (error);
5825e914b96SJeff Roberson }
5835e914b96SJeff Roberson 
5845e914b96SJeff Roberson SYSCTL_OID(_kern, OID_AUTO, mprof, CTLTYPE_STRING|CTLFLAG_RD,
5855e914b96SJeff Roberson     NULL, 0, sysctl_kern_mprof, "A", "Malloc Profiling");
5865e914b96SJeff Roberson #endif /* MALLOC_PROFILE */
587