xref: /freebsd/sys/kern/kern_malloc.c (revision 4db4f5c87f179ef1402f65026e5c457d812c4b97)
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>
491fb14a47SPoul-Henning Kamp #include <sys/time.h>
509a02e8c6SJason Evans 
51df8bae1dSRodney W. Grimes #include <vm/vm.h>
5299571dc3SJeff Roberson #include <vm/pmap.h>
53efeaf95aSDavid Greenman #include <vm/vm_param.h>
54df8bae1dSRodney W. Grimes #include <vm/vm_kern.h>
55efeaf95aSDavid Greenman #include <vm/vm_extern.h>
563075778bSJohn Dyson #include <vm/vm_map.h>
5799571dc3SJeff Roberson #include <vm/vm_page.h>
588355f576SJeff Roberson #include <vm/uma.h>
598355f576SJeff Roberson #include <vm/uma_int.h>
608efc4effSJeff Roberson #include <vm/uma_dbg.h>
61df8bae1dSRodney W. Grimes 
62984982d6SPoul-Henning Kamp #if defined(INVARIANTS) && defined(__i386__)
63984982d6SPoul-Henning Kamp #include <machine/cpu.h>
64984982d6SPoul-Henning Kamp #endif
65984982d6SPoul-Henning Kamp 
6644a8ff31SArchie Cobbs /*
6744a8ff31SArchie Cobbs  * When realloc() is called, if the new size is sufficiently smaller than
6844a8ff31SArchie Cobbs  * the old size, realloc() will allocate a new, smaller block to avoid
6944a8ff31SArchie Cobbs  * wasting memory. 'Sufficiently smaller' is defined as: newsize <=
7044a8ff31SArchie Cobbs  * oldsize / 2^n, where REALLOC_FRACTION defines the value of 'n'.
7144a8ff31SArchie Cobbs  */
7244a8ff31SArchie Cobbs #ifndef REALLOC_FRACTION
7344a8ff31SArchie Cobbs #define	REALLOC_FRACTION	1	/* new block if <= half the size */
7444a8ff31SArchie Cobbs #endif
7544a8ff31SArchie Cobbs 
763b6fb885SPoul-Henning Kamp MALLOC_DEFINE(M_CACHE, "cache", "Various Dynamically allocated caches");
779ef246c6SBruce Evans MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory");
789ef246c6SBruce Evans MALLOC_DEFINE(M_TEMP, "temp", "misc temporary data buffers");
799ef246c6SBruce Evans 
8082cd038dSYoshinobu Inoue MALLOC_DEFINE(M_IP6OPT, "ip6opt", "IPv6 options");
8182cd038dSYoshinobu Inoue MALLOC_DEFINE(M_IP6NDP, "ip6ndp", "IPv6 Neighbor Discovery");
8282cd038dSYoshinobu Inoue 
834d77a549SAlfred Perlstein static void kmeminit(void *);
842b14f991SJulian Elischer SYSINIT(kmem, SI_SUB_KMEM, SI_ORDER_FIRST, kmeminit, NULL)
852b14f991SJulian Elischer 
86a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_FREE, "free", "should be on free list");
87a1c995b6SPoul-Henning Kamp 
88db669378SPeter Wemm static struct malloc_type *kmemstatistics;
89254c6cb3SPoul-Henning Kamp static char *kmembase;
90043a2f3bSBruce Evans static char *kmemlimit;
911f6889a1SMatthew Dillon 
928355f576SJeff Roberson #define KMEM_ZSHIFT	4
938355f576SJeff Roberson #define KMEM_ZBASE	16
948355f576SJeff Roberson #define KMEM_ZMASK	(KMEM_ZBASE - 1)
958355f576SJeff Roberson 
96bd796eb2SRobert Watson #define KMEM_ZMAX	65536
978355f576SJeff Roberson #define KMEM_ZSIZE	(KMEM_ZMAX >> KMEM_ZSHIFT)
986f267175SJeff Roberson static u_int8_t kmemsize[KMEM_ZSIZE + 1];
996f267175SJeff Roberson 
1008355f576SJeff Roberson /* These won't be powers of two for long */
1018355f576SJeff Roberson struct {
1026f267175SJeff Roberson 	int kz_size;
1036f267175SJeff Roberson 	char *kz_name;
1046f267175SJeff Roberson 	uma_zone_t kz_zone;
1056f267175SJeff Roberson } kmemzones[] = {
1066f267175SJeff Roberson 	{16, "16", NULL},
1076f267175SJeff Roberson 	{32, "32", NULL},
1086f267175SJeff Roberson 	{64, "64", NULL},
1096f267175SJeff Roberson 	{128, "128", NULL},
1106f267175SJeff Roberson 	{256, "256", NULL},
1116f267175SJeff Roberson 	{512, "512", NULL},
1126f267175SJeff Roberson 	{1024, "1024", NULL},
1136f267175SJeff Roberson 	{2048, "2048", NULL},
1146f267175SJeff Roberson 	{4096, "4096", NULL},
1156f267175SJeff Roberson 	{8192, "8192", NULL},
11643a7c4e9SRobert Watson 	{16384, "16384", NULL},
117bd796eb2SRobert Watson 	{32768, "32768", NULL},
118bd796eb2SRobert Watson 	{65536, "65536", NULL},
1198355f576SJeff Roberson 	{0, NULL},
1208355f576SJeff Roberson };
1218355f576SJeff Roberson 
1226f267175SJeff Roberson u_int vm_kmem_size;
1235a34a9f0SJeff Roberson 
1245a34a9f0SJeff Roberson /*
12599571dc3SJeff Roberson  * The malloc_mtx protects the kmemstatistics linked list.
1265a34a9f0SJeff Roberson  */
1275a34a9f0SJeff Roberson 
1285a34a9f0SJeff Roberson struct mtx malloc_mtx;
12969ef67f9SJason Evans 
1305e914b96SJeff Roberson #ifdef MALLOC_PROFILE
1315e914b96SJeff Roberson uint64_t krequests[KMEM_ZSIZE + 1];
1326f267175SJeff Roberson 
1335e914b96SJeff Roberson static int sysctl_kern_mprof(SYSCTL_HANDLER_ARGS);
1345e914b96SJeff Roberson #endif
1355e914b96SJeff Roberson 
1365e914b96SJeff Roberson static int sysctl_kern_malloc(SYSCTL_HANDLER_ARGS);
137df8bae1dSRodney W. Grimes 
1381fb14a47SPoul-Henning Kamp /* time_uptime of last malloc(9) failure */
1391fb14a47SPoul-Henning Kamp static time_t t_malloc_fail;
1401fb14a47SPoul-Henning Kamp 
1411fb14a47SPoul-Henning Kamp int
1421fb14a47SPoul-Henning Kamp malloc_last_fail(void)
1431fb14a47SPoul-Henning Kamp {
1441fb14a47SPoul-Henning Kamp 
1451fb14a47SPoul-Henning Kamp 	return (time_uptime - t_malloc_fail);
1461fb14a47SPoul-Henning Kamp }
1471fb14a47SPoul-Henning Kamp 
148df8bae1dSRodney W. Grimes /*
1491c7c3c6aSMatthew Dillon  *	malloc:
1501c7c3c6aSMatthew Dillon  *
1511c7c3c6aSMatthew Dillon  *	Allocate a block of memory.
1521c7c3c6aSMatthew Dillon  *
1531c7c3c6aSMatthew Dillon  *	If M_NOWAIT is set, this routine will not block and return NULL if
1541c7c3c6aSMatthew Dillon  *	the allocation fails.
155df8bae1dSRodney W. Grimes  */
156df8bae1dSRodney W. Grimes void *
157df8bae1dSRodney W. Grimes malloc(size, type, flags)
158df8bae1dSRodney W. Grimes 	unsigned long size;
15960a513e9SPoul-Henning Kamp 	struct malloc_type *type;
160254c6cb3SPoul-Henning Kamp 	int flags;
161df8bae1dSRodney W. Grimes {
1626f267175SJeff Roberson 	int indx;
1638355f576SJeff Roberson 	caddr_t va;
1648355f576SJeff Roberson 	uma_zone_t zone;
1654db4f5c8SPoul-Henning Kamp #ifdef DIAGNOSTIC
1664db4f5c8SPoul-Henning Kamp 	unsigned long osize = size;
1674db4f5c8SPoul-Henning Kamp #endif
16860a513e9SPoul-Henning Kamp 	register struct malloc_type *ksp = type;
169df8bae1dSRodney W. Grimes 
170708da94eSPoul-Henning Kamp #if 0
171708da94eSPoul-Henning Kamp 	if (size == 0)
172708da94eSPoul-Henning Kamp 		Debugger("zero size malloc");
173708da94eSPoul-Henning Kamp #endif
17442e49865SJohn Baldwin 	if (!(flags & M_NOWAIT))
175b40ce416SJulian Elischer 		KASSERT(curthread->td_intr_nesting_level == 0,
17644956c98SAlfred Perlstein 		   ("malloc() without M_NOWAIT in interrupt context"));
1778355f576SJeff Roberson 	if (size <= KMEM_ZMAX) {
1786f267175SJeff Roberson 		if (size & KMEM_ZMASK)
1796f267175SJeff Roberson 			size = (size & ~KMEM_ZMASK) + KMEM_ZBASE;
1806f267175SJeff Roberson 		indx = kmemsize[size >> KMEM_ZSHIFT];
1816f267175SJeff Roberson 		zone = kmemzones[indx].kz_zone;
1826f267175SJeff Roberson #ifdef MALLOC_PROFILE
1836f267175SJeff Roberson 		krequests[size >> KMEM_ZSHIFT]++;
1846f267175SJeff Roberson #endif
1858355f576SJeff Roberson 		va = uma_zalloc(zone, flags);
1865a34a9f0SJeff Roberson 		mtx_lock(&ksp->ks_mtx);
1876f267175SJeff Roberson 		if (va == NULL)
188df8bae1dSRodney W. Grimes 			goto out;
1896f267175SJeff Roberson 
1906f267175SJeff Roberson 		ksp->ks_size |= 1 << indx;
1916f267175SJeff Roberson 		size = zone->uz_size;
1928355f576SJeff Roberson 	} else {
1936f267175SJeff Roberson 		size = roundup(size, PAGE_SIZE);
1948355f576SJeff Roberson 		zone = NULL;
1958355f576SJeff Roberson 		va = uma_large_malloc(size, flags);
1965a34a9f0SJeff Roberson 		mtx_lock(&ksp->ks_mtx);
1976f267175SJeff Roberson 		if (va == NULL)
1988355f576SJeff Roberson 			goto out;
199df8bae1dSRodney W. Grimes 	}
2006f267175SJeff Roberson 	ksp->ks_memuse += size;
201df8bae1dSRodney W. Grimes 	ksp->ks_inuse++;
2028355f576SJeff Roberson out:
203df8bae1dSRodney W. Grimes 	ksp->ks_calls++;
204df8bae1dSRodney W. Grimes 	if (ksp->ks_memuse > ksp->ks_maxused)
205df8bae1dSRodney W. Grimes 		ksp->ks_maxused = ksp->ks_memuse;
2066f267175SJeff Roberson 
2075a34a9f0SJeff Roberson 	mtx_unlock(&ksp->ks_mtx);
2081fb14a47SPoul-Henning Kamp 	if (!(flags & M_NOWAIT))
20944956c98SAlfred Perlstein 		KASSERT(va != NULL, ("malloc() without M_NOWAIT returned NULL"));
2101fb14a47SPoul-Henning Kamp 	if (va == NULL) {
2111fb14a47SPoul-Henning Kamp 		t_malloc_fail = time_uptime;
2121fb14a47SPoul-Henning Kamp 	}
2134db4f5c8SPoul-Henning Kamp #ifdef DIAGNOSTIC
2144db4f5c8SPoul-Henning Kamp 	if (!(flags & M_ZERO)) {
2154db4f5c8SPoul-Henning Kamp 		memset(va, 0x70, osize);
2164db4f5c8SPoul-Henning Kamp 	}
2174db4f5c8SPoul-Henning Kamp #endif
218df8bae1dSRodney W. Grimes 	return ((void *) va);
219df8bae1dSRodney W. Grimes }
220df8bae1dSRodney W. Grimes 
221df8bae1dSRodney W. Grimes /*
2221c7c3c6aSMatthew Dillon  *	free:
2231c7c3c6aSMatthew Dillon  *
224df8bae1dSRodney W. Grimes  *	Free a block of memory allocated by malloc.
2251c7c3c6aSMatthew Dillon  *
2261c7c3c6aSMatthew Dillon  *	This routine may not block.
227df8bae1dSRodney W. Grimes  */
228df8bae1dSRodney W. Grimes void
229df8bae1dSRodney W. Grimes free(addr, type)
230df8bae1dSRodney W. Grimes 	void *addr;
23160a513e9SPoul-Henning Kamp 	struct malloc_type *type;
232df8bae1dSRodney W. Grimes {
23360a513e9SPoul-Henning Kamp 	register struct malloc_type *ksp = type;
23499571dc3SJeff Roberson 	uma_slab_t slab;
23599571dc3SJeff Roberson 	u_long size;
236254c6cb3SPoul-Henning Kamp 
23744a8ff31SArchie Cobbs 	/* free(NULL, ...) does nothing */
23844a8ff31SArchie Cobbs 	if (addr == NULL)
23944a8ff31SArchie Cobbs 		return;
24044a8ff31SArchie Cobbs 
2418355f576SJeff Roberson 	size = 0;
24269ef67f9SJason Evans 
24399571dc3SJeff Roberson 	slab = vtoslab((vm_offset_t)addr & (~UMA_SLAB_MASK));
2448355f576SJeff Roberson 
2458355f576SJeff Roberson 	if (slab == NULL)
2466f267175SJeff Roberson 		panic("free: address %p(%p) has not been allocated.\n",
24799571dc3SJeff Roberson 		    addr, (void *)((u_long)addr & (~UMA_SLAB_MASK)));
24899571dc3SJeff Roberson 
2498355f576SJeff Roberson 
2508355f576SJeff Roberson 	if (!(slab->us_flags & UMA_SLAB_MALLOC)) {
2518f70816cSJeff Roberson #ifdef INVARIANTS
2528f70816cSJeff Roberson 		struct malloc_type **mtp = addr;
2538f70816cSJeff Roberson #endif
2548355f576SJeff Roberson 		size = slab->us_zone->uz_size;
2558f70816cSJeff Roberson #ifdef INVARIANTS
2568f70816cSJeff Roberson 		/*
2578f70816cSJeff Roberson 		 * Cache a pointer to the malloc_type that most recently freed
2588f70816cSJeff Roberson 		 * this memory here.  This way we know who is most likely to
2598f70816cSJeff Roberson 		 * have stepped on it later.
2608f70816cSJeff Roberson 		 *
2618f70816cSJeff Roberson 		 * This code assumes that size is a multiple of 8 bytes for
2628f70816cSJeff Roberson 		 * 64 bit machines
2638f70816cSJeff Roberson 		 */
2648f70816cSJeff Roberson 		mtp = (struct malloc_type **)
2658f70816cSJeff Roberson 		    ((unsigned long)mtp & ~UMA_ALIGN_PTR);
2668f70816cSJeff Roberson 		mtp += (size - sizeof(struct malloc_type *)) /
2678f70816cSJeff Roberson 		    sizeof(struct malloc_type *);
2688f70816cSJeff Roberson 		*mtp = type;
2698f70816cSJeff Roberson #endif
2708355f576SJeff Roberson 		uma_zfree_arg(slab->us_zone, addr, slab);
27114bf02f8SJohn Dyson 	} else {
2728355f576SJeff Roberson 		size = slab->us_size;
2738355f576SJeff Roberson 		uma_large_free(slab);
27414bf02f8SJohn Dyson 	}
2755a34a9f0SJeff Roberson 	mtx_lock(&ksp->ks_mtx);
2768355f576SJeff Roberson 	ksp->ks_memuse -= size;
2778355f576SJeff Roberson 	ksp->ks_inuse--;
2785a34a9f0SJeff Roberson 	mtx_unlock(&ksp->ks_mtx);
279df8bae1dSRodney W. Grimes }
280df8bae1dSRodney W. Grimes 
281df8bae1dSRodney W. Grimes /*
28244a8ff31SArchie Cobbs  *	realloc: change the size of a memory block
28344a8ff31SArchie Cobbs  */
28444a8ff31SArchie Cobbs void *
28544a8ff31SArchie Cobbs realloc(addr, size, type, flags)
28644a8ff31SArchie Cobbs 	void *addr;
28744a8ff31SArchie Cobbs 	unsigned long size;
28844a8ff31SArchie Cobbs 	struct malloc_type *type;
28944a8ff31SArchie Cobbs 	int flags;
29044a8ff31SArchie Cobbs {
2918355f576SJeff Roberson 	uma_slab_t slab;
29244a8ff31SArchie Cobbs 	unsigned long alloc;
29344a8ff31SArchie Cobbs 	void *newaddr;
29444a8ff31SArchie Cobbs 
29544a8ff31SArchie Cobbs 	/* realloc(NULL, ...) is equivalent to malloc(...) */
29644a8ff31SArchie Cobbs 	if (addr == NULL)
29744a8ff31SArchie Cobbs 		return (malloc(size, type, flags));
29844a8ff31SArchie Cobbs 
29999571dc3SJeff Roberson 	slab = vtoslab((vm_offset_t)addr & ~(UMA_SLAB_MASK));
3008355f576SJeff Roberson 
30144a8ff31SArchie Cobbs 	/* Sanity check */
3028355f576SJeff Roberson 	KASSERT(slab != NULL,
30344a8ff31SArchie Cobbs 	    ("realloc: address %p out of range", (void *)addr));
30444a8ff31SArchie Cobbs 
30544a8ff31SArchie Cobbs 	/* Get the size of the original block */
3068355f576SJeff Roberson 	if (slab->us_zone)
3078355f576SJeff Roberson 		alloc = slab->us_zone->uz_size;
3088355f576SJeff Roberson 	else
3098355f576SJeff Roberson 		alloc = slab->us_size;
31044a8ff31SArchie Cobbs 
31144a8ff31SArchie Cobbs 	/* Reuse the original block if appropriate */
31244a8ff31SArchie Cobbs 	if (size <= alloc
31344a8ff31SArchie Cobbs 	    && (size > (alloc >> REALLOC_FRACTION) || alloc == MINALLOCSIZE))
31444a8ff31SArchie Cobbs 		return (addr);
31544a8ff31SArchie Cobbs 
31644a8ff31SArchie Cobbs 	/* Allocate a new, bigger (or smaller) block */
31744a8ff31SArchie Cobbs 	if ((newaddr = malloc(size, type, flags)) == NULL)
31844a8ff31SArchie Cobbs 		return (NULL);
31944a8ff31SArchie Cobbs 
32044a8ff31SArchie Cobbs 	/* Copy over original contents */
32144a8ff31SArchie Cobbs 	bcopy(addr, newaddr, min(size, alloc));
32244a8ff31SArchie Cobbs 	free(addr, type);
32344a8ff31SArchie Cobbs 	return (newaddr);
32444a8ff31SArchie Cobbs }
32544a8ff31SArchie Cobbs 
32644a8ff31SArchie Cobbs /*
32744a8ff31SArchie Cobbs  *	reallocf: same as realloc() but free memory on failure.
32844a8ff31SArchie Cobbs  */
32944a8ff31SArchie Cobbs void *
33044a8ff31SArchie Cobbs reallocf(addr, size, type, flags)
33144a8ff31SArchie Cobbs 	void *addr;
33244a8ff31SArchie Cobbs 	unsigned long size;
33344a8ff31SArchie Cobbs 	struct malloc_type *type;
33444a8ff31SArchie Cobbs 	int flags;
33544a8ff31SArchie Cobbs {
33644a8ff31SArchie Cobbs 	void *mem;
33744a8ff31SArchie Cobbs 
33844a8ff31SArchie Cobbs 	if ((mem = realloc(addr, size, type, flags)) == NULL)
33944a8ff31SArchie Cobbs 		free(addr, type);
34044a8ff31SArchie Cobbs 	return (mem);
34144a8ff31SArchie Cobbs }
34244a8ff31SArchie Cobbs 
34344a8ff31SArchie Cobbs /*
344df8bae1dSRodney W. Grimes  * Initialize the kernel memory allocator
345df8bae1dSRodney W. Grimes  */
3462b14f991SJulian Elischer /* ARGSUSED*/
3472b14f991SJulian Elischer static void
348d841aaa7SBruce Evans kmeminit(dummy)
349d841aaa7SBruce Evans 	void *dummy;
350df8bae1dSRodney W. Grimes {
3516f267175SJeff Roberson 	u_int8_t indx;
35227b8623fSDavid Greenman 	u_long npg;
35327b8623fSDavid Greenman 	u_long mem_size;
3548355f576SJeff Roberson 	int i;
3558a58a9f6SJohn Dyson 
3566008862bSJohn Baldwin 	mtx_init(&malloc_mtx, "malloc", NULL, MTX_DEF);
35769ef67f9SJason Evans 
3588a58a9f6SJohn Dyson 	/*
3598a58a9f6SJohn Dyson 	 * Try to auto-tune the kernel memory size, so that it is
3608a58a9f6SJohn Dyson 	 * more applicable for a wider range of machine sizes.
3618a58a9f6SJohn Dyson 	 * On an X86, a VM_KMEM_SIZE_SCALE value of 4 is good, while
3628a58a9f6SJohn Dyson 	 * a VM_KMEM_SIZE of 12MB is a fair compromise.  The
3638a58a9f6SJohn Dyson 	 * VM_KMEM_SIZE_MAX is dependent on the maximum KVA space
3648a58a9f6SJohn Dyson 	 * available, and on an X86 with a total KVA space of 256MB,
3658a58a9f6SJohn Dyson 	 * try to keep VM_KMEM_SIZE_MAX at 80MB or below.
3668a58a9f6SJohn Dyson 	 *
3678a58a9f6SJohn Dyson 	 * Note that the kmem_map is also used by the zone allocator,
3688a58a9f6SJohn Dyson 	 * so make sure that there is enough space.
3698a58a9f6SJohn Dyson 	 */
37081930014SPeter Wemm 	vm_kmem_size = VM_KMEM_SIZE;
3718a58a9f6SJohn Dyson 	mem_size = cnt.v_page_count * PAGE_SIZE;
3728a58a9f6SJohn Dyson 
3738a58a9f6SJohn Dyson #if defined(VM_KMEM_SIZE_SCALE)
37481930014SPeter Wemm 	if ((mem_size / VM_KMEM_SIZE_SCALE) > vm_kmem_size)
37581930014SPeter Wemm 		vm_kmem_size = mem_size / VM_KMEM_SIZE_SCALE;
3768a58a9f6SJohn Dyson #endif
3778a58a9f6SJohn Dyson 
3788a58a9f6SJohn Dyson #if defined(VM_KMEM_SIZE_MAX)
37981930014SPeter Wemm 	if (vm_kmem_size >= VM_KMEM_SIZE_MAX)
38081930014SPeter Wemm 		vm_kmem_size = VM_KMEM_SIZE_MAX;
3818a58a9f6SJohn Dyson #endif
3828a58a9f6SJohn Dyson 
3838de6e8e1SMike Smith 	/* Allow final override from the kernel environment */
38409786698SPeter Wemm 	TUNABLE_INT_FETCH("kern.vm.kmem.size", &vm_kmem_size);
3858de6e8e1SMike Smith 
38627b8623fSDavid Greenman 	/*
38727b8623fSDavid Greenman 	 * Limit kmem virtual size to twice the physical memory.
38827b8623fSDavid Greenman 	 * This allows for kmem map sparseness, but limits the size
38927b8623fSDavid Greenman 	 * to something sane. Be careful to not overflow the 32bit
39027b8623fSDavid Greenman 	 * ints while doing the check.
39127b8623fSDavid Greenman 	 */
39227b8623fSDavid Greenman 	if ((vm_kmem_size / 2) > (cnt.v_page_count * PAGE_SIZE))
39327b8623fSDavid Greenman 		vm_kmem_size = 2 * cnt.v_page_count * PAGE_SIZE;
3948a58a9f6SJohn Dyson 
39508442f8aSBosko Milekic 	/*
396ba3e8826SBosko Milekic 	 * In mbuf_init(), we set up submaps for mbufs and clusters, in which
39708442f8aSBosko Milekic 	 * case we rounddown() (nmbufs * MSIZE) and (nmbclusters * MCLBYTES),
39808442f8aSBosko Milekic 	 * respectively. Mathematically, this means that what we do here may
39908442f8aSBosko Milekic 	 * amount to slightly more address space than we need for the submaps,
40008442f8aSBosko Milekic 	 * but it never hurts to have an extra page in kmem_map.
40108442f8aSBosko Milekic 	 */
402d04d50d1SBosko Milekic 	npg = (nmbufs * MSIZE + nmbclusters * MCLBYTES + nmbcnt *
40308442f8aSBosko Milekic 	    sizeof(u_int) + vm_kmem_size) / PAGE_SIZE;
4040d94caffSDavid Greenman 
405df8bae1dSRodney W. Grimes 	kmem_map = kmem_suballoc(kernel_map, (vm_offset_t *)&kmembase,
4062d8acc0fSJohn Dyson 		(vm_offset_t *)&kmemlimit, (vm_size_t)(npg * PAGE_SIZE));
4073075778bSJohn Dyson 	kmem_map->system_map = 1;
4088355f576SJeff Roberson 
40999571dc3SJeff Roberson 	uma_startup2();
4108355f576SJeff Roberson 
4116f267175SJeff Roberson 	for (i = 0, indx = 0; kmemzones[indx].kz_size != 0; indx++) {
4126f267175SJeff Roberson 		int size = kmemzones[indx].kz_size;
4136f267175SJeff Roberson 		char *name = kmemzones[indx].kz_name;
4148355f576SJeff Roberson 
4158efc4effSJeff Roberson 		kmemzones[indx].kz_zone = uma_zcreate(name, size,
4168efc4effSJeff Roberson #ifdef INVARIANTS
4178f70816cSJeff Roberson 		    mtrash_ctor, mtrash_dtor, mtrash_init, mtrash_fini,
4188efc4effSJeff Roberson #else
4198efc4effSJeff Roberson 		    NULL, NULL, NULL, NULL,
4208efc4effSJeff Roberson #endif
4218efc4effSJeff Roberson 		    UMA_ALIGN_PTR, UMA_ZONE_MALLOC);
4226f267175SJeff Roberson 
4238355f576SJeff Roberson 		for (;i <= size; i+= KMEM_ZBASE)
4246f267175SJeff Roberson 			kmemsize[i >> KMEM_ZSHIFT] = indx;
4258355f576SJeff Roberson 
426df8bae1dSRodney W. Grimes 	}
427254c6cb3SPoul-Henning Kamp }
428254c6cb3SPoul-Henning Kamp 
429db669378SPeter Wemm void
430db669378SPeter Wemm malloc_init(data)
431db669378SPeter Wemm 	void *data;
432254c6cb3SPoul-Henning Kamp {
433db669378SPeter Wemm 	struct malloc_type *type = (struct malloc_type *)data;
434254c6cb3SPoul-Henning Kamp 
4356f267175SJeff Roberson 	mtx_lock(&malloc_mtx);
436d1bbc7ecSPoul-Henning Kamp 	if (type->ks_magic != M_MAGIC)
437d1bbc7ecSPoul-Henning Kamp 		panic("malloc type lacks magic");
438d1bbc7ecSPoul-Henning Kamp 
439d4060a87SJohn Dyson 	if (cnt.v_page_count == 0)
440d4060a87SJohn Dyson 		panic("malloc_init not allowed before vm init");
441d4060a87SJohn Dyson 
4426f267175SJeff Roberson 	if (type->ks_next != NULL)
4436f267175SJeff Roberson 		return;
4446f267175SJeff Roberson 
445254c6cb3SPoul-Henning Kamp 	type->ks_next = kmemstatistics;
446254c6cb3SPoul-Henning Kamp 	kmemstatistics = type;
4475a34a9f0SJeff Roberson 	mtx_init(&type->ks_mtx, type->ks_shortdesc, "Malloc Stats", MTX_DEF);
4486f267175SJeff Roberson 	mtx_unlock(&malloc_mtx);
449df8bae1dSRodney W. Grimes }
450db669378SPeter Wemm 
451db669378SPeter Wemm void
452db669378SPeter Wemm malloc_uninit(data)
453db669378SPeter Wemm 	void *data;
454db669378SPeter Wemm {
455db669378SPeter Wemm 	struct malloc_type *type = (struct malloc_type *)data;
456db669378SPeter Wemm 	struct malloc_type *t;
457db669378SPeter Wemm 
4586f267175SJeff Roberson 	mtx_lock(&malloc_mtx);
4595a34a9f0SJeff Roberson 	mtx_lock(&type->ks_mtx);
460db669378SPeter Wemm 	if (type->ks_magic != M_MAGIC)
461db669378SPeter Wemm 		panic("malloc type lacks magic");
462db669378SPeter Wemm 
463db669378SPeter Wemm 	if (cnt.v_page_count == 0)
464db669378SPeter Wemm 		panic("malloc_uninit not allowed before vm init");
465db669378SPeter Wemm 
466db669378SPeter Wemm 	if (type == kmemstatistics)
467db669378SPeter Wemm 		kmemstatistics = type->ks_next;
468db669378SPeter Wemm 	else {
469db669378SPeter Wemm 		for (t = kmemstatistics; t->ks_next != NULL; t = t->ks_next) {
470db669378SPeter Wemm 			if (t->ks_next == type) {
471db669378SPeter Wemm 				t->ks_next = type->ks_next;
472db669378SPeter Wemm 				break;
473db669378SPeter Wemm 			}
474db669378SPeter Wemm 		}
475db669378SPeter Wemm 	}
476ce45b512SBruce Evans 	type->ks_next = NULL;
4775a34a9f0SJeff Roberson 	mtx_destroy(&type->ks_mtx);
4786f267175SJeff Roberson 	mtx_unlock(&malloc_mtx);
479db669378SPeter Wemm }
4806f267175SJeff Roberson 
4816f267175SJeff Roberson static int
4826f267175SJeff Roberson sysctl_kern_malloc(SYSCTL_HANDLER_ARGS)
4836f267175SJeff Roberson {
4846f267175SJeff Roberson 	struct malloc_type *type;
4856f267175SJeff Roberson 	int linesize = 128;
4866f267175SJeff Roberson 	int curline;
4876f267175SJeff Roberson 	int bufsize;
4886f267175SJeff Roberson 	int first;
4896f267175SJeff Roberson 	int error;
4906f267175SJeff Roberson 	char *buf;
4916f267175SJeff Roberson 	char *p;
4926f267175SJeff Roberson 	int cnt;
4936f267175SJeff Roberson 	int len;
4946f267175SJeff Roberson 	int i;
4956f267175SJeff Roberson 
4966f267175SJeff Roberson 	cnt = 0;
4976f267175SJeff Roberson 
4986f267175SJeff Roberson 	mtx_lock(&malloc_mtx);
4996f267175SJeff Roberson 	for (type = kmemstatistics; type != NULL; type = type->ks_next)
5006f267175SJeff Roberson 		cnt++;
5016f267175SJeff Roberson 
5025a34a9f0SJeff Roberson 	mtx_unlock(&malloc_mtx);
5036f267175SJeff Roberson 	bufsize = linesize * (cnt + 1);
50444956c98SAlfred Perlstein 	p = buf = (char *)malloc(bufsize, M_TEMP, M_ZERO);
5055a34a9f0SJeff Roberson 	mtx_lock(&malloc_mtx);
5066f267175SJeff Roberson 
5076f267175SJeff Roberson 	len = snprintf(p, linesize,
5086f267175SJeff Roberson 	    "\n        Type  InUse MemUse HighUse Requests  Size(s)\n");
5096f267175SJeff Roberson 	p += len;
5106f267175SJeff Roberson 
5116f267175SJeff Roberson 	for (type = kmemstatistics; cnt != 0 && type != NULL;
5126f267175SJeff Roberson 	    type = type->ks_next, cnt--) {
5136f267175SJeff Roberson 		if (type->ks_calls == 0)
5146f267175SJeff Roberson 			continue;
5156f267175SJeff Roberson 
5166f267175SJeff Roberson 		curline = linesize - 2;	/* Leave room for the \n */
517289f207cSJeff Roberson 		len = snprintf(p, curline, "%13s%6lu%6luK%7luK%9llu",
5186f267175SJeff Roberson 			type->ks_shortdesc,
5196f267175SJeff Roberson 			type->ks_inuse,
5206f267175SJeff Roberson 			(type->ks_memuse + 1023) / 1024,
5216f267175SJeff Roberson 			(type->ks_maxused + 1023) / 1024,
5226f267175SJeff Roberson 			(long long unsigned)type->ks_calls);
5236f267175SJeff Roberson 		curline -= len;
5246f267175SJeff Roberson 		p += len;
5256f267175SJeff Roberson 
5266f267175SJeff Roberson 		first = 1;
527280759e7SRobert Drehmel 		for (i = 0; i < sizeof(kmemzones) / sizeof(kmemzones[0]) - 1;
528280759e7SRobert Drehmel 		    i++) {
5296f267175SJeff Roberson 			if (type->ks_size & (1 << i)) {
5306f267175SJeff Roberson 				if (first)
5316f267175SJeff Roberson 					len = snprintf(p, curline, "  ");
5326f267175SJeff Roberson 				else
5336f267175SJeff Roberson 					len = snprintf(p, curline, ",");
5346f267175SJeff Roberson 				curline -= len;
5356f267175SJeff Roberson 				p += len;
5366f267175SJeff Roberson 
5376f267175SJeff Roberson 				len = snprintf(p, curline,
5386f267175SJeff Roberson 				    "%s", kmemzones[i].kz_name);
5396f267175SJeff Roberson 				curline -= len;
5406f267175SJeff Roberson 				p += len;
5416f267175SJeff Roberson 
5426f267175SJeff Roberson 				first = 0;
5436f267175SJeff Roberson 			}
544280759e7SRobert Drehmel 		}
5456f267175SJeff Roberson 
5466f267175SJeff Roberson 		len = snprintf(p, 2, "\n");
5476f267175SJeff Roberson 		p += len;
5486f267175SJeff Roberson 	}
5496f267175SJeff Roberson 
5506f267175SJeff Roberson 	mtx_unlock(&malloc_mtx);
5516f267175SJeff Roberson 	error = SYSCTL_OUT(req, buf, p - buf);
5526f267175SJeff Roberson 
5536f267175SJeff Roberson 	free(buf, M_TEMP);
5546f267175SJeff Roberson 	return (error);
5556f267175SJeff Roberson }
5566f267175SJeff Roberson 
5576f267175SJeff Roberson SYSCTL_OID(_kern, OID_AUTO, malloc, CTLTYPE_STRING|CTLFLAG_RD,
5586f267175SJeff Roberson     NULL, 0, sysctl_kern_malloc, "A", "Malloc Stats");
5595e914b96SJeff Roberson 
5605e914b96SJeff Roberson #ifdef MALLOC_PROFILE
5615e914b96SJeff Roberson 
5625e914b96SJeff Roberson static int
5635e914b96SJeff Roberson sysctl_kern_mprof(SYSCTL_HANDLER_ARGS)
5645e914b96SJeff Roberson {
5655e914b96SJeff Roberson 	int linesize = 64;
5665e914b96SJeff Roberson 	uint64_t count;
5675e914b96SJeff Roberson 	uint64_t waste;
5685e914b96SJeff Roberson 	uint64_t mem;
5695e914b96SJeff Roberson 	int bufsize;
5705e914b96SJeff Roberson 	int error;
5715e914b96SJeff Roberson 	char *buf;
5725e914b96SJeff Roberson 	int rsize;
5735e914b96SJeff Roberson 	int size;
5745e914b96SJeff Roberson 	char *p;
5755e914b96SJeff Roberson 	int len;
5765e914b96SJeff Roberson 	int i;
5775e914b96SJeff Roberson 
5785e914b96SJeff Roberson 	bufsize = linesize * (KMEM_ZSIZE + 1);
5795e914b96SJeff Roberson 	bufsize += 128; 	/* For the stats line */
5805e914b96SJeff Roberson 	bufsize += 128; 	/* For the banner line */
5815e914b96SJeff Roberson 	waste = 0;
5825e914b96SJeff Roberson 	mem = 0;
5835e914b96SJeff Roberson 
58444956c98SAlfred Perlstein 	p = buf = (char *)malloc(bufsize, M_TEMP, M_ZERO);
5855e914b96SJeff Roberson 	len = snprintf(p, bufsize,
5865e914b96SJeff Roberson 	    "\n  Size                    Requests  Real Size\n");
5875e914b96SJeff Roberson 	bufsize -= len;
5885e914b96SJeff Roberson 	p += len;
5895e914b96SJeff Roberson 
5905e914b96SJeff Roberson 	for (i = 0; i < KMEM_ZSIZE; i++) {
5915e914b96SJeff Roberson 		size = i << KMEM_ZSHIFT;
5925e914b96SJeff Roberson 		rsize = kmemzones[kmemsize[i]].kz_size;
5935e914b96SJeff Roberson 		count = (long long unsigned)krequests[i];
5945e914b96SJeff Roberson 
5955e914b96SJeff Roberson 		len = snprintf(p, bufsize, "%6d%28llu%11d\n",
5965e914b96SJeff Roberson 		    size, (unsigned long long)count, rsize);
5975e914b96SJeff Roberson 		bufsize -= len;
5985e914b96SJeff Roberson 		p += len;
5995e914b96SJeff Roberson 
6005e914b96SJeff Roberson 		if ((rsize * count) > (size * count))
6015e914b96SJeff Roberson 			waste += (rsize * count) - (size * count);
6025e914b96SJeff Roberson 		mem += (rsize * count);
6035e914b96SJeff Roberson 	}
6045e914b96SJeff Roberson 
6055e914b96SJeff Roberson 	len = snprintf(p, bufsize,
6065e914b96SJeff Roberson 	    "\nTotal memory used:\t%30llu\nTotal Memory wasted:\t%30llu\n",
6075e914b96SJeff Roberson 	    (unsigned long long)mem, (unsigned long long)waste);
6085e914b96SJeff Roberson 	p += len;
6095e914b96SJeff Roberson 
6105e914b96SJeff Roberson 	error = SYSCTL_OUT(req, buf, p - buf);
6115e914b96SJeff Roberson 
6125e914b96SJeff Roberson 	free(buf, M_TEMP);
6135e914b96SJeff Roberson 	return (error);
6145e914b96SJeff Roberson }
6155e914b96SJeff Roberson 
6165e914b96SJeff Roberson SYSCTL_OID(_kern, OID_AUTO, mprof, CTLTYPE_STRING|CTLFLAG_RD,
6175e914b96SJeff Roberson     NULL, 0, sysctl_kern_mprof, "A", "Malloc Profiling");
6185e914b96SJeff Roberson #endif /* MALLOC_PROFILE */
619