xref: /freebsd/sys/kern/kern_malloc.c (revision 44f1c916109d4d88941d257b7c4c96c26ab55477)
19454b2d8SWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1987, 1991, 1993
363a7e0a3SRobert Watson  *	The Regents of the University of California.
4bb1c7df8SRobert Watson  * Copyright (c) 2005-2009 Robert N. M. Watson
563a7e0a3SRobert Watson  * All rights reserved.
6df8bae1dSRodney W. Grimes  *
7df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
8df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
9df8bae1dSRodney W. Grimes  * are met:
10df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
12df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
13df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
14df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
15df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
16df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
17df8bae1dSRodney W. Grimes  *    without specific prior written permission.
18df8bae1dSRodney W. Grimes  *
19df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
30df8bae1dSRodney W. Grimes  *
31df8bae1dSRodney W. Grimes  *	@(#)kern_malloc.c	8.3 (Berkeley) 1/4/94
32df8bae1dSRodney W. Grimes  */
33df8bae1dSRodney W. Grimes 
340ce3f16dSRobert Watson /*
350ce3f16dSRobert Watson  * Kernel malloc(9) implementation -- general purpose kernel memory allocator
360ce3f16dSRobert Watson  * based on memory types.  Back end is implemented using the UMA(9) zone
370ce3f16dSRobert Watson  * allocator.  A set of fixed-size buckets are used for smaller allocations,
380ce3f16dSRobert Watson  * and a special UMA allocation interface is used for larger allocations.
390ce3f16dSRobert Watson  * Callers declare memory types, and statistics are maintained independently
400ce3f16dSRobert Watson  * for each memory type.  Statistics are maintained per-CPU for performance
410ce3f16dSRobert Watson  * reasons.  See malloc(9) and comments in malloc.h for a detailed
420ce3f16dSRobert Watson  * description.
430ce3f16dSRobert Watson  */
440ce3f16dSRobert Watson 
45677b542eSDavid E. O'Brien #include <sys/cdefs.h>
46677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$");
47677b542eSDavid E. O'Brien 
48909ed16cSRobert Watson #include "opt_ddb.h"
498a58a9f6SJohn Dyson #include "opt_vm.h"
508a58a9f6SJohn Dyson 
51df8bae1dSRodney W. Grimes #include <sys/param.h>
5226f9a767SRodney W. Grimes #include <sys/systm.h>
532d50560aSMarcel Moolenaar #include <sys/kdb.h>
54df8bae1dSRodney W. Grimes #include <sys/kernel.h>
55fb919e4dSMark Murray #include <sys/lock.h>
56df8bae1dSRodney W. Grimes #include <sys/malloc.h>
57eec258d2SJohn Baldwin #include <sys/mutex.h>
58efeaf95aSDavid Greenman #include <sys/vmmeter.h>
59a448b62aSJake Burkholder #include <sys/proc.h>
6063a7e0a3SRobert Watson #include <sys/sbuf.h>
616f267175SJeff Roberson #include <sys/sysctl.h>
621fb14a47SPoul-Henning Kamp #include <sys/time.h>
635df87b21SJeff Roberson #include <sys/vmem.h>
649a02e8c6SJason Evans 
65df8bae1dSRodney W. Grimes #include <vm/vm.h>
6699571dc3SJeff Roberson #include <vm/pmap.h>
675df87b21SJeff Roberson #include <vm/vm_pageout.h>
68efeaf95aSDavid Greenman #include <vm/vm_param.h>
69df8bae1dSRodney W. Grimes #include <vm/vm_kern.h>
70efeaf95aSDavid Greenman #include <vm/vm_extern.h>
713075778bSJohn Dyson #include <vm/vm_map.h>
7299571dc3SJeff Roberson #include <vm/vm_page.h>
738355f576SJeff Roberson #include <vm/uma.h>
748355f576SJeff Roberson #include <vm/uma_int.h>
758efc4effSJeff Roberson #include <vm/uma_dbg.h>
76df8bae1dSRodney W. Grimes 
77e4eb384bSBosko Milekic #ifdef DEBUG_MEMGUARD
78e4eb384bSBosko Milekic #include <vm/memguard.h>
79e4eb384bSBosko Milekic #endif
80847a2a17SPawel Jakub Dawidek #ifdef DEBUG_REDZONE
81847a2a17SPawel Jakub Dawidek #include <vm/redzone.h>
82847a2a17SPawel Jakub Dawidek #endif
83e4eb384bSBosko Milekic 
84984982d6SPoul-Henning Kamp #if defined(INVARIANTS) && defined(__i386__)
85984982d6SPoul-Henning Kamp #include <machine/cpu.h>
86984982d6SPoul-Henning Kamp #endif
87984982d6SPoul-Henning Kamp 
88909ed16cSRobert Watson #include <ddb/ddb.h>
89909ed16cSRobert Watson 
9091dd776cSJohn Birrell #ifdef KDTRACE_HOOKS
9191dd776cSJohn Birrell #include <sys/dtrace_bsd.h>
9291dd776cSJohn Birrell 
9391dd776cSJohn Birrell dtrace_malloc_probe_func_t	dtrace_malloc_probe;
9491dd776cSJohn Birrell #endif
9591dd776cSJohn Birrell 
9644a8ff31SArchie Cobbs /*
9744a8ff31SArchie Cobbs  * When realloc() is called, if the new size is sufficiently smaller than
9844a8ff31SArchie Cobbs  * the old size, realloc() will allocate a new, smaller block to avoid
9944a8ff31SArchie Cobbs  * wasting memory. 'Sufficiently smaller' is defined as: newsize <=
10044a8ff31SArchie Cobbs  * oldsize / 2^n, where REALLOC_FRACTION defines the value of 'n'.
10144a8ff31SArchie Cobbs  */
10244a8ff31SArchie Cobbs #ifndef REALLOC_FRACTION
10344a8ff31SArchie Cobbs #define	REALLOC_FRACTION	1	/* new block if <= half the size */
10444a8ff31SArchie Cobbs #endif
10544a8ff31SArchie Cobbs 
1060ce3f16dSRobert Watson /*
1070ce3f16dSRobert Watson  * Centrally define some common malloc types.
1080ce3f16dSRobert Watson  */
1093b6fb885SPoul-Henning Kamp MALLOC_DEFINE(M_CACHE, "cache", "Various Dynamically allocated caches");
1109ef246c6SBruce Evans MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory");
1119ef246c6SBruce Evans MALLOC_DEFINE(M_TEMP, "temp", "misc temporary data buffers");
1129ef246c6SBruce Evans 
11382cd038dSYoshinobu Inoue MALLOC_DEFINE(M_IP6OPT, "ip6opt", "IPv6 options");
11482cd038dSYoshinobu Inoue MALLOC_DEFINE(M_IP6NDP, "ip6ndp", "IPv6 Neighbor Discovery");
11582cd038dSYoshinobu Inoue 
116db669378SPeter Wemm static struct malloc_type *kmemstatistics;
117cd814b26SRobert Watson static int kmemcount;
1181f6889a1SMatthew Dillon 
1198355f576SJeff Roberson #define KMEM_ZSHIFT	4
1208355f576SJeff Roberson #define KMEM_ZBASE	16
1218355f576SJeff Roberson #define KMEM_ZMASK	(KMEM_ZBASE - 1)
1228355f576SJeff Roberson 
1239fb535deSJeff Roberson #define KMEM_ZMAX	PAGE_SIZE
1248355f576SJeff Roberson #define KMEM_ZSIZE	(KMEM_ZMAX >> KMEM_ZSHIFT)
12560ae52f7SEd Schouten static uint8_t kmemsize[KMEM_ZSIZE + 1];
1266f267175SJeff Roberson 
127d7854da1SMatthew D Fleming #ifndef MALLOC_DEBUG_MAXZONES
128d7854da1SMatthew D Fleming #define	MALLOC_DEBUG_MAXZONES	1
129d7854da1SMatthew D Fleming #endif
130d7854da1SMatthew D Fleming static int numzones = MALLOC_DEBUG_MAXZONES;
131d7854da1SMatthew D Fleming 
1320ce3f16dSRobert Watson /*
1330ce3f16dSRobert Watson  * Small malloc(9) memory allocations are allocated from a set of UMA buckets
1340ce3f16dSRobert Watson  * of various sizes.
1350ce3f16dSRobert Watson  *
1360ce3f16dSRobert Watson  * XXX: The comment here used to read "These won't be powers of two for
1370ce3f16dSRobert Watson  * long."  It's possible that a significant amount of wasted memory could be
1380ce3f16dSRobert Watson  * recovered by tuning the sizes of these buckets.
1390ce3f16dSRobert Watson  */
1408355f576SJeff Roberson struct {
1416f267175SJeff Roberson 	int kz_size;
1426f267175SJeff Roberson 	char *kz_name;
143d7854da1SMatthew D Fleming 	uma_zone_t kz_zone[MALLOC_DEBUG_MAXZONES];
1446f267175SJeff Roberson } kmemzones[] = {
145d7854da1SMatthew D Fleming 	{16, "16", },
146d7854da1SMatthew D Fleming 	{32, "32", },
147d7854da1SMatthew D Fleming 	{64, "64", },
148d7854da1SMatthew D Fleming 	{128, "128", },
149d7854da1SMatthew D Fleming 	{256, "256", },
150d7854da1SMatthew D Fleming 	{512, "512", },
151d7854da1SMatthew D Fleming 	{1024, "1024", },
152d7854da1SMatthew D Fleming 	{2048, "2048", },
153d7854da1SMatthew D Fleming 	{4096, "4096", },
1549fb535deSJeff Roberson #if PAGE_SIZE > 4096
155d7854da1SMatthew D Fleming 	{8192, "8192", },
1569fb535deSJeff Roberson #if PAGE_SIZE > 8192
157d7854da1SMatthew D Fleming 	{16384, "16384", },
1589fb535deSJeff Roberson #if PAGE_SIZE > 16384
159d7854da1SMatthew D Fleming 	{32768, "32768", },
1609fb535deSJeff Roberson #if PAGE_SIZE > 32768
161d7854da1SMatthew D Fleming 	{65536, "65536", },
1629fb535deSJeff Roberson #if PAGE_SIZE > 65536
1639fb535deSJeff Roberson #error	"Unsupported PAGE_SIZE"
1649fb535deSJeff Roberson #endif	/* 65536 */
1659fb535deSJeff Roberson #endif	/* 32768 */
1669fb535deSJeff Roberson #endif	/* 16384 */
1679fb535deSJeff Roberson #endif	/* 8192 */
1689fb535deSJeff Roberson #endif	/* 4096 */
1698355f576SJeff Roberson 	{0, NULL},
1708355f576SJeff Roberson };
1718355f576SJeff Roberson 
1720ce3f16dSRobert Watson /*
1730ce3f16dSRobert Watson  * Zone to allocate malloc type descriptions from.  For ABI reasons, memory
1740ce3f16dSRobert Watson  * types are described by a data structure passed by the declaring code, but
1750ce3f16dSRobert Watson  * the malloc(9) implementation has its own data structure describing the
1760ce3f16dSRobert Watson  * type and statistics.  This permits the malloc(9)-internal data structures
1770ce3f16dSRobert Watson  * to be modified without breaking binary-compiled kernel modules that
1780ce3f16dSRobert Watson  * declare malloc types.
1790ce3f16dSRobert Watson  */
18063a7e0a3SRobert Watson static uma_zone_t mt_zone;
18163a7e0a3SRobert Watson 
182b89eaf4eSAlan Cox u_long vm_kmem_size;
183d801e824SAndriy Gapon SYSCTL_ULONG(_vm, OID_AUTO, kmem_size, CTLFLAG_RDTUN, &vm_kmem_size, 0,
18484344f9fSDag-Erling Smørgrav     "Size of kernel memory");
1855a34a9f0SJeff Roberson 
186b89eaf4eSAlan Cox static u_long vm_kmem_size_min;
187d801e824SAndriy Gapon SYSCTL_ULONG(_vm, OID_AUTO, kmem_size_min, CTLFLAG_RDTUN, &vm_kmem_size_min, 0,
1880e5179e4SStephane E. Potvin     "Minimum size of kernel memory");
1890e5179e4SStephane E. Potvin 
190b89eaf4eSAlan Cox static u_long vm_kmem_size_max;
191d801e824SAndriy Gapon SYSCTL_ULONG(_vm, OID_AUTO, kmem_size_max, CTLFLAG_RDTUN, &vm_kmem_size_max, 0,
192479439b4SDag-Erling Smørgrav     "Maximum size of kernel memory");
193479439b4SDag-Erling Smørgrav 
194b89eaf4eSAlan Cox static u_int vm_kmem_size_scale;
195d801e824SAndriy Gapon SYSCTL_UINT(_vm, OID_AUTO, kmem_size_scale, CTLFLAG_RDTUN, &vm_kmem_size_scale, 0,
196479439b4SDag-Erling Smørgrav     "Scale factor for kernel memory size");
197479439b4SDag-Erling Smørgrav 
1987814c80aSAndriy Gapon static int sysctl_kmem_map_size(SYSCTL_HANDLER_ARGS);
1997814c80aSAndriy Gapon SYSCTL_PROC(_vm, OID_AUTO, kmem_map_size,
2007814c80aSAndriy Gapon     CTLFLAG_RD | CTLTYPE_ULONG | CTLFLAG_MPSAFE, NULL, 0,
2015df87b21SJeff Roberson     sysctl_kmem_map_size, "LU", "Current kmem allocation size");
2027814c80aSAndriy Gapon 
20395bb9d38SAndriy Gapon static int sysctl_kmem_map_free(SYSCTL_HANDLER_ARGS);
20495bb9d38SAndriy Gapon SYSCTL_PROC(_vm, OID_AUTO, kmem_map_free,
20595bb9d38SAndriy Gapon     CTLFLAG_RD | CTLTYPE_ULONG | CTLFLAG_MPSAFE, NULL, 0,
2065df87b21SJeff Roberson     sysctl_kmem_map_free, "LU", "Free space in kmem");
20795bb9d38SAndriy Gapon 
2085a34a9f0SJeff Roberson /*
20999571dc3SJeff Roberson  * The malloc_mtx protects the kmemstatistics linked list.
2105a34a9f0SJeff Roberson  */
2115a34a9f0SJeff Roberson struct mtx malloc_mtx;
21269ef67f9SJason Evans 
2135e914b96SJeff Roberson #ifdef MALLOC_PROFILE
2145e914b96SJeff Roberson uint64_t krequests[KMEM_ZSIZE + 1];
2156f267175SJeff Roberson 
2165e914b96SJeff Roberson static int sysctl_kern_mprof(SYSCTL_HANDLER_ARGS);
2175e914b96SJeff Roberson #endif
2185e914b96SJeff Roberson 
219cd814b26SRobert Watson static int sysctl_kern_malloc_stats(SYSCTL_HANDLER_ARGS);
220df8bae1dSRodney W. Grimes 
2210ce3f16dSRobert Watson /*
2220ce3f16dSRobert Watson  * time_uptime of the last malloc(9) failure (induced or real).
2230ce3f16dSRobert Watson  */
2241fb14a47SPoul-Henning Kamp static time_t t_malloc_fail;
2251fb14a47SPoul-Henning Kamp 
226d7854da1SMatthew D Fleming #if defined(MALLOC_MAKE_FAILURES) || (MALLOC_DEBUG_MAXZONES > 1)
2276472ac3dSEd Schouten static SYSCTL_NODE(_debug, OID_AUTO, malloc, CTLFLAG_RD, 0,
228d7854da1SMatthew D Fleming     "Kernel malloc debugging options");
229d7854da1SMatthew D Fleming #endif
230d7854da1SMatthew D Fleming 
231eae870cdSRobert Watson /*
2320ce3f16dSRobert Watson  * malloc(9) fault injection -- cause malloc failures every (n) mallocs when
2330ce3f16dSRobert Watson  * the caller specifies M_NOWAIT.  If set to 0, no failures are caused.
234eae870cdSRobert Watson  */
2350ce3f16dSRobert Watson #ifdef MALLOC_MAKE_FAILURES
236eae870cdSRobert Watson static int malloc_failure_rate;
237eae870cdSRobert Watson static int malloc_nowait_count;
238eae870cdSRobert Watson static int malloc_failure_count;
239eae870cdSRobert Watson SYSCTL_INT(_debug_malloc, OID_AUTO, failure_rate, CTLFLAG_RW,
240eae870cdSRobert Watson     &malloc_failure_rate, 0, "Every (n) mallocs with M_NOWAIT will fail");
241f2538508SRobert Watson TUNABLE_INT("debug.malloc.failure_rate", &malloc_failure_rate);
242eae870cdSRobert Watson SYSCTL_INT(_debug_malloc, OID_AUTO, failure_count, CTLFLAG_RD,
243eae870cdSRobert Watson     &malloc_failure_count, 0, "Number of imposed M_NOWAIT malloc failures");
244eae870cdSRobert Watson #endif
245eae870cdSRobert Watson 
2467814c80aSAndriy Gapon static int
2477814c80aSAndriy Gapon sysctl_kmem_map_size(SYSCTL_HANDLER_ARGS)
2487814c80aSAndriy Gapon {
2497814c80aSAndriy Gapon 	u_long size;
2507814c80aSAndriy Gapon 
2515df87b21SJeff Roberson 	size = vmem_size(kmem_arena, VMEM_ALLOC);
2527814c80aSAndriy Gapon 	return (sysctl_handle_long(oidp, &size, 0, req));
2537814c80aSAndriy Gapon }
2547814c80aSAndriy Gapon 
25595bb9d38SAndriy Gapon static int
25695bb9d38SAndriy Gapon sysctl_kmem_map_free(SYSCTL_HANDLER_ARGS)
25795bb9d38SAndriy Gapon {
25895bb9d38SAndriy Gapon 	u_long size;
25995bb9d38SAndriy Gapon 
2605df87b21SJeff Roberson 	size = vmem_size(kmem_arena, VMEM_FREE);
26195bb9d38SAndriy Gapon 	return (sysctl_handle_long(oidp, &size, 0, req));
26295bb9d38SAndriy Gapon }
26395bb9d38SAndriy Gapon 
264d7854da1SMatthew D Fleming /*
265d7854da1SMatthew D Fleming  * malloc(9) uma zone separation -- sub-page buffer overruns in one
266d7854da1SMatthew D Fleming  * malloc type will affect only a subset of other malloc types.
267d7854da1SMatthew D Fleming  */
268d7854da1SMatthew D Fleming #if MALLOC_DEBUG_MAXZONES > 1
269d7854da1SMatthew D Fleming static void
270d7854da1SMatthew D Fleming tunable_set_numzones(void)
271d7854da1SMatthew D Fleming {
272d7854da1SMatthew D Fleming 
273d7854da1SMatthew D Fleming 	TUNABLE_INT_FETCH("debug.malloc.numzones",
274d7854da1SMatthew D Fleming 	    &numzones);
275d7854da1SMatthew D Fleming 
276d7854da1SMatthew D Fleming 	/* Sanity check the number of malloc uma zones. */
277d7854da1SMatthew D Fleming 	if (numzones <= 0)
278d7854da1SMatthew D Fleming 		numzones = 1;
279d7854da1SMatthew D Fleming 	if (numzones > MALLOC_DEBUG_MAXZONES)
280d7854da1SMatthew D Fleming 		numzones = MALLOC_DEBUG_MAXZONES;
281d7854da1SMatthew D Fleming }
282d7854da1SMatthew D Fleming SYSINIT(numzones, SI_SUB_TUNABLES, SI_ORDER_ANY, tunable_set_numzones, NULL);
283d7854da1SMatthew D Fleming SYSCTL_INT(_debug_malloc, OID_AUTO, numzones, CTLFLAG_RDTUN,
284d7854da1SMatthew D Fleming     &numzones, 0, "Number of malloc uma subzones");
285d7854da1SMatthew D Fleming 
286d7854da1SMatthew D Fleming /*
287d7854da1SMatthew D Fleming  * Any number that changes regularly is an okay choice for the
288d7854da1SMatthew D Fleming  * offset.  Build numbers are pretty good of you have them.
289d7854da1SMatthew D Fleming  */
290d7854da1SMatthew D Fleming static u_int zone_offset = __FreeBSD_version;
291d7854da1SMatthew D Fleming TUNABLE_INT("debug.malloc.zone_offset", &zone_offset);
292d7854da1SMatthew D Fleming SYSCTL_UINT(_debug_malloc, OID_AUTO, zone_offset, CTLFLAG_RDTUN,
293d7854da1SMatthew D Fleming     &zone_offset, 0, "Separate malloc types by examining the "
294d7854da1SMatthew D Fleming     "Nth character in the malloc type short description.");
295d7854da1SMatthew D Fleming 
296d7854da1SMatthew D Fleming static u_int
297d7854da1SMatthew D Fleming mtp_get_subzone(const char *desc)
298d7854da1SMatthew D Fleming {
299d7854da1SMatthew D Fleming 	size_t len;
300d7854da1SMatthew D Fleming 	u_int val;
301d7854da1SMatthew D Fleming 
302d7854da1SMatthew D Fleming 	if (desc == NULL || (len = strlen(desc)) == 0)
303d7854da1SMatthew D Fleming 		return (0);
304d7854da1SMatthew D Fleming 	val = desc[zone_offset % len];
305d7854da1SMatthew D Fleming 	return (val % numzones);
306d7854da1SMatthew D Fleming }
307d7854da1SMatthew D Fleming #elif MALLOC_DEBUG_MAXZONES == 0
308d7854da1SMatthew D Fleming #error "MALLOC_DEBUG_MAXZONES must be positive."
309d7854da1SMatthew D Fleming #else
310d7854da1SMatthew D Fleming static inline u_int
311d7854da1SMatthew D Fleming mtp_get_subzone(const char *desc)
312d7854da1SMatthew D Fleming {
313d7854da1SMatthew D Fleming 
314d7854da1SMatthew D Fleming 	return (0);
315d7854da1SMatthew D Fleming }
316d7854da1SMatthew D Fleming #endif /* MALLOC_DEBUG_MAXZONES > 1 */
317d7854da1SMatthew D Fleming 
3181fb14a47SPoul-Henning Kamp int
3191fb14a47SPoul-Henning Kamp malloc_last_fail(void)
3201fb14a47SPoul-Henning Kamp {
3211fb14a47SPoul-Henning Kamp 
3221fb14a47SPoul-Henning Kamp 	return (time_uptime - t_malloc_fail);
3231fb14a47SPoul-Henning Kamp }
3241fb14a47SPoul-Henning Kamp 
325df8bae1dSRodney W. Grimes /*
3260ce3f16dSRobert Watson  * An allocation has succeeded -- update malloc type statistics for the
3270ce3f16dSRobert Watson  * amount of bucket size.  Occurs within a critical section so that the
3280ce3f16dSRobert Watson  * thread isn't preempted and doesn't migrate while updating per-PCU
3290ce3f16dSRobert Watson  * statistics.
3304362fadaSBrian Feldman  */
3314362fadaSBrian Feldman static void
33263a7e0a3SRobert Watson malloc_type_zone_allocated(struct malloc_type *mtp, unsigned long size,
3334362fadaSBrian Feldman     int zindx)
3344362fadaSBrian Feldman {
33563a7e0a3SRobert Watson 	struct malloc_type_internal *mtip;
33663a7e0a3SRobert Watson 	struct malloc_type_stats *mtsp;
33763a7e0a3SRobert Watson 
33863a7e0a3SRobert Watson 	critical_enter();
33963a7e0a3SRobert Watson 	mtip = mtp->ks_handle;
34063a7e0a3SRobert Watson 	mtsp = &mtip->mti_stats[curcpu];
34173864adbSPawel Jakub Dawidek 	if (size > 0) {
34263a7e0a3SRobert Watson 		mtsp->mts_memalloced += size;
34363a7e0a3SRobert Watson 		mtsp->mts_numallocs++;
34473864adbSPawel Jakub Dawidek 	}
3454362fadaSBrian Feldman 	if (zindx != -1)
34663a7e0a3SRobert Watson 		mtsp->mts_size |= 1 << zindx;
34791dd776cSJohn Birrell 
34891dd776cSJohn Birrell #ifdef KDTRACE_HOOKS
34991dd776cSJohn Birrell 	if (dtrace_malloc_probe != NULL) {
35091dd776cSJohn Birrell 		uint32_t probe_id = mtip->mti_probes[DTMALLOC_PROBE_MALLOC];
35191dd776cSJohn Birrell 		if (probe_id != 0)
35291dd776cSJohn Birrell 			(dtrace_malloc_probe)(probe_id,
35391dd776cSJohn Birrell 			    (uintptr_t) mtp, (uintptr_t) mtip,
35491dd776cSJohn Birrell 			    (uintptr_t) mtsp, size, zindx);
35591dd776cSJohn Birrell 	}
35691dd776cSJohn Birrell #endif
35791dd776cSJohn Birrell 
35863a7e0a3SRobert Watson 	critical_exit();
3594362fadaSBrian Feldman }
3604362fadaSBrian Feldman 
3614362fadaSBrian Feldman void
36263a7e0a3SRobert Watson malloc_type_allocated(struct malloc_type *mtp, unsigned long size)
3634362fadaSBrian Feldman {
36463a7e0a3SRobert Watson 
36573864adbSPawel Jakub Dawidek 	if (size > 0)
36663a7e0a3SRobert Watson 		malloc_type_zone_allocated(mtp, size, -1);
3674362fadaSBrian Feldman }
3684362fadaSBrian Feldman 
3694362fadaSBrian Feldman /*
3703805385eSRobert Watson  * A free operation has occurred -- update malloc type statistics for the
3710ce3f16dSRobert Watson  * amount of the bucket size.  Occurs within a critical section so that the
3720ce3f16dSRobert Watson  * thread isn't preempted and doesn't migrate while updating per-CPU
3730ce3f16dSRobert Watson  * statistics.
3744362fadaSBrian Feldman  */
3754362fadaSBrian Feldman void
37663a7e0a3SRobert Watson malloc_type_freed(struct malloc_type *mtp, unsigned long size)
3774362fadaSBrian Feldman {
37863a7e0a3SRobert Watson 	struct malloc_type_internal *mtip;
37963a7e0a3SRobert Watson 	struct malloc_type_stats *mtsp;
38063a7e0a3SRobert Watson 
38163a7e0a3SRobert Watson 	critical_enter();
38263a7e0a3SRobert Watson 	mtip = mtp->ks_handle;
38363a7e0a3SRobert Watson 	mtsp = &mtip->mti_stats[curcpu];
38463a7e0a3SRobert Watson 	mtsp->mts_memfreed += size;
38563a7e0a3SRobert Watson 	mtsp->mts_numfrees++;
38691dd776cSJohn Birrell 
38791dd776cSJohn Birrell #ifdef KDTRACE_HOOKS
38891dd776cSJohn Birrell 	if (dtrace_malloc_probe != NULL) {
38991dd776cSJohn Birrell 		uint32_t probe_id = mtip->mti_probes[DTMALLOC_PROBE_FREE];
39091dd776cSJohn Birrell 		if (probe_id != 0)
39191dd776cSJohn Birrell 			(dtrace_malloc_probe)(probe_id,
39291dd776cSJohn Birrell 			    (uintptr_t) mtp, (uintptr_t) mtip,
39391dd776cSJohn Birrell 			    (uintptr_t) mtsp, size, 0);
39491dd776cSJohn Birrell 	}
39591dd776cSJohn Birrell #endif
39691dd776cSJohn Birrell 
39763a7e0a3SRobert Watson 	critical_exit();
3984362fadaSBrian Feldman }
3994362fadaSBrian Feldman 
4004362fadaSBrian Feldman /*
401f346986bSAlan Cox  *	contigmalloc:
402f346986bSAlan Cox  *
403f346986bSAlan Cox  *	Allocate a block of physically contiguous memory.
404f346986bSAlan Cox  *
405f346986bSAlan Cox  *	If M_NOWAIT is set, this routine will not block and return NULL if
406f346986bSAlan Cox  *	the allocation fails.
407f346986bSAlan Cox  */
408f346986bSAlan Cox void *
409f346986bSAlan Cox contigmalloc(unsigned long size, struct malloc_type *type, int flags,
410f346986bSAlan Cox     vm_paddr_t low, vm_paddr_t high, unsigned long alignment,
411831ce4cbSJohn Baldwin     vm_paddr_t boundary)
412f346986bSAlan Cox {
413f346986bSAlan Cox 	void *ret;
414f346986bSAlan Cox 
4155df87b21SJeff Roberson 	ret = (void *)kmem_alloc_contig(kernel_arena, size, flags, low, high,
416f346986bSAlan Cox 	    alignment, boundary, VM_MEMATTR_DEFAULT);
417f346986bSAlan Cox 	if (ret != NULL)
418f346986bSAlan Cox 		malloc_type_allocated(type, round_page(size));
419f346986bSAlan Cox 	return (ret);
420f346986bSAlan Cox }
421f346986bSAlan Cox 
422f346986bSAlan Cox /*
423f346986bSAlan Cox  *	contigfree:
424f346986bSAlan Cox  *
425f346986bSAlan Cox  *	Free a block of memory allocated by contigmalloc.
426f346986bSAlan Cox  *
427f346986bSAlan Cox  *	This routine may not block.
428f346986bSAlan Cox  */
429f346986bSAlan Cox void
430f346986bSAlan Cox contigfree(void *addr, unsigned long size, struct malloc_type *type)
431f346986bSAlan Cox {
432f346986bSAlan Cox 
4335df87b21SJeff Roberson 	kmem_free(kernel_arena, (vm_offset_t)addr, size);
434f346986bSAlan Cox 	malloc_type_freed(type, round_page(size));
435f346986bSAlan Cox }
436f346986bSAlan Cox 
437f346986bSAlan Cox /*
4381c7c3c6aSMatthew Dillon  *	malloc:
4391c7c3c6aSMatthew Dillon  *
4401c7c3c6aSMatthew Dillon  *	Allocate a block of memory.
4411c7c3c6aSMatthew Dillon  *
4421c7c3c6aSMatthew Dillon  *	If M_NOWAIT is set, this routine will not block and return NULL if
4431c7c3c6aSMatthew Dillon  *	the allocation fails.
444df8bae1dSRodney W. Grimes  */
445df8bae1dSRodney W. Grimes void *
44663a7e0a3SRobert Watson malloc(unsigned long size, struct malloc_type *mtp, int flags)
447df8bae1dSRodney W. Grimes {
4486f267175SJeff Roberson 	int indx;
449d7854da1SMatthew D Fleming 	struct malloc_type_internal *mtip;
4508355f576SJeff Roberson 	caddr_t va;
4518355f576SJeff Roberson 	uma_zone_t zone;
452847a2a17SPawel Jakub Dawidek #if defined(DIAGNOSTIC) || defined(DEBUG_REDZONE)
4534db4f5c8SPoul-Henning Kamp 	unsigned long osize = size;
4544db4f5c8SPoul-Henning Kamp #endif
455df8bae1dSRodney W. Grimes 
456194a0abfSPoul-Henning Kamp #ifdef INVARIANTS
457bb1c7df8SRobert Watson 	KASSERT(mtp->ks_magic == M_MAGIC, ("malloc: bad malloc type magic"));
458d3c11994SPoul-Henning Kamp 	/*
45923198357SRuslan Ermilov 	 * Check that exactly one of M_WAITOK or M_NOWAIT is specified.
460d3c11994SPoul-Henning Kamp 	 */
46123198357SRuslan Ermilov 	indx = flags & (M_WAITOK | M_NOWAIT);
462d3c11994SPoul-Henning Kamp 	if (indx != M_NOWAIT && indx != M_WAITOK) {
463d3c11994SPoul-Henning Kamp 		static	struct timeval lasterr;
464d3c11994SPoul-Henning Kamp 		static	int curerr, once;
465d3c11994SPoul-Henning Kamp 		if (once == 0 && ppsratecheck(&lasterr, &curerr, 1)) {
466d3c11994SPoul-Henning Kamp 			printf("Bad malloc flags: %x\n", indx);
4672d50560aSMarcel Moolenaar 			kdb_backtrace();
468d3c11994SPoul-Henning Kamp 			flags |= M_WAITOK;
469d3c11994SPoul-Henning Kamp 			once++;
470d3c11994SPoul-Henning Kamp 		}
471d3c11994SPoul-Henning Kamp 	}
472194a0abfSPoul-Henning Kamp #endif
473eae870cdSRobert Watson #ifdef MALLOC_MAKE_FAILURES
474eae870cdSRobert Watson 	if ((flags & M_NOWAIT) && (malloc_failure_rate != 0)) {
475eae870cdSRobert Watson 		atomic_add_int(&malloc_nowait_count, 1);
476eae870cdSRobert Watson 		if ((malloc_nowait_count % malloc_failure_rate) == 0) {
477eae870cdSRobert Watson 			atomic_add_int(&malloc_failure_count, 1);
4783f6ee876SPoul-Henning Kamp 			t_malloc_fail = time_uptime;
479eae870cdSRobert Watson 			return (NULL);
480eae870cdSRobert Watson 		}
481eae870cdSRobert Watson 	}
482eae870cdSRobert Watson #endif
483d3c11994SPoul-Henning Kamp 	if (flags & M_WAITOK)
484b40ce416SJulian Elischer 		KASSERT(curthread->td_intr_nesting_level == 0,
485a163d034SWarner Losh 		   ("malloc(M_WAITOK) in interrupt context"));
486e4eb384bSBosko Milekic 
487e4eb384bSBosko Milekic #ifdef DEBUG_MEMGUARD
4888d689e04SGleb Smirnoff 	if (memguard_cmp_mtp(mtp, size)) {
489e3813573SMatthew D Fleming 		va = memguard_alloc(size, flags);
490e3813573SMatthew D Fleming 		if (va != NULL)
491e3813573SMatthew D Fleming 			return (va);
492e3813573SMatthew D Fleming 		/* This is unfortunate but should not be fatal. */
493e3813573SMatthew D Fleming 	}
494e4eb384bSBosko Milekic #endif
495e4eb384bSBosko Milekic 
496847a2a17SPawel Jakub Dawidek #ifdef DEBUG_REDZONE
497847a2a17SPawel Jakub Dawidek 	size = redzone_size_ntor(size);
498847a2a17SPawel Jakub Dawidek #endif
499847a2a17SPawel Jakub Dawidek 
5008355f576SJeff Roberson 	if (size <= KMEM_ZMAX) {
501d7854da1SMatthew D Fleming 		mtip = mtp->ks_handle;
5026f267175SJeff Roberson 		if (size & KMEM_ZMASK)
5036f267175SJeff Roberson 			size = (size & ~KMEM_ZMASK) + KMEM_ZBASE;
5046f267175SJeff Roberson 		indx = kmemsize[size >> KMEM_ZSHIFT];
505d7854da1SMatthew D Fleming 		KASSERT(mtip->mti_zone < numzones,
506d7854da1SMatthew D Fleming 		    ("mti_zone %u out of range %d",
507d7854da1SMatthew D Fleming 		    mtip->mti_zone, numzones));
508d7854da1SMatthew D Fleming 		zone = kmemzones[indx].kz_zone[mtip->mti_zone];
5096f267175SJeff Roberson #ifdef MALLOC_PROFILE
5106f267175SJeff Roberson 		krequests[size >> KMEM_ZSHIFT]++;
5116f267175SJeff Roberson #endif
5128355f576SJeff Roberson 		va = uma_zalloc(zone, flags);
5134362fadaSBrian Feldman 		if (va != NULL)
514e20a199fSJeff Roberson 			size = zone->uz_size;
51563a7e0a3SRobert Watson 		malloc_type_zone_allocated(mtp, va == NULL ? 0 : size, indx);
5168355f576SJeff Roberson 	} else {
5176f267175SJeff Roberson 		size = roundup(size, PAGE_SIZE);
5188355f576SJeff Roberson 		zone = NULL;
5198355f576SJeff Roberson 		va = uma_large_malloc(size, flags);
52063a7e0a3SRobert Watson 		malloc_type_allocated(mtp, va == NULL ? 0 : size);
521df8bae1dSRodney W. Grimes 	}
5221282e9acSPoul-Henning Kamp 	if (flags & M_WAITOK)
523a163d034SWarner Losh 		KASSERT(va != NULL, ("malloc(M_WAITOK) returned NULL"));
5241282e9acSPoul-Henning Kamp 	else if (va == NULL)
5251fb14a47SPoul-Henning Kamp 		t_malloc_fail = time_uptime;
5264db4f5c8SPoul-Henning Kamp #ifdef DIAGNOSTIC
5271282e9acSPoul-Henning Kamp 	if (va != NULL && !(flags & M_ZERO)) {
5284db4f5c8SPoul-Henning Kamp 		memset(va, 0x70, osize);
5294db4f5c8SPoul-Henning Kamp 	}
5304db4f5c8SPoul-Henning Kamp #endif
531847a2a17SPawel Jakub Dawidek #ifdef DEBUG_REDZONE
532847a2a17SPawel Jakub Dawidek 	if (va != NULL)
533847a2a17SPawel Jakub Dawidek 		va = redzone_setup(va, osize);
534847a2a17SPawel Jakub Dawidek #endif
535df8bae1dSRodney W. Grimes 	return ((void *) va);
536df8bae1dSRodney W. Grimes }
537df8bae1dSRodney W. Grimes 
538df8bae1dSRodney W. Grimes /*
5391c7c3c6aSMatthew Dillon  *	free:
5401c7c3c6aSMatthew Dillon  *
541df8bae1dSRodney W. Grimes  *	Free a block of memory allocated by malloc.
5421c7c3c6aSMatthew Dillon  *
5431c7c3c6aSMatthew Dillon  *	This routine may not block.
544df8bae1dSRodney W. Grimes  */
545df8bae1dSRodney W. Grimes void
54663a7e0a3SRobert Watson free(void *addr, struct malloc_type *mtp)
547df8bae1dSRodney W. Grimes {
54899571dc3SJeff Roberson 	uma_slab_t slab;
54999571dc3SJeff Roberson 	u_long size;
550254c6cb3SPoul-Henning Kamp 
551bb1c7df8SRobert Watson 	KASSERT(mtp->ks_magic == M_MAGIC, ("free: bad malloc type magic"));
552bb1c7df8SRobert Watson 
55344a8ff31SArchie Cobbs 	/* free(NULL, ...) does nothing */
55444a8ff31SArchie Cobbs 	if (addr == NULL)
55544a8ff31SArchie Cobbs 		return;
55644a8ff31SArchie Cobbs 
557e4eb384bSBosko Milekic #ifdef DEBUG_MEMGUARD
558e3813573SMatthew D Fleming 	if (is_memguard_addr(addr)) {
559e4eb384bSBosko Milekic 		memguard_free(addr);
560e4eb384bSBosko Milekic 		return;
561e4eb384bSBosko Milekic 	}
562e4eb384bSBosko Milekic #endif
563e4eb384bSBosko Milekic 
564847a2a17SPawel Jakub Dawidek #ifdef DEBUG_REDZONE
565847a2a17SPawel Jakub Dawidek 	redzone_check(addr);
566847a2a17SPawel Jakub Dawidek 	addr = redzone_addr_ntor(addr);
567847a2a17SPawel Jakub Dawidek #endif
568847a2a17SPawel Jakub Dawidek 
56999571dc3SJeff Roberson 	slab = vtoslab((vm_offset_t)addr & (~UMA_SLAB_MASK));
5708355f576SJeff Roberson 
5718355f576SJeff Roberson 	if (slab == NULL)
5726f267175SJeff Roberson 		panic("free: address %p(%p) has not been allocated.\n",
57399571dc3SJeff Roberson 		    addr, (void *)((u_long)addr & (~UMA_SLAB_MASK)));
57499571dc3SJeff Roberson 
5758355f576SJeff Roberson 	if (!(slab->us_flags & UMA_SLAB_MALLOC)) {
5768f70816cSJeff Roberson #ifdef INVARIANTS
57763a7e0a3SRobert Watson 		struct malloc_type **mtpp = addr;
5788f70816cSJeff Roberson #endif
579099a0e58SBosko Milekic 		size = slab->us_keg->uk_size;
5808f70816cSJeff Roberson #ifdef INVARIANTS
5818f70816cSJeff Roberson 		/*
5828f70816cSJeff Roberson 		 * Cache a pointer to the malloc_type that most recently freed
5838f70816cSJeff Roberson 		 * this memory here.  This way we know who is most likely to
5848f70816cSJeff Roberson 		 * have stepped on it later.
5858f70816cSJeff Roberson 		 *
5868f70816cSJeff Roberson 		 * This code assumes that size is a multiple of 8 bytes for
5878f70816cSJeff Roberson 		 * 64 bit machines
5888f70816cSJeff Roberson 		 */
58963a7e0a3SRobert Watson 		mtpp = (struct malloc_type **)
59063a7e0a3SRobert Watson 		    ((unsigned long)mtpp & ~UMA_ALIGN_PTR);
59163a7e0a3SRobert Watson 		mtpp += (size - sizeof(struct malloc_type *)) /
5928f70816cSJeff Roberson 		    sizeof(struct malloc_type *);
59363a7e0a3SRobert Watson 		*mtpp = mtp;
5948f70816cSJeff Roberson #endif
595099a0e58SBosko Milekic 		uma_zfree_arg(LIST_FIRST(&slab->us_keg->uk_zones), addr, slab);
59614bf02f8SJohn Dyson 	} else {
5978355f576SJeff Roberson 		size = slab->us_size;
5988355f576SJeff Roberson 		uma_large_free(slab);
59914bf02f8SJohn Dyson 	}
60063a7e0a3SRobert Watson 	malloc_type_freed(mtp, size);
601df8bae1dSRodney W. Grimes }
602df8bae1dSRodney W. Grimes 
603df8bae1dSRodney W. Grimes /*
60444a8ff31SArchie Cobbs  *	realloc: change the size of a memory block
60544a8ff31SArchie Cobbs  */
60644a8ff31SArchie Cobbs void *
60763a7e0a3SRobert Watson realloc(void *addr, unsigned long size, struct malloc_type *mtp, int flags)
60844a8ff31SArchie Cobbs {
6098355f576SJeff Roberson 	uma_slab_t slab;
61044a8ff31SArchie Cobbs 	unsigned long alloc;
61144a8ff31SArchie Cobbs 	void *newaddr;
61244a8ff31SArchie Cobbs 
613bb1c7df8SRobert Watson 	KASSERT(mtp->ks_magic == M_MAGIC,
614bb1c7df8SRobert Watson 	    ("realloc: bad malloc type magic"));
615bb1c7df8SRobert Watson 
61644a8ff31SArchie Cobbs 	/* realloc(NULL, ...) is equivalent to malloc(...) */
61744a8ff31SArchie Cobbs 	if (addr == NULL)
61863a7e0a3SRobert Watson 		return (malloc(size, mtp, flags));
61963a7e0a3SRobert Watson 
62063a7e0a3SRobert Watson 	/*
62163a7e0a3SRobert Watson 	 * XXX: Should report free of old memory and alloc of new memory to
62263a7e0a3SRobert Watson 	 * per-CPU stats.
62363a7e0a3SRobert Watson 	 */
62444a8ff31SArchie Cobbs 
625e4eb384bSBosko Milekic #ifdef DEBUG_MEMGUARD
6266d3ed393SMatthew D Fleming 	if (is_memguard_addr(addr))
6276d3ed393SMatthew D Fleming 		return (memguard_realloc(addr, size, mtp, flags));
628e4eb384bSBosko Milekic #endif
629e4eb384bSBosko Milekic 
630847a2a17SPawel Jakub Dawidek #ifdef DEBUG_REDZONE
631847a2a17SPawel Jakub Dawidek 	slab = NULL;
632847a2a17SPawel Jakub Dawidek 	alloc = redzone_get_size(addr);
633847a2a17SPawel Jakub Dawidek #else
63499571dc3SJeff Roberson 	slab = vtoslab((vm_offset_t)addr & ~(UMA_SLAB_MASK));
6358355f576SJeff Roberson 
63644a8ff31SArchie Cobbs 	/* Sanity check */
6378355f576SJeff Roberson 	KASSERT(slab != NULL,
63844a8ff31SArchie Cobbs 	    ("realloc: address %p out of range", (void *)addr));
63944a8ff31SArchie Cobbs 
64044a8ff31SArchie Cobbs 	/* Get the size of the original block */
641619f2841SPawel Jakub Dawidek 	if (!(slab->us_flags & UMA_SLAB_MALLOC))
642099a0e58SBosko Milekic 		alloc = slab->us_keg->uk_size;
6438355f576SJeff Roberson 	else
6448355f576SJeff Roberson 		alloc = slab->us_size;
64544a8ff31SArchie Cobbs 
64644a8ff31SArchie Cobbs 	/* Reuse the original block if appropriate */
64744a8ff31SArchie Cobbs 	if (size <= alloc
64844a8ff31SArchie Cobbs 	    && (size > (alloc >> REALLOC_FRACTION) || alloc == MINALLOCSIZE))
64944a8ff31SArchie Cobbs 		return (addr);
650847a2a17SPawel Jakub Dawidek #endif /* !DEBUG_REDZONE */
65144a8ff31SArchie Cobbs 
65244a8ff31SArchie Cobbs 	/* Allocate a new, bigger (or smaller) block */
65363a7e0a3SRobert Watson 	if ((newaddr = malloc(size, mtp, flags)) == NULL)
65444a8ff31SArchie Cobbs 		return (NULL);
65544a8ff31SArchie Cobbs 
65644a8ff31SArchie Cobbs 	/* Copy over original contents */
65744a8ff31SArchie Cobbs 	bcopy(addr, newaddr, min(size, alloc));
65863a7e0a3SRobert Watson 	free(addr, mtp);
65944a8ff31SArchie Cobbs 	return (newaddr);
66044a8ff31SArchie Cobbs }
66144a8ff31SArchie Cobbs 
66244a8ff31SArchie Cobbs /*
66344a8ff31SArchie Cobbs  *	reallocf: same as realloc() but free memory on failure.
66444a8ff31SArchie Cobbs  */
66544a8ff31SArchie Cobbs void *
66663a7e0a3SRobert Watson reallocf(void *addr, unsigned long size, struct malloc_type *mtp, int flags)
66744a8ff31SArchie Cobbs {
66844a8ff31SArchie Cobbs 	void *mem;
66944a8ff31SArchie Cobbs 
67063a7e0a3SRobert Watson 	if ((mem = realloc(addr, size, mtp, flags)) == NULL)
67163a7e0a3SRobert Watson 		free(addr, mtp);
67244a8ff31SArchie Cobbs 	return (mem);
67344a8ff31SArchie Cobbs }
67444a8ff31SArchie Cobbs 
67544a8ff31SArchie Cobbs /*
6765df87b21SJeff Roberson  * Wake the page daemon when we exhaust KVA.  It will call the lowmem handler
6775df87b21SJeff Roberson  * and uma_reclaim() callbacks in a context that is safe.
678df8bae1dSRodney W. Grimes  */
6792b14f991SJulian Elischer static void
6805df87b21SJeff Roberson kmem_reclaim(vmem_t *vm, int flags)
681df8bae1dSRodney W. Grimes {
6828a58a9f6SJohn Dyson 
6835df87b21SJeff Roberson 	pagedaemon_wakeup();
6845df87b21SJeff Roberson }
6855df87b21SJeff Roberson 
686f9d498adSDimitry Andric #ifndef __sparc64__
687c70af487SAlan Cox CTASSERT(VM_KMEM_SIZE_SCALE >= 1);
688f9d498adSDimitry Andric #endif
689c70af487SAlan Cox 
6905df87b21SJeff Roberson /*
691c70af487SAlan Cox  * Initialize the kernel memory (kmem) arena.
6925df87b21SJeff Roberson  */
6935df87b21SJeff Roberson void
6945df87b21SJeff Roberson kmeminit(void)
6955df87b21SJeff Roberson {
6965df87b21SJeff Roberson 	u_long mem_size, tmp;
69769ef67f9SJason Evans 
6988a58a9f6SJohn Dyson 	/*
699c70af487SAlan Cox 	 * Calculate the amount of kernel virtual address (KVA) space that is
700c70af487SAlan Cox 	 * preallocated to the kmem arena.  In order to support a wide range
701c70af487SAlan Cox 	 * of machines, it is a function of the physical memory size,
702c70af487SAlan Cox 	 * specifically,
7038a58a9f6SJohn Dyson 	 *
704c70af487SAlan Cox 	 *	min(max(physical memory size / VM_KMEM_SIZE_SCALE,
705c70af487SAlan Cox 	 *	    VM_KMEM_SIZE_MIN), VM_KMEM_SIZE_MAX)
706c70af487SAlan Cox 	 *
707c70af487SAlan Cox 	 * Every architecture must define an integral value for
708c70af487SAlan Cox 	 * VM_KMEM_SIZE_SCALE.  However, the definitions of VM_KMEM_SIZE_MIN
709c70af487SAlan Cox 	 * and VM_KMEM_SIZE_MAX, which represent respectively the floor and
710c70af487SAlan Cox 	 * ceiling on this preallocation, are optional.  Typically,
711c70af487SAlan Cox 	 * VM_KMEM_SIZE_MAX is itself a function of the available KVA space on
712c70af487SAlan Cox 	 * a given architecture.
7138a58a9f6SJohn Dyson 	 */
714*44f1c916SBryan Drewery 	mem_size = vm_cnt.v_page_count;
7158a58a9f6SJohn Dyson 
716479439b4SDag-Erling Smørgrav 	vm_kmem_size_scale = VM_KMEM_SIZE_SCALE;
717479439b4SDag-Erling Smørgrav 	TUNABLE_INT_FETCH("vm.kmem_size_scale", &vm_kmem_size_scale);
718c70af487SAlan Cox 	if (vm_kmem_size_scale < 1)
719c70af487SAlan Cox 		vm_kmem_size_scale = VM_KMEM_SIZE_SCALE;
720c70af487SAlan Cox 
721479439b4SDag-Erling Smørgrav 	vm_kmem_size = (mem_size / vm_kmem_size_scale) * PAGE_SIZE;
7228a58a9f6SJohn Dyson 
7230e5179e4SStephane E. Potvin #if defined(VM_KMEM_SIZE_MIN)
7240e5179e4SStephane E. Potvin 	vm_kmem_size_min = VM_KMEM_SIZE_MIN;
7250e5179e4SStephane E. Potvin #endif
726b89eaf4eSAlan Cox 	TUNABLE_ULONG_FETCH("vm.kmem_size_min", &vm_kmem_size_min);
727c70af487SAlan Cox 	if (vm_kmem_size_min > 0 && vm_kmem_size < vm_kmem_size_min)
7280e5179e4SStephane E. Potvin 		vm_kmem_size = vm_kmem_size_min;
7290e5179e4SStephane E. Potvin 
7308a58a9f6SJohn Dyson #if defined(VM_KMEM_SIZE_MAX)
731479439b4SDag-Erling Smørgrav 	vm_kmem_size_max = VM_KMEM_SIZE_MAX;
7328a58a9f6SJohn Dyson #endif
733b89eaf4eSAlan Cox 	TUNABLE_ULONG_FETCH("vm.kmem_size_max", &vm_kmem_size_max);
734479439b4SDag-Erling Smørgrav 	if (vm_kmem_size_max > 0 && vm_kmem_size >= vm_kmem_size_max)
735479439b4SDag-Erling Smørgrav 		vm_kmem_size = vm_kmem_size_max;
7368a58a9f6SJohn Dyson 
73727b8623fSDavid Greenman 	/*
738c70af487SAlan Cox 	 * Alternatively, the amount of KVA space that is preallocated to the
739c70af487SAlan Cox 	 * kmem arena can be set statically at compile-time or manually
740c70af487SAlan Cox 	 * through the kernel environment.  However, it is still limited to
741c70af487SAlan Cox 	 * twice the physical memory size, which has been sufficient to handle
742c70af487SAlan Cox 	 * the most severe cases of external fragmentation in the kmem arena.
74327b8623fSDavid Greenman 	 */
744c70af487SAlan Cox #if defined(VM_KMEM_SIZE)
745c70af487SAlan Cox 	vm_kmem_size = VM_KMEM_SIZE;
746c70af487SAlan Cox #endif
747c70af487SAlan Cox 	TUNABLE_ULONG_FETCH("vm.kmem_size", &vm_kmem_size);
748c749c003SAlan Cox 	if (vm_kmem_size / 2 / PAGE_SIZE > mem_size)
749c749c003SAlan Cox 		vm_kmem_size = 2 * mem_size * PAGE_SIZE;
7508a58a9f6SJohn Dyson 
751e137643eSOlivier Houchard 	vm_kmem_size = round_page(vm_kmem_size);
752e3813573SMatthew D Fleming #ifdef DEBUG_MEMGUARD
753f806cdcfSMatthew D Fleming 	tmp = memguard_fudge(vm_kmem_size, kernel_map);
754e3813573SMatthew D Fleming #else
755e3813573SMatthew D Fleming 	tmp = vm_kmem_size;
756e3813573SMatthew D Fleming #endif
7575df87b21SJeff Roberson 	vmem_init(kmem_arena, "kmem arena", kva_alloc(tmp), tmp, PAGE_SIZE,
75899de9af2SJeff Roberson 	    0, 0);
7595df87b21SJeff Roberson 	vmem_set_reclaim(kmem_arena, kmem_reclaim);
7608355f576SJeff Roberson 
761e4eb384bSBosko Milekic #ifdef DEBUG_MEMGUARD
762e4eb384bSBosko Milekic 	/*
763e4eb384bSBosko Milekic 	 * Initialize MemGuard if support compiled in.  MemGuard is a
764e4eb384bSBosko Milekic 	 * replacement allocator used for detecting tamper-after-free
765e4eb384bSBosko Milekic 	 * scenarios as they occur.  It is only used for debugging.
766e4eb384bSBosko Milekic 	 */
7675df87b21SJeff Roberson 	memguard_init(kmem_arena);
768e4eb384bSBosko Milekic #endif
7695df87b21SJeff Roberson }
7705df87b21SJeff Roberson 
7715df87b21SJeff Roberson /*
7725df87b21SJeff Roberson  * Initialize the kernel memory allocator
7735df87b21SJeff Roberson  */
7745df87b21SJeff Roberson /* ARGSUSED*/
7755df87b21SJeff Roberson static void
7765df87b21SJeff Roberson mallocinit(void *dummy)
7775df87b21SJeff Roberson {
7785df87b21SJeff Roberson 	int i;
7795df87b21SJeff Roberson 	uint8_t indx;
7805df87b21SJeff Roberson 
7815df87b21SJeff Roberson 	mtx_init(&malloc_mtx, "malloc", NULL, MTX_DEF);
7825df87b21SJeff Roberson 
7835df87b21SJeff Roberson 	kmeminit();
784e4eb384bSBosko Milekic 
78599571dc3SJeff Roberson 	uma_startup2();
7868355f576SJeff Roberson 
78763a7e0a3SRobert Watson 	mt_zone = uma_zcreate("mt_zone", sizeof(struct malloc_type_internal),
78863a7e0a3SRobert Watson #ifdef INVARIANTS
78963a7e0a3SRobert Watson 	    mtrash_ctor, mtrash_dtor, mtrash_init, mtrash_fini,
79063a7e0a3SRobert Watson #else
79163a7e0a3SRobert Watson 	    NULL, NULL, NULL, NULL,
79263a7e0a3SRobert Watson #endif
79363a7e0a3SRobert Watson 	    UMA_ALIGN_PTR, UMA_ZONE_MALLOC);
7946f267175SJeff Roberson 	for (i = 0, indx = 0; kmemzones[indx].kz_size != 0; indx++) {
7956f267175SJeff Roberson 		int size = kmemzones[indx].kz_size;
7966f267175SJeff Roberson 		char *name = kmemzones[indx].kz_name;
797d7854da1SMatthew D Fleming 		int subzone;
7988355f576SJeff Roberson 
799d7854da1SMatthew D Fleming 		for (subzone = 0; subzone < numzones; subzone++) {
800d7854da1SMatthew D Fleming 			kmemzones[indx].kz_zone[subzone] =
801d7854da1SMatthew D Fleming 			    uma_zcreate(name, size,
8028efc4effSJeff Roberson #ifdef INVARIANTS
8038f70816cSJeff Roberson 			    mtrash_ctor, mtrash_dtor, mtrash_init, mtrash_fini,
8048efc4effSJeff Roberson #else
8058efc4effSJeff Roberson 			    NULL, NULL, NULL, NULL,
8068efc4effSJeff Roberson #endif
8078efc4effSJeff Roberson 			    UMA_ALIGN_PTR, UMA_ZONE_MALLOC);
808d7854da1SMatthew D Fleming 		}
8098355f576SJeff Roberson 		for (;i <= size; i+= KMEM_ZBASE)
8106f267175SJeff Roberson 			kmemsize[i >> KMEM_ZSHIFT] = indx;
8118355f576SJeff Roberson 
812df8bae1dSRodney W. Grimes 	}
813254c6cb3SPoul-Henning Kamp }
8145df87b21SJeff Roberson SYSINIT(kmem, SI_SUB_KMEM, SI_ORDER_FIRST, mallocinit, NULL);
815254c6cb3SPoul-Henning Kamp 
816db669378SPeter Wemm void
81787efd4d5SRobert Watson malloc_init(void *data)
818254c6cb3SPoul-Henning Kamp {
81963a7e0a3SRobert Watson 	struct malloc_type_internal *mtip;
82063a7e0a3SRobert Watson 	struct malloc_type *mtp;
82163a7e0a3SRobert Watson 
822*44f1c916SBryan Drewery 	KASSERT(vm_cnt.v_page_count != 0, ("malloc_register before vm_init"));
82363a7e0a3SRobert Watson 
82463a7e0a3SRobert Watson 	mtp = data;
825f121baaaSBrian Somers 	if (mtp->ks_magic != M_MAGIC)
826f121baaaSBrian Somers 		panic("malloc_init: bad malloc type magic");
827bb1c7df8SRobert Watson 
82863a7e0a3SRobert Watson 	mtip = uma_zalloc(mt_zone, M_WAITOK | M_ZERO);
82963a7e0a3SRobert Watson 	mtp->ks_handle = mtip;
830d7854da1SMatthew D Fleming 	mtip->mti_zone = mtp_get_subzone(mtp->ks_shortdesc);
831254c6cb3SPoul-Henning Kamp 
8326f267175SJeff Roberson 	mtx_lock(&malloc_mtx);
83363a7e0a3SRobert Watson 	mtp->ks_next = kmemstatistics;
83463a7e0a3SRobert Watson 	kmemstatistics = mtp;
835cd814b26SRobert Watson 	kmemcount++;
8366f267175SJeff Roberson 	mtx_unlock(&malloc_mtx);
837df8bae1dSRodney W. Grimes }
838db669378SPeter Wemm 
839db669378SPeter Wemm void
84087efd4d5SRobert Watson malloc_uninit(void *data)
841db669378SPeter Wemm {
84263a7e0a3SRobert Watson 	struct malloc_type_internal *mtip;
8432a143d5bSPawel Jakub Dawidek 	struct malloc_type_stats *mtsp;
84463a7e0a3SRobert Watson 	struct malloc_type *mtp, *temp;
84545d48bdaSPaul Saab 	uma_slab_t slab;
8462a143d5bSPawel Jakub Dawidek 	long temp_allocs, temp_bytes;
8472a143d5bSPawel Jakub Dawidek 	int i;
848db669378SPeter Wemm 
84963a7e0a3SRobert Watson 	mtp = data;
850bb1c7df8SRobert Watson 	KASSERT(mtp->ks_magic == M_MAGIC,
851bb1c7df8SRobert Watson 	    ("malloc_uninit: bad malloc type magic"));
85263a7e0a3SRobert Watson 	KASSERT(mtp->ks_handle != NULL, ("malloc_deregister: cookie NULL"));
853bb1c7df8SRobert Watson 
8546f267175SJeff Roberson 	mtx_lock(&malloc_mtx);
85563a7e0a3SRobert Watson 	mtip = mtp->ks_handle;
85663a7e0a3SRobert Watson 	mtp->ks_handle = NULL;
85763a7e0a3SRobert Watson 	if (mtp != kmemstatistics) {
85863a7e0a3SRobert Watson 		for (temp = kmemstatistics; temp != NULL;
85963a7e0a3SRobert Watson 		    temp = temp->ks_next) {
860f121baaaSBrian Somers 			if (temp->ks_next == mtp) {
86163a7e0a3SRobert Watson 				temp->ks_next = mtp->ks_next;
862f121baaaSBrian Somers 				break;
863db669378SPeter Wemm 			}
864f121baaaSBrian Somers 		}
865f121baaaSBrian Somers 		KASSERT(temp,
866f121baaaSBrian Somers 		    ("malloc_uninit: type '%s' not found", mtp->ks_shortdesc));
86763a7e0a3SRobert Watson 	} else
86863a7e0a3SRobert Watson 		kmemstatistics = mtp->ks_next;
869cd814b26SRobert Watson 	kmemcount--;
8706f267175SJeff Roberson 	mtx_unlock(&malloc_mtx);
8712a143d5bSPawel Jakub Dawidek 
8722a143d5bSPawel Jakub Dawidek 	/*
8732a143d5bSPawel Jakub Dawidek 	 * Look for memory leaks.
8742a143d5bSPawel Jakub Dawidek 	 */
8752a143d5bSPawel Jakub Dawidek 	temp_allocs = temp_bytes = 0;
8762a143d5bSPawel Jakub Dawidek 	for (i = 0; i < MAXCPU; i++) {
8772a143d5bSPawel Jakub Dawidek 		mtsp = &mtip->mti_stats[i];
8782a143d5bSPawel Jakub Dawidek 		temp_allocs += mtsp->mts_numallocs;
8792a143d5bSPawel Jakub Dawidek 		temp_allocs -= mtsp->mts_numfrees;
8802a143d5bSPawel Jakub Dawidek 		temp_bytes += mtsp->mts_memalloced;
8812a143d5bSPawel Jakub Dawidek 		temp_bytes -= mtsp->mts_memfreed;
8822a143d5bSPawel Jakub Dawidek 	}
8832a143d5bSPawel Jakub Dawidek 	if (temp_allocs > 0 || temp_bytes > 0) {
8842a143d5bSPawel Jakub Dawidek 		printf("Warning: memory type %s leaked memory on destroy "
8852a143d5bSPawel Jakub Dawidek 		    "(%ld allocations, %ld bytes leaked).\n", mtp->ks_shortdesc,
8862a143d5bSPawel Jakub Dawidek 		    temp_allocs, temp_bytes);
8872a143d5bSPawel Jakub Dawidek 	}
8882a143d5bSPawel Jakub Dawidek 
88945d48bdaSPaul Saab 	slab = vtoslab((vm_offset_t) mtip & (~UMA_SLAB_MASK));
89045d48bdaSPaul Saab 	uma_zfree_arg(mt_zone, mtip, slab);
891db669378SPeter Wemm }
8926f267175SJeff Roberson 
893d362c40dSPawel Jakub Dawidek struct malloc_type *
894d362c40dSPawel Jakub Dawidek malloc_desc2type(const char *desc)
895d362c40dSPawel Jakub Dawidek {
896d362c40dSPawel Jakub Dawidek 	struct malloc_type *mtp;
897d362c40dSPawel Jakub Dawidek 
898d362c40dSPawel Jakub Dawidek 	mtx_assert(&malloc_mtx, MA_OWNED);
899d362c40dSPawel Jakub Dawidek 	for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) {
900d362c40dSPawel Jakub Dawidek 		if (strcmp(mtp->ks_shortdesc, desc) == 0)
901d362c40dSPawel Jakub Dawidek 			return (mtp);
902d362c40dSPawel Jakub Dawidek 	}
903d362c40dSPawel Jakub Dawidek 	return (NULL);
904d362c40dSPawel Jakub Dawidek }
905d362c40dSPawel Jakub Dawidek 
9066f267175SJeff Roberson static int
907cd814b26SRobert Watson sysctl_kern_malloc_stats(SYSCTL_HANDLER_ARGS)
908cd814b26SRobert Watson {
909cd814b26SRobert Watson 	struct malloc_type_stream_header mtsh;
910cd814b26SRobert Watson 	struct malloc_type_internal *mtip;
911cd814b26SRobert Watson 	struct malloc_type_header mth;
912cd814b26SRobert Watson 	struct malloc_type *mtp;
9134e657159SMatthew D Fleming 	int error, i;
914cd814b26SRobert Watson 	struct sbuf sbuf;
915cd814b26SRobert Watson 
91600f0e671SMatthew D Fleming 	error = sysctl_wire_old_buffer(req, 0);
91700f0e671SMatthew D Fleming 	if (error != 0)
91800f0e671SMatthew D Fleming 		return (error);
9194e657159SMatthew D Fleming 	sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
920cd814b26SRobert Watson 	mtx_lock(&malloc_mtx);
921cd814b26SRobert Watson 
922cd814b26SRobert Watson 	/*
923cd814b26SRobert Watson 	 * Insert stream header.
924cd814b26SRobert Watson 	 */
925cd814b26SRobert Watson 	bzero(&mtsh, sizeof(mtsh));
926cd814b26SRobert Watson 	mtsh.mtsh_version = MALLOC_TYPE_STREAM_VERSION;
927cd814b26SRobert Watson 	mtsh.mtsh_maxcpus = MAXCPU;
928cd814b26SRobert Watson 	mtsh.mtsh_count = kmemcount;
9294e657159SMatthew D Fleming 	(void)sbuf_bcat(&sbuf, &mtsh, sizeof(mtsh));
930cd814b26SRobert Watson 
931cd814b26SRobert Watson 	/*
932cd814b26SRobert Watson 	 * Insert alternating sequence of type headers and type statistics.
933cd814b26SRobert Watson 	 */
934cd814b26SRobert Watson 	for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) {
935cd814b26SRobert Watson 		mtip = (struct malloc_type_internal *)mtp->ks_handle;
936cd814b26SRobert Watson 
937cd814b26SRobert Watson 		/*
938cd814b26SRobert Watson 		 * Insert type header.
939cd814b26SRobert Watson 		 */
940cd814b26SRobert Watson 		bzero(&mth, sizeof(mth));
941cd814b26SRobert Watson 		strlcpy(mth.mth_name, mtp->ks_shortdesc, MALLOC_MAX_NAME);
9424e657159SMatthew D Fleming 		(void)sbuf_bcat(&sbuf, &mth, sizeof(mth));
943cd814b26SRobert Watson 
944cd814b26SRobert Watson 		/*
945cd814b26SRobert Watson 		 * Insert type statistics for each CPU.
946cd814b26SRobert Watson 		 */
947cd814b26SRobert Watson 		for (i = 0; i < MAXCPU; i++) {
9484e657159SMatthew D Fleming 			(void)sbuf_bcat(&sbuf, &mtip->mti_stats[i],
9494e657159SMatthew D Fleming 			    sizeof(mtip->mti_stats[i]));
950cd814b26SRobert Watson 		}
951cd814b26SRobert Watson 	}
952cd814b26SRobert Watson 	mtx_unlock(&malloc_mtx);
9534e657159SMatthew D Fleming 	error = sbuf_finish(&sbuf);
954cd814b26SRobert Watson 	sbuf_delete(&sbuf);
955cd814b26SRobert Watson 	return (error);
956cd814b26SRobert Watson }
957cd814b26SRobert Watson 
958cd814b26SRobert Watson SYSCTL_PROC(_kern, OID_AUTO, malloc_stats, CTLFLAG_RD|CTLTYPE_STRUCT,
959cd814b26SRobert Watson     0, 0, sysctl_kern_malloc_stats, "s,malloc_type_ustats",
960cd814b26SRobert Watson     "Return malloc types");
961cd814b26SRobert Watson 
962cd814b26SRobert Watson SYSCTL_INT(_kern, OID_AUTO, malloc_count, CTLFLAG_RD, &kmemcount, 0,
963cd814b26SRobert Watson     "Count of kernel malloc types");
964cd814b26SRobert Watson 
96591dd776cSJohn Birrell void
96691dd776cSJohn Birrell malloc_type_list(malloc_type_list_func_t *func, void *arg)
96791dd776cSJohn Birrell {
96891dd776cSJohn Birrell 	struct malloc_type *mtp, **bufmtp;
96991dd776cSJohn Birrell 	int count, i;
97091dd776cSJohn Birrell 	size_t buflen;
97191dd776cSJohn Birrell 
97291dd776cSJohn Birrell 	mtx_lock(&malloc_mtx);
97391dd776cSJohn Birrell restart:
97491dd776cSJohn Birrell 	mtx_assert(&malloc_mtx, MA_OWNED);
97591dd776cSJohn Birrell 	count = kmemcount;
97691dd776cSJohn Birrell 	mtx_unlock(&malloc_mtx);
97791dd776cSJohn Birrell 
97891dd776cSJohn Birrell 	buflen = sizeof(struct malloc_type *) * count;
97991dd776cSJohn Birrell 	bufmtp = malloc(buflen, M_TEMP, M_WAITOK);
98091dd776cSJohn Birrell 
98191dd776cSJohn Birrell 	mtx_lock(&malloc_mtx);
98291dd776cSJohn Birrell 
98391dd776cSJohn Birrell 	if (count < kmemcount) {
98491dd776cSJohn Birrell 		free(bufmtp, M_TEMP);
98591dd776cSJohn Birrell 		goto restart;
98691dd776cSJohn Birrell 	}
98791dd776cSJohn Birrell 
98891dd776cSJohn Birrell 	for (mtp = kmemstatistics, i = 0; mtp != NULL; mtp = mtp->ks_next, i++)
98991dd776cSJohn Birrell 		bufmtp[i] = mtp;
99091dd776cSJohn Birrell 
99191dd776cSJohn Birrell 	mtx_unlock(&malloc_mtx);
99291dd776cSJohn Birrell 
99391dd776cSJohn Birrell 	for (i = 0; i < count; i++)
99491dd776cSJohn Birrell 		(func)(bufmtp[i], arg);
99591dd776cSJohn Birrell 
99691dd776cSJohn Birrell 	free(bufmtp, M_TEMP);
99791dd776cSJohn Birrell }
99891dd776cSJohn Birrell 
999909ed16cSRobert Watson #ifdef DDB
1000909ed16cSRobert Watson DB_SHOW_COMMAND(malloc, db_show_malloc)
1001909ed16cSRobert Watson {
1002909ed16cSRobert Watson 	struct malloc_type_internal *mtip;
1003909ed16cSRobert Watson 	struct malloc_type *mtp;
100460ae52f7SEd Schouten 	uint64_t allocs, frees;
100560ae52f7SEd Schouten 	uint64_t alloced, freed;
1006909ed16cSRobert Watson 	int i;
1007909ed16cSRobert Watson 
100824076d13SRobert Watson 	db_printf("%18s %12s  %12s %12s\n", "Type", "InUse", "MemUse",
100924076d13SRobert Watson 	    "Requests");
1010909ed16cSRobert Watson 	for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) {
1011909ed16cSRobert Watson 		mtip = (struct malloc_type_internal *)mtp->ks_handle;
1012909ed16cSRobert Watson 		allocs = 0;
1013909ed16cSRobert Watson 		frees = 0;
101424076d13SRobert Watson 		alloced = 0;
101524076d13SRobert Watson 		freed = 0;
1016909ed16cSRobert Watson 		for (i = 0; i < MAXCPU; i++) {
1017909ed16cSRobert Watson 			allocs += mtip->mti_stats[i].mts_numallocs;
1018909ed16cSRobert Watson 			frees += mtip->mti_stats[i].mts_numfrees;
101924076d13SRobert Watson 			alloced += mtip->mti_stats[i].mts_memalloced;
102024076d13SRobert Watson 			freed += mtip->mti_stats[i].mts_memfreed;
1021909ed16cSRobert Watson 		}
102224076d13SRobert Watson 		db_printf("%18s %12ju %12juK %12ju\n",
102324076d13SRobert Watson 		    mtp->ks_shortdesc, allocs - frees,
102424076d13SRobert Watson 		    (alloced - freed + 1023) / 1024, allocs);
1025687c94aaSJohn Baldwin 		if (db_pager_quit)
1026687c94aaSJohn Baldwin 			break;
1027909ed16cSRobert Watson 	}
1028909ed16cSRobert Watson }
1029d7854da1SMatthew D Fleming 
1030d7854da1SMatthew D Fleming #if MALLOC_DEBUG_MAXZONES > 1
1031d7854da1SMatthew D Fleming DB_SHOW_COMMAND(multizone_matches, db_show_multizone_matches)
1032d7854da1SMatthew D Fleming {
1033d7854da1SMatthew D Fleming 	struct malloc_type_internal *mtip;
1034d7854da1SMatthew D Fleming 	struct malloc_type *mtp;
1035d7854da1SMatthew D Fleming 	u_int subzone;
1036d7854da1SMatthew D Fleming 
1037d7854da1SMatthew D Fleming 	if (!have_addr) {
1038d7854da1SMatthew D Fleming 		db_printf("Usage: show multizone_matches <malloc type/addr>\n");
1039d7854da1SMatthew D Fleming 		return;
1040d7854da1SMatthew D Fleming 	}
1041d7854da1SMatthew D Fleming 	mtp = (void *)addr;
1042d7854da1SMatthew D Fleming 	if (mtp->ks_magic != M_MAGIC) {
1043d7854da1SMatthew D Fleming 		db_printf("Magic %lx does not match expected %x\n",
1044d7854da1SMatthew D Fleming 		    mtp->ks_magic, M_MAGIC);
1045d7854da1SMatthew D Fleming 		return;
1046d7854da1SMatthew D Fleming 	}
1047d7854da1SMatthew D Fleming 
1048d7854da1SMatthew D Fleming 	mtip = mtp->ks_handle;
1049d7854da1SMatthew D Fleming 	subzone = mtip->mti_zone;
1050d7854da1SMatthew D Fleming 
1051d7854da1SMatthew D Fleming 	for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) {
1052d7854da1SMatthew D Fleming 		mtip = mtp->ks_handle;
1053d7854da1SMatthew D Fleming 		if (mtip->mti_zone != subzone)
1054d7854da1SMatthew D Fleming 			continue;
1055d7854da1SMatthew D Fleming 		db_printf("%s\n", mtp->ks_shortdesc);
1056687c94aaSJohn Baldwin 		if (db_pager_quit)
1057687c94aaSJohn Baldwin 			break;
1058d7854da1SMatthew D Fleming 	}
1059d7854da1SMatthew D Fleming }
1060d7854da1SMatthew D Fleming #endif /* MALLOC_DEBUG_MAXZONES > 1 */
1061d7854da1SMatthew D Fleming #endif /* DDB */
1062909ed16cSRobert Watson 
10635e914b96SJeff Roberson #ifdef MALLOC_PROFILE
10645e914b96SJeff Roberson 
10655e914b96SJeff Roberson static int
10665e914b96SJeff Roberson sysctl_kern_mprof(SYSCTL_HANDLER_ARGS)
10675e914b96SJeff Roberson {
106863a7e0a3SRobert Watson 	struct sbuf sbuf;
10695e914b96SJeff Roberson 	uint64_t count;
10705e914b96SJeff Roberson 	uint64_t waste;
10715e914b96SJeff Roberson 	uint64_t mem;
10725e914b96SJeff Roberson 	int error;
10735e914b96SJeff Roberson 	int rsize;
10745e914b96SJeff Roberson 	int size;
10755e914b96SJeff Roberson 	int i;
10765e914b96SJeff Roberson 
10775e914b96SJeff Roberson 	waste = 0;
10785e914b96SJeff Roberson 	mem = 0;
10795e914b96SJeff Roberson 
108000f0e671SMatthew D Fleming 	error = sysctl_wire_old_buffer(req, 0);
108100f0e671SMatthew D Fleming 	if (error != 0)
108200f0e671SMatthew D Fleming 		return (error);
10834e657159SMatthew D Fleming 	sbuf_new_for_sysctl(&sbuf, NULL, 128, req);
108463a7e0a3SRobert Watson 	sbuf_printf(&sbuf,
10855e914b96SJeff Roberson 	    "\n  Size                    Requests  Real Size\n");
10865e914b96SJeff Roberson 	for (i = 0; i < KMEM_ZSIZE; i++) {
10875e914b96SJeff Roberson 		size = i << KMEM_ZSHIFT;
10885e914b96SJeff Roberson 		rsize = kmemzones[kmemsize[i]].kz_size;
10895e914b96SJeff Roberson 		count = (long long unsigned)krequests[i];
10905e914b96SJeff Roberson 
109163a7e0a3SRobert Watson 		sbuf_printf(&sbuf, "%6d%28llu%11d\n", size,
109263a7e0a3SRobert Watson 		    (unsigned long long)count, rsize);
10935e914b96SJeff Roberson 
10945e914b96SJeff Roberson 		if ((rsize * count) > (size * count))
10955e914b96SJeff Roberson 			waste += (rsize * count) - (size * count);
10965e914b96SJeff Roberson 		mem += (rsize * count);
10975e914b96SJeff Roberson 	}
109863a7e0a3SRobert Watson 	sbuf_printf(&sbuf,
10995e914b96SJeff Roberson 	    "\nTotal memory used:\t%30llu\nTotal Memory wasted:\t%30llu\n",
11005e914b96SJeff Roberson 	    (unsigned long long)mem, (unsigned long long)waste);
11014e657159SMatthew D Fleming 	error = sbuf_finish(&sbuf);
110263a7e0a3SRobert Watson 	sbuf_delete(&sbuf);
11035e914b96SJeff Roberson 	return (error);
11045e914b96SJeff Roberson }
11055e914b96SJeff Roberson 
11065e914b96SJeff Roberson SYSCTL_OID(_kern, OID_AUTO, mprof, CTLTYPE_STRING|CTLFLAG_RD,
11075e914b96SJeff Roberson     NULL, 0, sysctl_kern_mprof, "A", "Malloc Profiling");
11085e914b96SJeff Roberson #endif /* MALLOC_PROFILE */
1109