xref: /freebsd/sys/kern/kern_malloc.c (revision eae870cdb4925ed3cf70ea90a67a683f28ab92da)
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 
141eae870cdSRobert Watson #ifdef MALLOC_MAKE_FAILURES
142eae870cdSRobert Watson /*
143eae870cdSRobert Watson  * Causes malloc failures every (n) mallocs with M_NOWAIT.  If set to 0,
144eae870cdSRobert Watson  * doesn't cause failures.
145eae870cdSRobert Watson  */
146eae870cdSRobert Watson SYSCTL_NODE(_debug, OID_AUTO, malloc, CTLFLAG_RD, 0,
147eae870cdSRobert Watson     "Kernel malloc debugging options");
148eae870cdSRobert Watson 
149eae870cdSRobert Watson static int malloc_failure_rate;
150eae870cdSRobert Watson static int malloc_nowait_count;
151eae870cdSRobert Watson static int malloc_failure_count;
152eae870cdSRobert Watson SYSCTL_INT(_debug_malloc, OID_AUTO, failure_rate, CTLFLAG_RW,
153eae870cdSRobert Watson     &malloc_failure_rate, 0, "Every (n) mallocs with M_NOWAIT will fail");
154eae870cdSRobert Watson SYSCTL_INT(_debug_malloc, OID_AUTO, failure_count, CTLFLAG_RD,
155eae870cdSRobert Watson     &malloc_failure_count, 0, "Number of imposed M_NOWAIT malloc failures");
156eae870cdSRobert Watson #endif
157eae870cdSRobert Watson 
1581fb14a47SPoul-Henning Kamp int
1591fb14a47SPoul-Henning Kamp malloc_last_fail(void)
1601fb14a47SPoul-Henning Kamp {
1611fb14a47SPoul-Henning Kamp 
1621fb14a47SPoul-Henning Kamp 	return (time_uptime - t_malloc_fail);
1631fb14a47SPoul-Henning Kamp }
1641fb14a47SPoul-Henning Kamp 
165df8bae1dSRodney W. Grimes /*
1661c7c3c6aSMatthew Dillon  *	malloc:
1671c7c3c6aSMatthew Dillon  *
1681c7c3c6aSMatthew Dillon  *	Allocate a block of memory.
1691c7c3c6aSMatthew Dillon  *
1701c7c3c6aSMatthew Dillon  *	If M_NOWAIT is set, this routine will not block and return NULL if
1711c7c3c6aSMatthew Dillon  *	the allocation fails.
172df8bae1dSRodney W. Grimes  */
173df8bae1dSRodney W. Grimes void *
174df8bae1dSRodney W. Grimes malloc(size, type, flags)
175df8bae1dSRodney W. Grimes 	unsigned long size;
17660a513e9SPoul-Henning Kamp 	struct malloc_type *type;
177254c6cb3SPoul-Henning Kamp 	int flags;
178df8bae1dSRodney W. Grimes {
1796f267175SJeff Roberson 	int indx;
1808355f576SJeff Roberson 	caddr_t va;
1818355f576SJeff Roberson 	uma_zone_t zone;
1824db4f5c8SPoul-Henning Kamp #ifdef DIAGNOSTIC
1834db4f5c8SPoul-Henning Kamp 	unsigned long osize = size;
1844db4f5c8SPoul-Henning Kamp #endif
18560a513e9SPoul-Henning Kamp 	register struct malloc_type *ksp = type;
186df8bae1dSRodney W. Grimes 
187194a0abfSPoul-Henning Kamp #ifdef INVARIANTS
188d3c11994SPoul-Henning Kamp 	/*
189d3c11994SPoul-Henning Kamp 	 * To make sure that WAITOK or NOWAIT is set, but not more than
190d3c11994SPoul-Henning Kamp 	 * one, and check against the API botches that are common.
191d3c11994SPoul-Henning Kamp 	 */
192d3c11994SPoul-Henning Kamp 	indx = flags & (M_WAITOK | M_NOWAIT | M_DONTWAIT | M_TRYWAIT);
193d3c11994SPoul-Henning Kamp 	if (indx != M_NOWAIT && indx != M_WAITOK) {
194d3c11994SPoul-Henning Kamp 		static	struct timeval lasterr;
195d3c11994SPoul-Henning Kamp 		static	int curerr, once;
196d3c11994SPoul-Henning Kamp 		if (once == 0 && ppsratecheck(&lasterr, &curerr, 1)) {
197d3c11994SPoul-Henning Kamp 			printf("Bad malloc flags: %x\n", indx);
198d3c11994SPoul-Henning Kamp 			backtrace();
199d3c11994SPoul-Henning Kamp 			flags |= M_WAITOK;
200d3c11994SPoul-Henning Kamp 			once++;
201d3c11994SPoul-Henning Kamp 		}
202d3c11994SPoul-Henning Kamp 	}
203194a0abfSPoul-Henning Kamp #endif
204708da94eSPoul-Henning Kamp #if 0
205708da94eSPoul-Henning Kamp 	if (size == 0)
206708da94eSPoul-Henning Kamp 		Debugger("zero size malloc");
207708da94eSPoul-Henning Kamp #endif
208eae870cdSRobert Watson #ifdef MALLOC_MAKE_FAILURES
209eae870cdSRobert Watson 	if ((flags & M_NOWAIT) && (malloc_failure_rate != 0)) {
210eae870cdSRobert Watson 		atomic_add_int(&malloc_nowait_count, 1);
211eae870cdSRobert Watson 		if ((malloc_nowait_count % malloc_failure_rate) == 0) {
212eae870cdSRobert Watson 			atomic_add_int(&malloc_failure_count, 1);
213eae870cdSRobert Watson 			return (NULL);
214eae870cdSRobert Watson 		}
215eae870cdSRobert Watson 	}
216eae870cdSRobert Watson #endif
217d3c11994SPoul-Henning Kamp 	if (flags & M_WAITOK)
218b40ce416SJulian Elischer 		KASSERT(curthread->td_intr_nesting_level == 0,
219a163d034SWarner Losh 		   ("malloc(M_WAITOK) in interrupt context"));
2208355f576SJeff Roberson 	if (size <= KMEM_ZMAX) {
2216f267175SJeff Roberson 		if (size & KMEM_ZMASK)
2226f267175SJeff Roberson 			size = (size & ~KMEM_ZMASK) + KMEM_ZBASE;
2236f267175SJeff Roberson 		indx = kmemsize[size >> KMEM_ZSHIFT];
2246f267175SJeff Roberson 		zone = kmemzones[indx].kz_zone;
2256f267175SJeff Roberson #ifdef MALLOC_PROFILE
2266f267175SJeff Roberson 		krequests[size >> KMEM_ZSHIFT]++;
2276f267175SJeff Roberson #endif
2288355f576SJeff Roberson 		va = uma_zalloc(zone, flags);
2295a34a9f0SJeff Roberson 		mtx_lock(&ksp->ks_mtx);
2306f267175SJeff Roberson 		if (va == NULL)
231df8bae1dSRodney W. Grimes 			goto out;
2326f267175SJeff Roberson 
2336f267175SJeff Roberson 		ksp->ks_size |= 1 << indx;
2346f267175SJeff Roberson 		size = zone->uz_size;
2358355f576SJeff Roberson 	} else {
2366f267175SJeff Roberson 		size = roundup(size, PAGE_SIZE);
2378355f576SJeff Roberson 		zone = NULL;
2388355f576SJeff Roberson 		va = uma_large_malloc(size, flags);
2395a34a9f0SJeff Roberson 		mtx_lock(&ksp->ks_mtx);
2406f267175SJeff Roberson 		if (va == NULL)
2418355f576SJeff Roberson 			goto out;
242df8bae1dSRodney W. Grimes 	}
2436f267175SJeff Roberson 	ksp->ks_memuse += size;
244df8bae1dSRodney W. Grimes 	ksp->ks_inuse++;
2458355f576SJeff Roberson out:
246df8bae1dSRodney W. Grimes 	ksp->ks_calls++;
247df8bae1dSRodney W. Grimes 	if (ksp->ks_memuse > ksp->ks_maxused)
248df8bae1dSRodney W. Grimes 		ksp->ks_maxused = ksp->ks_memuse;
2496f267175SJeff Roberson 
2505a34a9f0SJeff Roberson 	mtx_unlock(&ksp->ks_mtx);
2511fb14a47SPoul-Henning Kamp 	if (!(flags & M_NOWAIT))
252a163d034SWarner Losh 		KASSERT(va != NULL, ("malloc(M_WAITOK) returned NULL"));
2531fb14a47SPoul-Henning Kamp 	if (va == NULL) {
2541fb14a47SPoul-Henning Kamp 		t_malloc_fail = time_uptime;
2551fb14a47SPoul-Henning Kamp 	}
2564db4f5c8SPoul-Henning Kamp #ifdef DIAGNOSTIC
2574db4f5c8SPoul-Henning Kamp 	if (!(flags & M_ZERO)) {
2584db4f5c8SPoul-Henning Kamp 		memset(va, 0x70, osize);
2594db4f5c8SPoul-Henning Kamp 	}
2604db4f5c8SPoul-Henning Kamp #endif
261df8bae1dSRodney W. Grimes 	return ((void *) va);
262df8bae1dSRodney W. Grimes }
263df8bae1dSRodney W. Grimes 
264df8bae1dSRodney W. Grimes /*
2651c7c3c6aSMatthew Dillon  *	free:
2661c7c3c6aSMatthew Dillon  *
267df8bae1dSRodney W. Grimes  *	Free a block of memory allocated by malloc.
2681c7c3c6aSMatthew Dillon  *
2691c7c3c6aSMatthew Dillon  *	This routine may not block.
270df8bae1dSRodney W. Grimes  */
271df8bae1dSRodney W. Grimes void
272df8bae1dSRodney W. Grimes free(addr, type)
273df8bae1dSRodney W. Grimes 	void *addr;
27460a513e9SPoul-Henning Kamp 	struct malloc_type *type;
275df8bae1dSRodney W. Grimes {
27660a513e9SPoul-Henning Kamp 	register struct malloc_type *ksp = type;
27799571dc3SJeff Roberson 	uma_slab_t slab;
27899571dc3SJeff Roberson 	u_long size;
279254c6cb3SPoul-Henning Kamp 
28044a8ff31SArchie Cobbs 	/* free(NULL, ...) does nothing */
28144a8ff31SArchie Cobbs 	if (addr == NULL)
28244a8ff31SArchie Cobbs 		return;
28344a8ff31SArchie Cobbs 
2848355f576SJeff Roberson 	size = 0;
28569ef67f9SJason Evans 
28699571dc3SJeff Roberson 	slab = vtoslab((vm_offset_t)addr & (~UMA_SLAB_MASK));
2878355f576SJeff Roberson 
2888355f576SJeff Roberson 	if (slab == NULL)
2896f267175SJeff Roberson 		panic("free: address %p(%p) has not been allocated.\n",
29099571dc3SJeff Roberson 		    addr, (void *)((u_long)addr & (~UMA_SLAB_MASK)));
29199571dc3SJeff Roberson 
2928355f576SJeff Roberson 
2938355f576SJeff Roberson 	if (!(slab->us_flags & UMA_SLAB_MALLOC)) {
2948f70816cSJeff Roberson #ifdef INVARIANTS
2958f70816cSJeff Roberson 		struct malloc_type **mtp = addr;
2968f70816cSJeff Roberson #endif
2978355f576SJeff Roberson 		size = slab->us_zone->uz_size;
2988f70816cSJeff Roberson #ifdef INVARIANTS
2998f70816cSJeff Roberson 		/*
3008f70816cSJeff Roberson 		 * Cache a pointer to the malloc_type that most recently freed
3018f70816cSJeff Roberson 		 * this memory here.  This way we know who is most likely to
3028f70816cSJeff Roberson 		 * have stepped on it later.
3038f70816cSJeff Roberson 		 *
3048f70816cSJeff Roberson 		 * This code assumes that size is a multiple of 8 bytes for
3058f70816cSJeff Roberson 		 * 64 bit machines
3068f70816cSJeff Roberson 		 */
3078f70816cSJeff Roberson 		mtp = (struct malloc_type **)
3088f70816cSJeff Roberson 		    ((unsigned long)mtp & ~UMA_ALIGN_PTR);
3098f70816cSJeff Roberson 		mtp += (size - sizeof(struct malloc_type *)) /
3108f70816cSJeff Roberson 		    sizeof(struct malloc_type *);
3118f70816cSJeff Roberson 		*mtp = type;
3128f70816cSJeff Roberson #endif
3138355f576SJeff Roberson 		uma_zfree_arg(slab->us_zone, addr, slab);
31414bf02f8SJohn Dyson 	} else {
3158355f576SJeff Roberson 		size = slab->us_size;
3168355f576SJeff Roberson 		uma_large_free(slab);
31714bf02f8SJohn Dyson 	}
3185a34a9f0SJeff Roberson 	mtx_lock(&ksp->ks_mtx);
3198355f576SJeff Roberson 	ksp->ks_memuse -= size;
3208355f576SJeff Roberson 	ksp->ks_inuse--;
3215a34a9f0SJeff Roberson 	mtx_unlock(&ksp->ks_mtx);
322df8bae1dSRodney W. Grimes }
323df8bae1dSRodney W. Grimes 
324df8bae1dSRodney W. Grimes /*
32544a8ff31SArchie Cobbs  *	realloc: change the size of a memory block
32644a8ff31SArchie Cobbs  */
32744a8ff31SArchie Cobbs void *
32844a8ff31SArchie Cobbs realloc(addr, size, type, flags)
32944a8ff31SArchie Cobbs 	void *addr;
33044a8ff31SArchie Cobbs 	unsigned long size;
33144a8ff31SArchie Cobbs 	struct malloc_type *type;
33244a8ff31SArchie Cobbs 	int flags;
33344a8ff31SArchie Cobbs {
3348355f576SJeff Roberson 	uma_slab_t slab;
33544a8ff31SArchie Cobbs 	unsigned long alloc;
33644a8ff31SArchie Cobbs 	void *newaddr;
33744a8ff31SArchie Cobbs 
33844a8ff31SArchie Cobbs 	/* realloc(NULL, ...) is equivalent to malloc(...) */
33944a8ff31SArchie Cobbs 	if (addr == NULL)
34044a8ff31SArchie Cobbs 		return (malloc(size, type, flags));
34144a8ff31SArchie Cobbs 
34299571dc3SJeff Roberson 	slab = vtoslab((vm_offset_t)addr & ~(UMA_SLAB_MASK));
3438355f576SJeff Roberson 
34444a8ff31SArchie Cobbs 	/* Sanity check */
3458355f576SJeff Roberson 	KASSERT(slab != NULL,
34644a8ff31SArchie Cobbs 	    ("realloc: address %p out of range", (void *)addr));
34744a8ff31SArchie Cobbs 
34844a8ff31SArchie Cobbs 	/* Get the size of the original block */
3498355f576SJeff Roberson 	if (slab->us_zone)
3508355f576SJeff Roberson 		alloc = slab->us_zone->uz_size;
3518355f576SJeff Roberson 	else
3528355f576SJeff Roberson 		alloc = slab->us_size;
35344a8ff31SArchie Cobbs 
35444a8ff31SArchie Cobbs 	/* Reuse the original block if appropriate */
35544a8ff31SArchie Cobbs 	if (size <= alloc
35644a8ff31SArchie Cobbs 	    && (size > (alloc >> REALLOC_FRACTION) || alloc == MINALLOCSIZE))
35744a8ff31SArchie Cobbs 		return (addr);
35844a8ff31SArchie Cobbs 
35944a8ff31SArchie Cobbs 	/* Allocate a new, bigger (or smaller) block */
36044a8ff31SArchie Cobbs 	if ((newaddr = malloc(size, type, flags)) == NULL)
36144a8ff31SArchie Cobbs 		return (NULL);
36244a8ff31SArchie Cobbs 
36344a8ff31SArchie Cobbs 	/* Copy over original contents */
36444a8ff31SArchie Cobbs 	bcopy(addr, newaddr, min(size, alloc));
36544a8ff31SArchie Cobbs 	free(addr, type);
36644a8ff31SArchie Cobbs 	return (newaddr);
36744a8ff31SArchie Cobbs }
36844a8ff31SArchie Cobbs 
36944a8ff31SArchie Cobbs /*
37044a8ff31SArchie Cobbs  *	reallocf: same as realloc() but free memory on failure.
37144a8ff31SArchie Cobbs  */
37244a8ff31SArchie Cobbs void *
37344a8ff31SArchie Cobbs reallocf(addr, size, type, flags)
37444a8ff31SArchie Cobbs 	void *addr;
37544a8ff31SArchie Cobbs 	unsigned long size;
37644a8ff31SArchie Cobbs 	struct malloc_type *type;
37744a8ff31SArchie Cobbs 	int flags;
37844a8ff31SArchie Cobbs {
37944a8ff31SArchie Cobbs 	void *mem;
38044a8ff31SArchie Cobbs 
38144a8ff31SArchie Cobbs 	if ((mem = realloc(addr, size, type, flags)) == NULL)
38244a8ff31SArchie Cobbs 		free(addr, type);
38344a8ff31SArchie Cobbs 	return (mem);
38444a8ff31SArchie Cobbs }
38544a8ff31SArchie Cobbs 
38644a8ff31SArchie Cobbs /*
387df8bae1dSRodney W. Grimes  * Initialize the kernel memory allocator
388df8bae1dSRodney W. Grimes  */
3892b14f991SJulian Elischer /* ARGSUSED*/
3902b14f991SJulian Elischer static void
391d841aaa7SBruce Evans kmeminit(dummy)
392d841aaa7SBruce Evans 	void *dummy;
393df8bae1dSRodney W. Grimes {
3946f267175SJeff Roberson 	u_int8_t indx;
39527b8623fSDavid Greenman 	u_long npg;
39627b8623fSDavid Greenman 	u_long mem_size;
3978355f576SJeff Roberson 	int i;
3988a58a9f6SJohn Dyson 
3996008862bSJohn Baldwin 	mtx_init(&malloc_mtx, "malloc", NULL, MTX_DEF);
40069ef67f9SJason Evans 
4018a58a9f6SJohn Dyson 	/*
4028a58a9f6SJohn Dyson 	 * Try to auto-tune the kernel memory size, so that it is
4038a58a9f6SJohn Dyson 	 * more applicable for a wider range of machine sizes.
4048a58a9f6SJohn Dyson 	 * On an X86, a VM_KMEM_SIZE_SCALE value of 4 is good, while
4058a58a9f6SJohn Dyson 	 * a VM_KMEM_SIZE of 12MB is a fair compromise.  The
4068a58a9f6SJohn Dyson 	 * VM_KMEM_SIZE_MAX is dependent on the maximum KVA space
4078a58a9f6SJohn Dyson 	 * available, and on an X86 with a total KVA space of 256MB,
4088a58a9f6SJohn Dyson 	 * try to keep VM_KMEM_SIZE_MAX at 80MB or below.
4098a58a9f6SJohn Dyson 	 *
4108a58a9f6SJohn Dyson 	 * Note that the kmem_map is also used by the zone allocator,
4118a58a9f6SJohn Dyson 	 * so make sure that there is enough space.
4128a58a9f6SJohn Dyson 	 */
41381930014SPeter Wemm 	vm_kmem_size = VM_KMEM_SIZE;
4148a58a9f6SJohn Dyson 	mem_size = cnt.v_page_count * PAGE_SIZE;
4158a58a9f6SJohn Dyson 
4168a58a9f6SJohn Dyson #if defined(VM_KMEM_SIZE_SCALE)
41781930014SPeter Wemm 	if ((mem_size / VM_KMEM_SIZE_SCALE) > vm_kmem_size)
41881930014SPeter Wemm 		vm_kmem_size = mem_size / VM_KMEM_SIZE_SCALE;
4198a58a9f6SJohn Dyson #endif
4208a58a9f6SJohn Dyson 
4218a58a9f6SJohn Dyson #if defined(VM_KMEM_SIZE_MAX)
42281930014SPeter Wemm 	if (vm_kmem_size >= VM_KMEM_SIZE_MAX)
42381930014SPeter Wemm 		vm_kmem_size = VM_KMEM_SIZE_MAX;
4248a58a9f6SJohn Dyson #endif
4258a58a9f6SJohn Dyson 
4268de6e8e1SMike Smith 	/* Allow final override from the kernel environment */
42709786698SPeter Wemm 	TUNABLE_INT_FETCH("kern.vm.kmem.size", &vm_kmem_size);
4288de6e8e1SMike Smith 
42927b8623fSDavid Greenman 	/*
43027b8623fSDavid Greenman 	 * Limit kmem virtual size to twice the physical memory.
43127b8623fSDavid Greenman 	 * This allows for kmem map sparseness, but limits the size
43227b8623fSDavid Greenman 	 * to something sane. Be careful to not overflow the 32bit
43327b8623fSDavid Greenman 	 * ints while doing the check.
43427b8623fSDavid Greenman 	 */
43527b8623fSDavid Greenman 	if ((vm_kmem_size / 2) > (cnt.v_page_count * PAGE_SIZE))
43627b8623fSDavid Greenman 		vm_kmem_size = 2 * cnt.v_page_count * PAGE_SIZE;
4378a58a9f6SJohn Dyson 
43808442f8aSBosko Milekic 	/*
439ba3e8826SBosko Milekic 	 * In mbuf_init(), we set up submaps for mbufs and clusters, in which
44008442f8aSBosko Milekic 	 * case we rounddown() (nmbufs * MSIZE) and (nmbclusters * MCLBYTES),
44108442f8aSBosko Milekic 	 * respectively. Mathematically, this means that what we do here may
44208442f8aSBosko Milekic 	 * amount to slightly more address space than we need for the submaps,
44308442f8aSBosko Milekic 	 * but it never hurts to have an extra page in kmem_map.
44408442f8aSBosko Milekic 	 */
445025b4be1SBosko Milekic 	npg = (nmbufs*MSIZE + nmbclusters*MCLBYTES + vm_kmem_size) / PAGE_SIZE;
4460d94caffSDavid Greenman 
447df8bae1dSRodney W. Grimes 	kmem_map = kmem_suballoc(kernel_map, (vm_offset_t *)&kmembase,
4482d8acc0fSJohn Dyson 		(vm_offset_t *)&kmemlimit, (vm_size_t)(npg * PAGE_SIZE));
4493075778bSJohn Dyson 	kmem_map->system_map = 1;
4508355f576SJeff Roberson 
45199571dc3SJeff Roberson 	uma_startup2();
4528355f576SJeff Roberson 
4536f267175SJeff Roberson 	for (i = 0, indx = 0; kmemzones[indx].kz_size != 0; indx++) {
4546f267175SJeff Roberson 		int size = kmemzones[indx].kz_size;
4556f267175SJeff Roberson 		char *name = kmemzones[indx].kz_name;
4568355f576SJeff Roberson 
4578efc4effSJeff Roberson 		kmemzones[indx].kz_zone = uma_zcreate(name, size,
4588efc4effSJeff Roberson #ifdef INVARIANTS
4598f70816cSJeff Roberson 		    mtrash_ctor, mtrash_dtor, mtrash_init, mtrash_fini,
4608efc4effSJeff Roberson #else
4618efc4effSJeff Roberson 		    NULL, NULL, NULL, NULL,
4628efc4effSJeff Roberson #endif
4638efc4effSJeff Roberson 		    UMA_ALIGN_PTR, UMA_ZONE_MALLOC);
4646f267175SJeff Roberson 
4658355f576SJeff Roberson 		for (;i <= size; i+= KMEM_ZBASE)
4666f267175SJeff Roberson 			kmemsize[i >> KMEM_ZSHIFT] = indx;
4678355f576SJeff Roberson 
468df8bae1dSRodney W. Grimes 	}
469254c6cb3SPoul-Henning Kamp }
470254c6cb3SPoul-Henning Kamp 
471db669378SPeter Wemm void
472db669378SPeter Wemm malloc_init(data)
473db669378SPeter Wemm 	void *data;
474254c6cb3SPoul-Henning Kamp {
475db669378SPeter Wemm 	struct malloc_type *type = (struct malloc_type *)data;
476254c6cb3SPoul-Henning Kamp 
4776f267175SJeff Roberson 	mtx_lock(&malloc_mtx);
478d1bbc7ecSPoul-Henning Kamp 	if (type->ks_magic != M_MAGIC)
479d1bbc7ecSPoul-Henning Kamp 		panic("malloc type lacks magic");
480d1bbc7ecSPoul-Henning Kamp 
481d4060a87SJohn Dyson 	if (cnt.v_page_count == 0)
482d4060a87SJohn Dyson 		panic("malloc_init not allowed before vm init");
483d4060a87SJohn Dyson 
4846f267175SJeff Roberson 	if (type->ks_next != NULL)
4856f267175SJeff Roberson 		return;
4866f267175SJeff Roberson 
487254c6cb3SPoul-Henning Kamp 	type->ks_next = kmemstatistics;
488254c6cb3SPoul-Henning Kamp 	kmemstatistics = type;
4895a34a9f0SJeff Roberson 	mtx_init(&type->ks_mtx, type->ks_shortdesc, "Malloc Stats", MTX_DEF);
4906f267175SJeff Roberson 	mtx_unlock(&malloc_mtx);
491df8bae1dSRodney W. Grimes }
492db669378SPeter Wemm 
493db669378SPeter Wemm void
494db669378SPeter Wemm malloc_uninit(data)
495db669378SPeter Wemm 	void *data;
496db669378SPeter Wemm {
497db669378SPeter Wemm 	struct malloc_type *type = (struct malloc_type *)data;
498db669378SPeter Wemm 	struct malloc_type *t;
499db669378SPeter Wemm 
5006f267175SJeff Roberson 	mtx_lock(&malloc_mtx);
5015a34a9f0SJeff Roberson 	mtx_lock(&type->ks_mtx);
502db669378SPeter Wemm 	if (type->ks_magic != M_MAGIC)
503db669378SPeter Wemm 		panic("malloc type lacks magic");
504db669378SPeter Wemm 
505db669378SPeter Wemm 	if (cnt.v_page_count == 0)
506db669378SPeter Wemm 		panic("malloc_uninit not allowed before vm init");
507db669378SPeter Wemm 
508db669378SPeter Wemm 	if (type == kmemstatistics)
509db669378SPeter Wemm 		kmemstatistics = type->ks_next;
510db669378SPeter Wemm 	else {
511db669378SPeter Wemm 		for (t = kmemstatistics; t->ks_next != NULL; t = t->ks_next) {
512db669378SPeter Wemm 			if (t->ks_next == type) {
513db669378SPeter Wemm 				t->ks_next = type->ks_next;
514db669378SPeter Wemm 				break;
515db669378SPeter Wemm 			}
516db669378SPeter Wemm 		}
517db669378SPeter Wemm 	}
518ce45b512SBruce Evans 	type->ks_next = NULL;
5195a34a9f0SJeff Roberson 	mtx_destroy(&type->ks_mtx);
5206f267175SJeff Roberson 	mtx_unlock(&malloc_mtx);
521db669378SPeter Wemm }
5226f267175SJeff Roberson 
5236f267175SJeff Roberson static int
5246f267175SJeff Roberson sysctl_kern_malloc(SYSCTL_HANDLER_ARGS)
5256f267175SJeff Roberson {
5266f267175SJeff Roberson 	struct malloc_type *type;
5276f267175SJeff Roberson 	int linesize = 128;
5286f267175SJeff Roberson 	int curline;
5296f267175SJeff Roberson 	int bufsize;
5306f267175SJeff Roberson 	int first;
5316f267175SJeff Roberson 	int error;
5326f267175SJeff Roberson 	char *buf;
5336f267175SJeff Roberson 	char *p;
5346f267175SJeff Roberson 	int cnt;
5356f267175SJeff Roberson 	int len;
5366f267175SJeff Roberson 	int i;
5376f267175SJeff Roberson 
5386f267175SJeff Roberson 	cnt = 0;
5396f267175SJeff Roberson 
5406f267175SJeff Roberson 	mtx_lock(&malloc_mtx);
5416f267175SJeff Roberson 	for (type = kmemstatistics; type != NULL; type = type->ks_next)
5426f267175SJeff Roberson 		cnt++;
5436f267175SJeff Roberson 
5445a34a9f0SJeff Roberson 	mtx_unlock(&malloc_mtx);
5456f267175SJeff Roberson 	bufsize = linesize * (cnt + 1);
546a163d034SWarner Losh 	p = buf = (char *)malloc(bufsize, M_TEMP, M_WAITOK|M_ZERO);
5475a34a9f0SJeff Roberson 	mtx_lock(&malloc_mtx);
5486f267175SJeff Roberson 
5496f267175SJeff Roberson 	len = snprintf(p, linesize,
5506f267175SJeff Roberson 	    "\n        Type  InUse MemUse HighUse Requests  Size(s)\n");
5516f267175SJeff Roberson 	p += len;
5526f267175SJeff Roberson 
5536f267175SJeff Roberson 	for (type = kmemstatistics; cnt != 0 && type != NULL;
5546f267175SJeff Roberson 	    type = type->ks_next, cnt--) {
5556f267175SJeff Roberson 		if (type->ks_calls == 0)
5566f267175SJeff Roberson 			continue;
5576f267175SJeff Roberson 
5586f267175SJeff Roberson 		curline = linesize - 2;	/* Leave room for the \n */
559289f207cSJeff Roberson 		len = snprintf(p, curline, "%13s%6lu%6luK%7luK%9llu",
5606f267175SJeff Roberson 			type->ks_shortdesc,
5616f267175SJeff Roberson 			type->ks_inuse,
5626f267175SJeff Roberson 			(type->ks_memuse + 1023) / 1024,
5636f267175SJeff Roberson 			(type->ks_maxused + 1023) / 1024,
5646f267175SJeff Roberson 			(long long unsigned)type->ks_calls);
5656f267175SJeff Roberson 		curline -= len;
5666f267175SJeff Roberson 		p += len;
5676f267175SJeff Roberson 
5686f267175SJeff Roberson 		first = 1;
569280759e7SRobert Drehmel 		for (i = 0; i < sizeof(kmemzones) / sizeof(kmemzones[0]) - 1;
570280759e7SRobert Drehmel 		    i++) {
5716f267175SJeff Roberson 			if (type->ks_size & (1 << i)) {
5726f267175SJeff Roberson 				if (first)
5736f267175SJeff Roberson 					len = snprintf(p, curline, "  ");
5746f267175SJeff Roberson 				else
5756f267175SJeff Roberson 					len = snprintf(p, curline, ",");
5766f267175SJeff Roberson 				curline -= len;
5776f267175SJeff Roberson 				p += len;
5786f267175SJeff Roberson 
5796f267175SJeff Roberson 				len = snprintf(p, curline,
5806f267175SJeff Roberson 				    "%s", kmemzones[i].kz_name);
5816f267175SJeff Roberson 				curline -= len;
5826f267175SJeff Roberson 				p += len;
5836f267175SJeff Roberson 
5846f267175SJeff Roberson 				first = 0;
5856f267175SJeff Roberson 			}
586280759e7SRobert Drehmel 		}
5876f267175SJeff Roberson 
5886f267175SJeff Roberson 		len = snprintf(p, 2, "\n");
5896f267175SJeff Roberson 		p += len;
5906f267175SJeff Roberson 	}
5916f267175SJeff Roberson 
5926f267175SJeff Roberson 	mtx_unlock(&malloc_mtx);
5936f267175SJeff Roberson 	error = SYSCTL_OUT(req, buf, p - buf);
5946f267175SJeff Roberson 
5956f267175SJeff Roberson 	free(buf, M_TEMP);
5966f267175SJeff Roberson 	return (error);
5976f267175SJeff Roberson }
5986f267175SJeff Roberson 
5996f267175SJeff Roberson SYSCTL_OID(_kern, OID_AUTO, malloc, CTLTYPE_STRING|CTLFLAG_RD,
6006f267175SJeff Roberson     NULL, 0, sysctl_kern_malloc, "A", "Malloc Stats");
6015e914b96SJeff Roberson 
6025e914b96SJeff Roberson #ifdef MALLOC_PROFILE
6035e914b96SJeff Roberson 
6045e914b96SJeff Roberson static int
6055e914b96SJeff Roberson sysctl_kern_mprof(SYSCTL_HANDLER_ARGS)
6065e914b96SJeff Roberson {
6075e914b96SJeff Roberson 	int linesize = 64;
6085e914b96SJeff Roberson 	uint64_t count;
6095e914b96SJeff Roberson 	uint64_t waste;
6105e914b96SJeff Roberson 	uint64_t mem;
6115e914b96SJeff Roberson 	int bufsize;
6125e914b96SJeff Roberson 	int error;
6135e914b96SJeff Roberson 	char *buf;
6145e914b96SJeff Roberson 	int rsize;
6155e914b96SJeff Roberson 	int size;
6165e914b96SJeff Roberson 	char *p;
6175e914b96SJeff Roberson 	int len;
6185e914b96SJeff Roberson 	int i;
6195e914b96SJeff Roberson 
6205e914b96SJeff Roberson 	bufsize = linesize * (KMEM_ZSIZE + 1);
6215e914b96SJeff Roberson 	bufsize += 128; 	/* For the stats line */
6225e914b96SJeff Roberson 	bufsize += 128; 	/* For the banner line */
6235e914b96SJeff Roberson 	waste = 0;
6245e914b96SJeff Roberson 	mem = 0;
6255e914b96SJeff Roberson 
626a163d034SWarner Losh 	p = buf = (char *)malloc(bufsize, M_TEMP, M_WAITOK|M_ZERO);
6275e914b96SJeff Roberson 	len = snprintf(p, bufsize,
6285e914b96SJeff Roberson 	    "\n  Size                    Requests  Real Size\n");
6295e914b96SJeff Roberson 	bufsize -= len;
6305e914b96SJeff Roberson 	p += len;
6315e914b96SJeff Roberson 
6325e914b96SJeff Roberson 	for (i = 0; i < KMEM_ZSIZE; i++) {
6335e914b96SJeff Roberson 		size = i << KMEM_ZSHIFT;
6345e914b96SJeff Roberson 		rsize = kmemzones[kmemsize[i]].kz_size;
6355e914b96SJeff Roberson 		count = (long long unsigned)krequests[i];
6365e914b96SJeff Roberson 
6375e914b96SJeff Roberson 		len = snprintf(p, bufsize, "%6d%28llu%11d\n",
6385e914b96SJeff Roberson 		    size, (unsigned long long)count, rsize);
6395e914b96SJeff Roberson 		bufsize -= len;
6405e914b96SJeff Roberson 		p += len;
6415e914b96SJeff Roberson 
6425e914b96SJeff Roberson 		if ((rsize * count) > (size * count))
6435e914b96SJeff Roberson 			waste += (rsize * count) - (size * count);
6445e914b96SJeff Roberson 		mem += (rsize * count);
6455e914b96SJeff Roberson 	}
6465e914b96SJeff Roberson 
6475e914b96SJeff Roberson 	len = snprintf(p, bufsize,
6485e914b96SJeff Roberson 	    "\nTotal memory used:\t%30llu\nTotal Memory wasted:\t%30llu\n",
6495e914b96SJeff Roberson 	    (unsigned long long)mem, (unsigned long long)waste);
6505e914b96SJeff Roberson 	p += len;
6515e914b96SJeff Roberson 
6525e914b96SJeff Roberson 	error = SYSCTL_OUT(req, buf, p - buf);
6535e914b96SJeff Roberson 
6545e914b96SJeff Roberson 	free(buf, M_TEMP);
6555e914b96SJeff Roberson 	return (error);
6565e914b96SJeff Roberson }
6575e914b96SJeff Roberson 
6585e914b96SJeff Roberson SYSCTL_OID(_kern, OID_AUTO, mprof, CTLTYPE_STRING|CTLFLAG_RD,
6595e914b96SJeff Roberson     NULL, 0, sysctl_kern_mprof, "A", "Malloc Profiling");
6605e914b96SJeff Roberson #endif /* MALLOC_PROFILE */
661