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 194*4813ad54SHans Petter Selasky 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; 239af3b2549SHans Petter Selasky SYSCTL_INT(_debug_malloc, OID_AUTO, failure_rate, CTLFLAG_RWTUN, 240eae870cdSRobert Watson &malloc_failure_rate, 0, "Every (n) mallocs with M_NOWAIT will fail"); 241eae870cdSRobert Watson SYSCTL_INT(_debug_malloc, OID_AUTO, failure_count, CTLFLAG_RD, 242eae870cdSRobert Watson &malloc_failure_count, 0, "Number of imposed M_NOWAIT malloc failures"); 243eae870cdSRobert Watson #endif 244eae870cdSRobert Watson 2457814c80aSAndriy Gapon static int 2467814c80aSAndriy Gapon sysctl_kmem_map_size(SYSCTL_HANDLER_ARGS) 2477814c80aSAndriy Gapon { 2487814c80aSAndriy Gapon u_long size; 2497814c80aSAndriy Gapon 2505df87b21SJeff Roberson size = vmem_size(kmem_arena, VMEM_ALLOC); 2517814c80aSAndriy Gapon return (sysctl_handle_long(oidp, &size, 0, req)); 2527814c80aSAndriy Gapon } 2537814c80aSAndriy Gapon 25495bb9d38SAndriy Gapon static int 25595bb9d38SAndriy Gapon sysctl_kmem_map_free(SYSCTL_HANDLER_ARGS) 25695bb9d38SAndriy Gapon { 25795bb9d38SAndriy Gapon u_long size; 25895bb9d38SAndriy Gapon 2595df87b21SJeff Roberson size = vmem_size(kmem_arena, VMEM_FREE); 26095bb9d38SAndriy Gapon return (sysctl_handle_long(oidp, &size, 0, req)); 26195bb9d38SAndriy Gapon } 26295bb9d38SAndriy Gapon 263d7854da1SMatthew D Fleming /* 264d7854da1SMatthew D Fleming * malloc(9) uma zone separation -- sub-page buffer overruns in one 265d7854da1SMatthew D Fleming * malloc type will affect only a subset of other malloc types. 266d7854da1SMatthew D Fleming */ 267d7854da1SMatthew D Fleming #if MALLOC_DEBUG_MAXZONES > 1 268d7854da1SMatthew D Fleming static void 269d7854da1SMatthew D Fleming tunable_set_numzones(void) 270d7854da1SMatthew D Fleming { 271d7854da1SMatthew D Fleming 272d7854da1SMatthew D Fleming TUNABLE_INT_FETCH("debug.malloc.numzones", 273d7854da1SMatthew D Fleming &numzones); 274d7854da1SMatthew D Fleming 275d7854da1SMatthew D Fleming /* Sanity check the number of malloc uma zones. */ 276d7854da1SMatthew D Fleming if (numzones <= 0) 277d7854da1SMatthew D Fleming numzones = 1; 278d7854da1SMatthew D Fleming if (numzones > MALLOC_DEBUG_MAXZONES) 279d7854da1SMatthew D Fleming numzones = MALLOC_DEBUG_MAXZONES; 280d7854da1SMatthew D Fleming } 281d7854da1SMatthew D Fleming SYSINIT(numzones, SI_SUB_TUNABLES, SI_ORDER_ANY, tunable_set_numzones, NULL); 282af3b2549SHans Petter Selasky SYSCTL_INT(_debug_malloc, OID_AUTO, numzones, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, 283d7854da1SMatthew D Fleming &numzones, 0, "Number of malloc uma subzones"); 284d7854da1SMatthew D Fleming 285d7854da1SMatthew D Fleming /* 286d7854da1SMatthew D Fleming * Any number that changes regularly is an okay choice for the 287d7854da1SMatthew D Fleming * offset. Build numbers are pretty good of you have them. 288d7854da1SMatthew D Fleming */ 289d7854da1SMatthew D Fleming static u_int zone_offset = __FreeBSD_version; 290d7854da1SMatthew D Fleming TUNABLE_INT("debug.malloc.zone_offset", &zone_offset); 291d7854da1SMatthew D Fleming SYSCTL_UINT(_debug_malloc, OID_AUTO, zone_offset, CTLFLAG_RDTUN, 292d7854da1SMatthew D Fleming &zone_offset, 0, "Separate malloc types by examining the " 293d7854da1SMatthew D Fleming "Nth character in the malloc type short description."); 294d7854da1SMatthew D Fleming 295d7854da1SMatthew D Fleming static u_int 296d7854da1SMatthew D Fleming mtp_get_subzone(const char *desc) 297d7854da1SMatthew D Fleming { 298d7854da1SMatthew D Fleming size_t len; 299d7854da1SMatthew D Fleming u_int val; 300d7854da1SMatthew D Fleming 301d7854da1SMatthew D Fleming if (desc == NULL || (len = strlen(desc)) == 0) 302d7854da1SMatthew D Fleming return (0); 303d7854da1SMatthew D Fleming val = desc[zone_offset % len]; 304d7854da1SMatthew D Fleming return (val % numzones); 305d7854da1SMatthew D Fleming } 306d7854da1SMatthew D Fleming #elif MALLOC_DEBUG_MAXZONES == 0 307d7854da1SMatthew D Fleming #error "MALLOC_DEBUG_MAXZONES must be positive." 308d7854da1SMatthew D Fleming #else 309d7854da1SMatthew D Fleming static inline u_int 310d7854da1SMatthew D Fleming mtp_get_subzone(const char *desc) 311d7854da1SMatthew D Fleming { 312d7854da1SMatthew D Fleming 313d7854da1SMatthew D Fleming return (0); 314d7854da1SMatthew D Fleming } 315d7854da1SMatthew D Fleming #endif /* MALLOC_DEBUG_MAXZONES > 1 */ 316d7854da1SMatthew D Fleming 3171fb14a47SPoul-Henning Kamp int 3181fb14a47SPoul-Henning Kamp malloc_last_fail(void) 3191fb14a47SPoul-Henning Kamp { 3201fb14a47SPoul-Henning Kamp 3211fb14a47SPoul-Henning Kamp return (time_uptime - t_malloc_fail); 3221fb14a47SPoul-Henning Kamp } 3231fb14a47SPoul-Henning Kamp 324df8bae1dSRodney W. Grimes /* 3250ce3f16dSRobert Watson * An allocation has succeeded -- update malloc type statistics for the 3260ce3f16dSRobert Watson * amount of bucket size. Occurs within a critical section so that the 3270ce3f16dSRobert Watson * thread isn't preempted and doesn't migrate while updating per-PCU 3280ce3f16dSRobert Watson * statistics. 3294362fadaSBrian Feldman */ 3304362fadaSBrian Feldman static void 33163a7e0a3SRobert Watson malloc_type_zone_allocated(struct malloc_type *mtp, unsigned long size, 3324362fadaSBrian Feldman int zindx) 3334362fadaSBrian Feldman { 33463a7e0a3SRobert Watson struct malloc_type_internal *mtip; 33563a7e0a3SRobert Watson struct malloc_type_stats *mtsp; 33663a7e0a3SRobert Watson 33763a7e0a3SRobert Watson critical_enter(); 33863a7e0a3SRobert Watson mtip = mtp->ks_handle; 33963a7e0a3SRobert Watson mtsp = &mtip->mti_stats[curcpu]; 34073864adbSPawel Jakub Dawidek if (size > 0) { 34163a7e0a3SRobert Watson mtsp->mts_memalloced += size; 34263a7e0a3SRobert Watson mtsp->mts_numallocs++; 34373864adbSPawel Jakub Dawidek } 3444362fadaSBrian Feldman if (zindx != -1) 34563a7e0a3SRobert Watson mtsp->mts_size |= 1 << zindx; 34691dd776cSJohn Birrell 34791dd776cSJohn Birrell #ifdef KDTRACE_HOOKS 34891dd776cSJohn Birrell if (dtrace_malloc_probe != NULL) { 34991dd776cSJohn Birrell uint32_t probe_id = mtip->mti_probes[DTMALLOC_PROBE_MALLOC]; 35091dd776cSJohn Birrell if (probe_id != 0) 35191dd776cSJohn Birrell (dtrace_malloc_probe)(probe_id, 35291dd776cSJohn Birrell (uintptr_t) mtp, (uintptr_t) mtip, 35391dd776cSJohn Birrell (uintptr_t) mtsp, size, zindx); 35491dd776cSJohn Birrell } 35591dd776cSJohn Birrell #endif 35691dd776cSJohn Birrell 35763a7e0a3SRobert Watson critical_exit(); 3584362fadaSBrian Feldman } 3594362fadaSBrian Feldman 3604362fadaSBrian Feldman void 36163a7e0a3SRobert Watson malloc_type_allocated(struct malloc_type *mtp, unsigned long size) 3624362fadaSBrian Feldman { 36363a7e0a3SRobert Watson 36473864adbSPawel Jakub Dawidek if (size > 0) 36563a7e0a3SRobert Watson malloc_type_zone_allocated(mtp, size, -1); 3664362fadaSBrian Feldman } 3674362fadaSBrian Feldman 3684362fadaSBrian Feldman /* 3693805385eSRobert Watson * A free operation has occurred -- update malloc type statistics for the 3700ce3f16dSRobert Watson * amount of the bucket size. Occurs within a critical section so that the 3710ce3f16dSRobert Watson * thread isn't preempted and doesn't migrate while updating per-CPU 3720ce3f16dSRobert Watson * statistics. 3734362fadaSBrian Feldman */ 3744362fadaSBrian Feldman void 37563a7e0a3SRobert Watson malloc_type_freed(struct malloc_type *mtp, unsigned long size) 3764362fadaSBrian Feldman { 37763a7e0a3SRobert Watson struct malloc_type_internal *mtip; 37863a7e0a3SRobert Watson struct malloc_type_stats *mtsp; 37963a7e0a3SRobert Watson 38063a7e0a3SRobert Watson critical_enter(); 38163a7e0a3SRobert Watson mtip = mtp->ks_handle; 38263a7e0a3SRobert Watson mtsp = &mtip->mti_stats[curcpu]; 38363a7e0a3SRobert Watson mtsp->mts_memfreed += size; 38463a7e0a3SRobert Watson mtsp->mts_numfrees++; 38591dd776cSJohn Birrell 38691dd776cSJohn Birrell #ifdef KDTRACE_HOOKS 38791dd776cSJohn Birrell if (dtrace_malloc_probe != NULL) { 38891dd776cSJohn Birrell uint32_t probe_id = mtip->mti_probes[DTMALLOC_PROBE_FREE]; 38991dd776cSJohn Birrell if (probe_id != 0) 39091dd776cSJohn Birrell (dtrace_malloc_probe)(probe_id, 39191dd776cSJohn Birrell (uintptr_t) mtp, (uintptr_t) mtip, 39291dd776cSJohn Birrell (uintptr_t) mtsp, size, 0); 39391dd776cSJohn Birrell } 39491dd776cSJohn Birrell #endif 39591dd776cSJohn Birrell 39663a7e0a3SRobert Watson critical_exit(); 3974362fadaSBrian Feldman } 3984362fadaSBrian Feldman 3994362fadaSBrian Feldman /* 400f346986bSAlan Cox * contigmalloc: 401f346986bSAlan Cox * 402f346986bSAlan Cox * Allocate a block of physically contiguous memory. 403f346986bSAlan Cox * 404f346986bSAlan Cox * If M_NOWAIT is set, this routine will not block and return NULL if 405f346986bSAlan Cox * the allocation fails. 406f346986bSAlan Cox */ 407f346986bSAlan Cox void * 408f346986bSAlan Cox contigmalloc(unsigned long size, struct malloc_type *type, int flags, 409f346986bSAlan Cox vm_paddr_t low, vm_paddr_t high, unsigned long alignment, 410831ce4cbSJohn Baldwin vm_paddr_t boundary) 411f346986bSAlan Cox { 412f346986bSAlan Cox void *ret; 413f346986bSAlan Cox 4145df87b21SJeff Roberson ret = (void *)kmem_alloc_contig(kernel_arena, size, flags, low, high, 415f346986bSAlan Cox alignment, boundary, VM_MEMATTR_DEFAULT); 416f346986bSAlan Cox if (ret != NULL) 417f346986bSAlan Cox malloc_type_allocated(type, round_page(size)); 418f346986bSAlan Cox return (ret); 419f346986bSAlan Cox } 420f346986bSAlan Cox 421f346986bSAlan Cox /* 422f346986bSAlan Cox * contigfree: 423f346986bSAlan Cox * 424f346986bSAlan Cox * Free a block of memory allocated by contigmalloc. 425f346986bSAlan Cox * 426f346986bSAlan Cox * This routine may not block. 427f346986bSAlan Cox */ 428f346986bSAlan Cox void 429f346986bSAlan Cox contigfree(void *addr, unsigned long size, struct malloc_type *type) 430f346986bSAlan Cox { 431f346986bSAlan Cox 4325df87b21SJeff Roberson kmem_free(kernel_arena, (vm_offset_t)addr, size); 433f346986bSAlan Cox malloc_type_freed(type, round_page(size)); 434f346986bSAlan Cox } 435f346986bSAlan Cox 436f346986bSAlan Cox /* 4371c7c3c6aSMatthew Dillon * malloc: 4381c7c3c6aSMatthew Dillon * 4391c7c3c6aSMatthew Dillon * Allocate a block of memory. 4401c7c3c6aSMatthew Dillon * 4411c7c3c6aSMatthew Dillon * If M_NOWAIT is set, this routine will not block and return NULL if 4421c7c3c6aSMatthew Dillon * the allocation fails. 443df8bae1dSRodney W. Grimes */ 444df8bae1dSRodney W. Grimes void * 44563a7e0a3SRobert Watson malloc(unsigned long size, struct malloc_type *mtp, int flags) 446df8bae1dSRodney W. Grimes { 4476f267175SJeff Roberson int indx; 448d7854da1SMatthew D Fleming struct malloc_type_internal *mtip; 4498355f576SJeff Roberson caddr_t va; 4508355f576SJeff Roberson uma_zone_t zone; 451847a2a17SPawel Jakub Dawidek #if defined(DIAGNOSTIC) || defined(DEBUG_REDZONE) 4524db4f5c8SPoul-Henning Kamp unsigned long osize = size; 4534db4f5c8SPoul-Henning Kamp #endif 454df8bae1dSRodney W. Grimes 455194a0abfSPoul-Henning Kamp #ifdef INVARIANTS 456bb1c7df8SRobert Watson KASSERT(mtp->ks_magic == M_MAGIC, ("malloc: bad malloc type magic")); 457d3c11994SPoul-Henning Kamp /* 45823198357SRuslan Ermilov * Check that exactly one of M_WAITOK or M_NOWAIT is specified. 459d3c11994SPoul-Henning Kamp */ 46023198357SRuslan Ermilov indx = flags & (M_WAITOK | M_NOWAIT); 461d3c11994SPoul-Henning Kamp if (indx != M_NOWAIT && indx != M_WAITOK) { 462d3c11994SPoul-Henning Kamp static struct timeval lasterr; 463d3c11994SPoul-Henning Kamp static int curerr, once; 464d3c11994SPoul-Henning Kamp if (once == 0 && ppsratecheck(&lasterr, &curerr, 1)) { 465d3c11994SPoul-Henning Kamp printf("Bad malloc flags: %x\n", indx); 4662d50560aSMarcel Moolenaar kdb_backtrace(); 467d3c11994SPoul-Henning Kamp flags |= M_WAITOK; 468d3c11994SPoul-Henning Kamp once++; 469d3c11994SPoul-Henning Kamp } 470d3c11994SPoul-Henning Kamp } 471194a0abfSPoul-Henning Kamp #endif 472eae870cdSRobert Watson #ifdef MALLOC_MAKE_FAILURES 473eae870cdSRobert Watson if ((flags & M_NOWAIT) && (malloc_failure_rate != 0)) { 474eae870cdSRobert Watson atomic_add_int(&malloc_nowait_count, 1); 475eae870cdSRobert Watson if ((malloc_nowait_count % malloc_failure_rate) == 0) { 476eae870cdSRobert Watson atomic_add_int(&malloc_failure_count, 1); 4773f6ee876SPoul-Henning Kamp t_malloc_fail = time_uptime; 478eae870cdSRobert Watson return (NULL); 479eae870cdSRobert Watson } 480eae870cdSRobert Watson } 481eae870cdSRobert Watson #endif 482d3c11994SPoul-Henning Kamp if (flags & M_WAITOK) 483b40ce416SJulian Elischer KASSERT(curthread->td_intr_nesting_level == 0, 484a163d034SWarner Losh ("malloc(M_WAITOK) in interrupt context")); 485e4eb384bSBosko Milekic 486e4eb384bSBosko Milekic #ifdef DEBUG_MEMGUARD 4878d689e04SGleb Smirnoff if (memguard_cmp_mtp(mtp, size)) { 488e3813573SMatthew D Fleming va = memguard_alloc(size, flags); 489e3813573SMatthew D Fleming if (va != NULL) 490e3813573SMatthew D Fleming return (va); 491e3813573SMatthew D Fleming /* This is unfortunate but should not be fatal. */ 492e3813573SMatthew D Fleming } 493e4eb384bSBosko Milekic #endif 494e4eb384bSBosko Milekic 495847a2a17SPawel Jakub Dawidek #ifdef DEBUG_REDZONE 496847a2a17SPawel Jakub Dawidek size = redzone_size_ntor(size); 497847a2a17SPawel Jakub Dawidek #endif 498847a2a17SPawel Jakub Dawidek 4998355f576SJeff Roberson if (size <= KMEM_ZMAX) { 500d7854da1SMatthew D Fleming mtip = mtp->ks_handle; 5016f267175SJeff Roberson if (size & KMEM_ZMASK) 5026f267175SJeff Roberson size = (size & ~KMEM_ZMASK) + KMEM_ZBASE; 5036f267175SJeff Roberson indx = kmemsize[size >> KMEM_ZSHIFT]; 504d7854da1SMatthew D Fleming KASSERT(mtip->mti_zone < numzones, 505d7854da1SMatthew D Fleming ("mti_zone %u out of range %d", 506d7854da1SMatthew D Fleming mtip->mti_zone, numzones)); 507d7854da1SMatthew D Fleming zone = kmemzones[indx].kz_zone[mtip->mti_zone]; 5086f267175SJeff Roberson #ifdef MALLOC_PROFILE 5096f267175SJeff Roberson krequests[size >> KMEM_ZSHIFT]++; 5106f267175SJeff Roberson #endif 5118355f576SJeff Roberson va = uma_zalloc(zone, flags); 5124362fadaSBrian Feldman if (va != NULL) 513e20a199fSJeff Roberson size = zone->uz_size; 51463a7e0a3SRobert Watson malloc_type_zone_allocated(mtp, va == NULL ? 0 : size, indx); 5158355f576SJeff Roberson } else { 5166f267175SJeff Roberson size = roundup(size, PAGE_SIZE); 5178355f576SJeff Roberson zone = NULL; 5188355f576SJeff Roberson va = uma_large_malloc(size, flags); 51963a7e0a3SRobert Watson malloc_type_allocated(mtp, va == NULL ? 0 : size); 520df8bae1dSRodney W. Grimes } 5211282e9acSPoul-Henning Kamp if (flags & M_WAITOK) 522a163d034SWarner Losh KASSERT(va != NULL, ("malloc(M_WAITOK) returned NULL")); 5231282e9acSPoul-Henning Kamp else if (va == NULL) 5241fb14a47SPoul-Henning Kamp t_malloc_fail = time_uptime; 5254db4f5c8SPoul-Henning Kamp #ifdef DIAGNOSTIC 5261282e9acSPoul-Henning Kamp if (va != NULL && !(flags & M_ZERO)) { 5274db4f5c8SPoul-Henning Kamp memset(va, 0x70, osize); 5284db4f5c8SPoul-Henning Kamp } 5294db4f5c8SPoul-Henning Kamp #endif 530847a2a17SPawel Jakub Dawidek #ifdef DEBUG_REDZONE 531847a2a17SPawel Jakub Dawidek if (va != NULL) 532847a2a17SPawel Jakub Dawidek va = redzone_setup(va, osize); 533847a2a17SPawel Jakub Dawidek #endif 534df8bae1dSRodney W. Grimes return ((void *) va); 535df8bae1dSRodney W. Grimes } 536df8bae1dSRodney W. Grimes 537df8bae1dSRodney W. Grimes /* 5381c7c3c6aSMatthew Dillon * free: 5391c7c3c6aSMatthew Dillon * 540df8bae1dSRodney W. Grimes * Free a block of memory allocated by malloc. 5411c7c3c6aSMatthew Dillon * 5421c7c3c6aSMatthew Dillon * This routine may not block. 543df8bae1dSRodney W. Grimes */ 544df8bae1dSRodney W. Grimes void 54563a7e0a3SRobert Watson free(void *addr, struct malloc_type *mtp) 546df8bae1dSRodney W. Grimes { 54799571dc3SJeff Roberson uma_slab_t slab; 54899571dc3SJeff Roberson u_long size; 549254c6cb3SPoul-Henning Kamp 550bb1c7df8SRobert Watson KASSERT(mtp->ks_magic == M_MAGIC, ("free: bad malloc type magic")); 551bb1c7df8SRobert Watson 55244a8ff31SArchie Cobbs /* free(NULL, ...) does nothing */ 55344a8ff31SArchie Cobbs if (addr == NULL) 55444a8ff31SArchie Cobbs return; 55544a8ff31SArchie Cobbs 556e4eb384bSBosko Milekic #ifdef DEBUG_MEMGUARD 557e3813573SMatthew D Fleming if (is_memguard_addr(addr)) { 558e4eb384bSBosko Milekic memguard_free(addr); 559e4eb384bSBosko Milekic return; 560e4eb384bSBosko Milekic } 561e4eb384bSBosko Milekic #endif 562e4eb384bSBosko Milekic 563847a2a17SPawel Jakub Dawidek #ifdef DEBUG_REDZONE 564847a2a17SPawel Jakub Dawidek redzone_check(addr); 565847a2a17SPawel Jakub Dawidek addr = redzone_addr_ntor(addr); 566847a2a17SPawel Jakub Dawidek #endif 567847a2a17SPawel Jakub Dawidek 56899571dc3SJeff Roberson slab = vtoslab((vm_offset_t)addr & (~UMA_SLAB_MASK)); 5698355f576SJeff Roberson 5708355f576SJeff Roberson if (slab == NULL) 5716f267175SJeff Roberson panic("free: address %p(%p) has not been allocated.\n", 57299571dc3SJeff Roberson addr, (void *)((u_long)addr & (~UMA_SLAB_MASK))); 57399571dc3SJeff Roberson 5748355f576SJeff Roberson if (!(slab->us_flags & UMA_SLAB_MALLOC)) { 5758f70816cSJeff Roberson #ifdef INVARIANTS 57663a7e0a3SRobert Watson struct malloc_type **mtpp = addr; 5778f70816cSJeff Roberson #endif 578099a0e58SBosko Milekic size = slab->us_keg->uk_size; 5798f70816cSJeff Roberson #ifdef INVARIANTS 5808f70816cSJeff Roberson /* 5818f70816cSJeff Roberson * Cache a pointer to the malloc_type that most recently freed 5828f70816cSJeff Roberson * this memory here. This way we know who is most likely to 5838f70816cSJeff Roberson * have stepped on it later. 5848f70816cSJeff Roberson * 5858f70816cSJeff Roberson * This code assumes that size is a multiple of 8 bytes for 5868f70816cSJeff Roberson * 64 bit machines 5878f70816cSJeff Roberson */ 58863a7e0a3SRobert Watson mtpp = (struct malloc_type **) 58963a7e0a3SRobert Watson ((unsigned long)mtpp & ~UMA_ALIGN_PTR); 59063a7e0a3SRobert Watson mtpp += (size - sizeof(struct malloc_type *)) / 5918f70816cSJeff Roberson sizeof(struct malloc_type *); 59263a7e0a3SRobert Watson *mtpp = mtp; 5938f70816cSJeff Roberson #endif 594099a0e58SBosko Milekic uma_zfree_arg(LIST_FIRST(&slab->us_keg->uk_zones), addr, slab); 59514bf02f8SJohn Dyson } else { 5968355f576SJeff Roberson size = slab->us_size; 5978355f576SJeff Roberson uma_large_free(slab); 59814bf02f8SJohn Dyson } 59963a7e0a3SRobert Watson malloc_type_freed(mtp, size); 600df8bae1dSRodney W. Grimes } 601df8bae1dSRodney W. Grimes 602df8bae1dSRodney W. Grimes /* 60344a8ff31SArchie Cobbs * realloc: change the size of a memory block 60444a8ff31SArchie Cobbs */ 60544a8ff31SArchie Cobbs void * 60663a7e0a3SRobert Watson realloc(void *addr, unsigned long size, struct malloc_type *mtp, int flags) 60744a8ff31SArchie Cobbs { 6088355f576SJeff Roberson uma_slab_t slab; 60944a8ff31SArchie Cobbs unsigned long alloc; 61044a8ff31SArchie Cobbs void *newaddr; 61144a8ff31SArchie Cobbs 612bb1c7df8SRobert Watson KASSERT(mtp->ks_magic == M_MAGIC, 613bb1c7df8SRobert Watson ("realloc: bad malloc type magic")); 614bb1c7df8SRobert Watson 61544a8ff31SArchie Cobbs /* realloc(NULL, ...) is equivalent to malloc(...) */ 61644a8ff31SArchie Cobbs if (addr == NULL) 61763a7e0a3SRobert Watson return (malloc(size, mtp, flags)); 61863a7e0a3SRobert Watson 61963a7e0a3SRobert Watson /* 62063a7e0a3SRobert Watson * XXX: Should report free of old memory and alloc of new memory to 62163a7e0a3SRobert Watson * per-CPU stats. 62263a7e0a3SRobert Watson */ 62344a8ff31SArchie Cobbs 624e4eb384bSBosko Milekic #ifdef DEBUG_MEMGUARD 6256d3ed393SMatthew D Fleming if (is_memguard_addr(addr)) 6266d3ed393SMatthew D Fleming return (memguard_realloc(addr, size, mtp, flags)); 627e4eb384bSBosko Milekic #endif 628e4eb384bSBosko Milekic 629847a2a17SPawel Jakub Dawidek #ifdef DEBUG_REDZONE 630847a2a17SPawel Jakub Dawidek slab = NULL; 631847a2a17SPawel Jakub Dawidek alloc = redzone_get_size(addr); 632847a2a17SPawel Jakub Dawidek #else 63399571dc3SJeff Roberson slab = vtoslab((vm_offset_t)addr & ~(UMA_SLAB_MASK)); 6348355f576SJeff Roberson 63544a8ff31SArchie Cobbs /* Sanity check */ 6368355f576SJeff Roberson KASSERT(slab != NULL, 63744a8ff31SArchie Cobbs ("realloc: address %p out of range", (void *)addr)); 63844a8ff31SArchie Cobbs 63944a8ff31SArchie Cobbs /* Get the size of the original block */ 640619f2841SPawel Jakub Dawidek if (!(slab->us_flags & UMA_SLAB_MALLOC)) 641099a0e58SBosko Milekic alloc = slab->us_keg->uk_size; 6428355f576SJeff Roberson else 6438355f576SJeff Roberson alloc = slab->us_size; 64444a8ff31SArchie Cobbs 64544a8ff31SArchie Cobbs /* Reuse the original block if appropriate */ 64644a8ff31SArchie Cobbs if (size <= alloc 64744a8ff31SArchie Cobbs && (size > (alloc >> REALLOC_FRACTION) || alloc == MINALLOCSIZE)) 64844a8ff31SArchie Cobbs return (addr); 649847a2a17SPawel Jakub Dawidek #endif /* !DEBUG_REDZONE */ 65044a8ff31SArchie Cobbs 65144a8ff31SArchie Cobbs /* Allocate a new, bigger (or smaller) block */ 65263a7e0a3SRobert Watson if ((newaddr = malloc(size, mtp, flags)) == NULL) 65344a8ff31SArchie Cobbs return (NULL); 65444a8ff31SArchie Cobbs 65544a8ff31SArchie Cobbs /* Copy over original contents */ 65644a8ff31SArchie Cobbs bcopy(addr, newaddr, min(size, alloc)); 65763a7e0a3SRobert Watson free(addr, mtp); 65844a8ff31SArchie Cobbs return (newaddr); 65944a8ff31SArchie Cobbs } 66044a8ff31SArchie Cobbs 66144a8ff31SArchie Cobbs /* 66244a8ff31SArchie Cobbs * reallocf: same as realloc() but free memory on failure. 66344a8ff31SArchie Cobbs */ 66444a8ff31SArchie Cobbs void * 66563a7e0a3SRobert Watson reallocf(void *addr, unsigned long size, struct malloc_type *mtp, int flags) 66644a8ff31SArchie Cobbs { 66744a8ff31SArchie Cobbs void *mem; 66844a8ff31SArchie Cobbs 66963a7e0a3SRobert Watson if ((mem = realloc(addr, size, mtp, flags)) == NULL) 67063a7e0a3SRobert Watson free(addr, mtp); 67144a8ff31SArchie Cobbs return (mem); 67244a8ff31SArchie Cobbs } 67344a8ff31SArchie Cobbs 67444a8ff31SArchie Cobbs /* 6755df87b21SJeff Roberson * Wake the page daemon when we exhaust KVA. It will call the lowmem handler 6765df87b21SJeff Roberson * and uma_reclaim() callbacks in a context that is safe. 677df8bae1dSRodney W. Grimes */ 6782b14f991SJulian Elischer static void 6795df87b21SJeff Roberson kmem_reclaim(vmem_t *vm, int flags) 680df8bae1dSRodney W. Grimes { 6818a58a9f6SJohn Dyson 6825df87b21SJeff Roberson pagedaemon_wakeup(); 6835df87b21SJeff Roberson } 6845df87b21SJeff Roberson 685f9d498adSDimitry Andric #ifndef __sparc64__ 686c70af487SAlan Cox CTASSERT(VM_KMEM_SIZE_SCALE >= 1); 687f9d498adSDimitry Andric #endif 688c70af487SAlan Cox 6895df87b21SJeff Roberson /* 690c70af487SAlan Cox * Initialize the kernel memory (kmem) arena. 6915df87b21SJeff Roberson */ 6925df87b21SJeff Roberson void 6935df87b21SJeff Roberson kmeminit(void) 6945df87b21SJeff Roberson { 695af3b2549SHans Petter Selasky u_long mem_size; 696af3b2549SHans Petter Selasky u_long tmp; 69769ef67f9SJason Evans 698af3b2549SHans Petter Selasky #ifdef VM_KMEM_SIZE 699af3b2549SHans Petter Selasky if (vm_kmem_size == 0) 700af3b2549SHans Petter Selasky vm_kmem_size = VM_KMEM_SIZE; 701af3b2549SHans Petter Selasky #endif 702af3b2549SHans Petter Selasky #ifdef VM_KMEM_SIZE_MIN 703af3b2549SHans Petter Selasky if (vm_kmem_size_min == 0) 704af3b2549SHans Petter Selasky vm_kmem_size_min = VM_KMEM_SIZE_MIN; 705af3b2549SHans Petter Selasky #endif 706af3b2549SHans Petter Selasky #ifdef VM_KMEM_SIZE_MAX 707af3b2549SHans Petter Selasky if (vm_kmem_size_max == 0) 708af3b2549SHans Petter Selasky vm_kmem_size_max = VM_KMEM_SIZE_MAX; 709af3b2549SHans Petter Selasky #endif 7108a58a9f6SJohn Dyson /* 711c70af487SAlan Cox * Calculate the amount of kernel virtual address (KVA) space that is 712c70af487SAlan Cox * preallocated to the kmem arena. In order to support a wide range 713c70af487SAlan Cox * of machines, it is a function of the physical memory size, 714c70af487SAlan Cox * specifically, 7158a58a9f6SJohn Dyson * 716c70af487SAlan Cox * min(max(physical memory size / VM_KMEM_SIZE_SCALE, 717c70af487SAlan Cox * VM_KMEM_SIZE_MIN), VM_KMEM_SIZE_MAX) 718c70af487SAlan Cox * 719c70af487SAlan Cox * Every architecture must define an integral value for 720c70af487SAlan Cox * VM_KMEM_SIZE_SCALE. However, the definitions of VM_KMEM_SIZE_MIN 721c70af487SAlan Cox * and VM_KMEM_SIZE_MAX, which represent respectively the floor and 722c70af487SAlan Cox * ceiling on this preallocation, are optional. Typically, 723c70af487SAlan Cox * VM_KMEM_SIZE_MAX is itself a function of the available KVA space on 724c70af487SAlan Cox * a given architecture. 7258a58a9f6SJohn Dyson */ 72644f1c916SBryan Drewery mem_size = vm_cnt.v_page_count; 7278a58a9f6SJohn Dyson 728c70af487SAlan Cox if (vm_kmem_size_scale < 1) 729c70af487SAlan Cox vm_kmem_size_scale = VM_KMEM_SIZE_SCALE; 730c70af487SAlan Cox 731af3b2549SHans Petter Selasky /* 732af3b2549SHans Petter Selasky * Check if we should use defaults for the "vm_kmem_size" 733af3b2549SHans Petter Selasky * variable: 734af3b2549SHans Petter Selasky */ 735af3b2549SHans Petter Selasky if (vm_kmem_size == 0) { 736479439b4SDag-Erling Smørgrav vm_kmem_size = (mem_size / vm_kmem_size_scale) * PAGE_SIZE; 7378a58a9f6SJohn Dyson 738c70af487SAlan Cox if (vm_kmem_size_min > 0 && vm_kmem_size < vm_kmem_size_min) 7390e5179e4SStephane E. Potvin vm_kmem_size = vm_kmem_size_min; 740479439b4SDag-Erling Smørgrav if (vm_kmem_size_max > 0 && vm_kmem_size >= vm_kmem_size_max) 741479439b4SDag-Erling Smørgrav vm_kmem_size = vm_kmem_size_max; 742af3b2549SHans Petter Selasky } 7438a58a9f6SJohn Dyson 74427b8623fSDavid Greenman /* 745af3b2549SHans Petter Selasky * The amount of KVA space that is preallocated to the 746c70af487SAlan Cox * kmem arena can be set statically at compile-time or manually 747c70af487SAlan Cox * through the kernel environment. However, it is still limited to 748c70af487SAlan Cox * twice the physical memory size, which has been sufficient to handle 749c70af487SAlan Cox * the most severe cases of external fragmentation in the kmem arena. 75027b8623fSDavid Greenman */ 751c749c003SAlan Cox if (vm_kmem_size / 2 / PAGE_SIZE > mem_size) 752c749c003SAlan Cox vm_kmem_size = 2 * mem_size * PAGE_SIZE; 7538a58a9f6SJohn Dyson 754e137643eSOlivier Houchard vm_kmem_size = round_page(vm_kmem_size); 755e3813573SMatthew D Fleming #ifdef DEBUG_MEMGUARD 756f806cdcfSMatthew D Fleming tmp = memguard_fudge(vm_kmem_size, kernel_map); 757e3813573SMatthew D Fleming #else 758e3813573SMatthew D Fleming tmp = vm_kmem_size; 759e3813573SMatthew D Fleming #endif 7605df87b21SJeff Roberson vmem_init(kmem_arena, "kmem arena", kva_alloc(tmp), tmp, PAGE_SIZE, 76199de9af2SJeff Roberson 0, 0); 7625df87b21SJeff Roberson vmem_set_reclaim(kmem_arena, kmem_reclaim); 7638355f576SJeff Roberson 764e4eb384bSBosko Milekic #ifdef DEBUG_MEMGUARD 765e4eb384bSBosko Milekic /* 766e4eb384bSBosko Milekic * Initialize MemGuard if support compiled in. MemGuard is a 767e4eb384bSBosko Milekic * replacement allocator used for detecting tamper-after-free 768e4eb384bSBosko Milekic * scenarios as they occur. It is only used for debugging. 769e4eb384bSBosko Milekic */ 7705df87b21SJeff Roberson memguard_init(kmem_arena); 771e4eb384bSBosko Milekic #endif 7725df87b21SJeff Roberson } 7735df87b21SJeff Roberson 7745df87b21SJeff Roberson /* 7755df87b21SJeff Roberson * Initialize the kernel memory allocator 7765df87b21SJeff Roberson */ 7775df87b21SJeff Roberson /* ARGSUSED*/ 7785df87b21SJeff Roberson static void 7795df87b21SJeff Roberson mallocinit(void *dummy) 7805df87b21SJeff Roberson { 7815df87b21SJeff Roberson int i; 7825df87b21SJeff Roberson uint8_t indx; 7835df87b21SJeff Roberson 7845df87b21SJeff Roberson mtx_init(&malloc_mtx, "malloc", NULL, MTX_DEF); 7855df87b21SJeff Roberson 7865df87b21SJeff Roberson kmeminit(); 787e4eb384bSBosko Milekic 78899571dc3SJeff Roberson uma_startup2(); 7898355f576SJeff Roberson 79063a7e0a3SRobert Watson mt_zone = uma_zcreate("mt_zone", sizeof(struct malloc_type_internal), 79163a7e0a3SRobert Watson #ifdef INVARIANTS 79263a7e0a3SRobert Watson mtrash_ctor, mtrash_dtor, mtrash_init, mtrash_fini, 79363a7e0a3SRobert Watson #else 79463a7e0a3SRobert Watson NULL, NULL, NULL, NULL, 79563a7e0a3SRobert Watson #endif 79663a7e0a3SRobert Watson UMA_ALIGN_PTR, UMA_ZONE_MALLOC); 7976f267175SJeff Roberson for (i = 0, indx = 0; kmemzones[indx].kz_size != 0; indx++) { 7986f267175SJeff Roberson int size = kmemzones[indx].kz_size; 7996f267175SJeff Roberson char *name = kmemzones[indx].kz_name; 800d7854da1SMatthew D Fleming int subzone; 8018355f576SJeff Roberson 802d7854da1SMatthew D Fleming for (subzone = 0; subzone < numzones; subzone++) { 803d7854da1SMatthew D Fleming kmemzones[indx].kz_zone[subzone] = 804d7854da1SMatthew D Fleming uma_zcreate(name, size, 8058efc4effSJeff Roberson #ifdef INVARIANTS 8068f70816cSJeff Roberson mtrash_ctor, mtrash_dtor, mtrash_init, mtrash_fini, 8078efc4effSJeff Roberson #else 8088efc4effSJeff Roberson NULL, NULL, NULL, NULL, 8098efc4effSJeff Roberson #endif 8108efc4effSJeff Roberson UMA_ALIGN_PTR, UMA_ZONE_MALLOC); 811d7854da1SMatthew D Fleming } 8128355f576SJeff Roberson for (;i <= size; i+= KMEM_ZBASE) 8136f267175SJeff Roberson kmemsize[i >> KMEM_ZSHIFT] = indx; 8148355f576SJeff Roberson 815df8bae1dSRodney W. Grimes } 816254c6cb3SPoul-Henning Kamp } 817af3b2549SHans Petter Selasky SYSINIT(kmem, SI_SUB_KMEM, SI_ORDER_SECOND, mallocinit, NULL); 818254c6cb3SPoul-Henning Kamp 819db669378SPeter Wemm void 82087efd4d5SRobert Watson malloc_init(void *data) 821254c6cb3SPoul-Henning Kamp { 82263a7e0a3SRobert Watson struct malloc_type_internal *mtip; 82363a7e0a3SRobert Watson struct malloc_type *mtp; 82463a7e0a3SRobert Watson 82544f1c916SBryan Drewery KASSERT(vm_cnt.v_page_count != 0, ("malloc_register before vm_init")); 82663a7e0a3SRobert Watson 82763a7e0a3SRobert Watson mtp = data; 828f121baaaSBrian Somers if (mtp->ks_magic != M_MAGIC) 829f121baaaSBrian Somers panic("malloc_init: bad malloc type magic"); 830bb1c7df8SRobert Watson 83163a7e0a3SRobert Watson mtip = uma_zalloc(mt_zone, M_WAITOK | M_ZERO); 83263a7e0a3SRobert Watson mtp->ks_handle = mtip; 833d7854da1SMatthew D Fleming mtip->mti_zone = mtp_get_subzone(mtp->ks_shortdesc); 834254c6cb3SPoul-Henning Kamp 8356f267175SJeff Roberson mtx_lock(&malloc_mtx); 83663a7e0a3SRobert Watson mtp->ks_next = kmemstatistics; 83763a7e0a3SRobert Watson kmemstatistics = mtp; 838cd814b26SRobert Watson kmemcount++; 8396f267175SJeff Roberson mtx_unlock(&malloc_mtx); 840df8bae1dSRodney W. Grimes } 841db669378SPeter Wemm 842db669378SPeter Wemm void 84387efd4d5SRobert Watson malloc_uninit(void *data) 844db669378SPeter Wemm { 84563a7e0a3SRobert Watson struct malloc_type_internal *mtip; 8462a143d5bSPawel Jakub Dawidek struct malloc_type_stats *mtsp; 84763a7e0a3SRobert Watson struct malloc_type *mtp, *temp; 84845d48bdaSPaul Saab uma_slab_t slab; 8492a143d5bSPawel Jakub Dawidek long temp_allocs, temp_bytes; 8502a143d5bSPawel Jakub Dawidek int i; 851db669378SPeter Wemm 85263a7e0a3SRobert Watson mtp = data; 853bb1c7df8SRobert Watson KASSERT(mtp->ks_magic == M_MAGIC, 854bb1c7df8SRobert Watson ("malloc_uninit: bad malloc type magic")); 85563a7e0a3SRobert Watson KASSERT(mtp->ks_handle != NULL, ("malloc_deregister: cookie NULL")); 856bb1c7df8SRobert Watson 8576f267175SJeff Roberson mtx_lock(&malloc_mtx); 85863a7e0a3SRobert Watson mtip = mtp->ks_handle; 85963a7e0a3SRobert Watson mtp->ks_handle = NULL; 86063a7e0a3SRobert Watson if (mtp != kmemstatistics) { 86163a7e0a3SRobert Watson for (temp = kmemstatistics; temp != NULL; 86263a7e0a3SRobert Watson temp = temp->ks_next) { 863f121baaaSBrian Somers if (temp->ks_next == mtp) { 86463a7e0a3SRobert Watson temp->ks_next = mtp->ks_next; 865f121baaaSBrian Somers break; 866db669378SPeter Wemm } 867f121baaaSBrian Somers } 868f121baaaSBrian Somers KASSERT(temp, 869f121baaaSBrian Somers ("malloc_uninit: type '%s' not found", mtp->ks_shortdesc)); 87063a7e0a3SRobert Watson } else 87163a7e0a3SRobert Watson kmemstatistics = mtp->ks_next; 872cd814b26SRobert Watson kmemcount--; 8736f267175SJeff Roberson mtx_unlock(&malloc_mtx); 8742a143d5bSPawel Jakub Dawidek 8752a143d5bSPawel Jakub Dawidek /* 8762a143d5bSPawel Jakub Dawidek * Look for memory leaks. 8772a143d5bSPawel Jakub Dawidek */ 8782a143d5bSPawel Jakub Dawidek temp_allocs = temp_bytes = 0; 8792a143d5bSPawel Jakub Dawidek for (i = 0; i < MAXCPU; i++) { 8802a143d5bSPawel Jakub Dawidek mtsp = &mtip->mti_stats[i]; 8812a143d5bSPawel Jakub Dawidek temp_allocs += mtsp->mts_numallocs; 8822a143d5bSPawel Jakub Dawidek temp_allocs -= mtsp->mts_numfrees; 8832a143d5bSPawel Jakub Dawidek temp_bytes += mtsp->mts_memalloced; 8842a143d5bSPawel Jakub Dawidek temp_bytes -= mtsp->mts_memfreed; 8852a143d5bSPawel Jakub Dawidek } 8862a143d5bSPawel Jakub Dawidek if (temp_allocs > 0 || temp_bytes > 0) { 8872a143d5bSPawel Jakub Dawidek printf("Warning: memory type %s leaked memory on destroy " 8882a143d5bSPawel Jakub Dawidek "(%ld allocations, %ld bytes leaked).\n", mtp->ks_shortdesc, 8892a143d5bSPawel Jakub Dawidek temp_allocs, temp_bytes); 8902a143d5bSPawel Jakub Dawidek } 8912a143d5bSPawel Jakub Dawidek 89245d48bdaSPaul Saab slab = vtoslab((vm_offset_t) mtip & (~UMA_SLAB_MASK)); 89345d48bdaSPaul Saab uma_zfree_arg(mt_zone, mtip, slab); 894db669378SPeter Wemm } 8956f267175SJeff Roberson 896d362c40dSPawel Jakub Dawidek struct malloc_type * 897d362c40dSPawel Jakub Dawidek malloc_desc2type(const char *desc) 898d362c40dSPawel Jakub Dawidek { 899d362c40dSPawel Jakub Dawidek struct malloc_type *mtp; 900d362c40dSPawel Jakub Dawidek 901d362c40dSPawel Jakub Dawidek mtx_assert(&malloc_mtx, MA_OWNED); 902d362c40dSPawel Jakub Dawidek for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) { 903d362c40dSPawel Jakub Dawidek if (strcmp(mtp->ks_shortdesc, desc) == 0) 904d362c40dSPawel Jakub Dawidek return (mtp); 905d362c40dSPawel Jakub Dawidek } 906d362c40dSPawel Jakub Dawidek return (NULL); 907d362c40dSPawel Jakub Dawidek } 908d362c40dSPawel Jakub Dawidek 9096f267175SJeff Roberson static int 910cd814b26SRobert Watson sysctl_kern_malloc_stats(SYSCTL_HANDLER_ARGS) 911cd814b26SRobert Watson { 912cd814b26SRobert Watson struct malloc_type_stream_header mtsh; 913cd814b26SRobert Watson struct malloc_type_internal *mtip; 914cd814b26SRobert Watson struct malloc_type_header mth; 915cd814b26SRobert Watson struct malloc_type *mtp; 9164e657159SMatthew D Fleming int error, i; 917cd814b26SRobert Watson struct sbuf sbuf; 918cd814b26SRobert Watson 91900f0e671SMatthew D Fleming error = sysctl_wire_old_buffer(req, 0); 92000f0e671SMatthew D Fleming if (error != 0) 92100f0e671SMatthew D Fleming return (error); 9224e657159SMatthew D Fleming sbuf_new_for_sysctl(&sbuf, NULL, 128, req); 923cd814b26SRobert Watson mtx_lock(&malloc_mtx); 924cd814b26SRobert Watson 925cd814b26SRobert Watson /* 926cd814b26SRobert Watson * Insert stream header. 927cd814b26SRobert Watson */ 928cd814b26SRobert Watson bzero(&mtsh, sizeof(mtsh)); 929cd814b26SRobert Watson mtsh.mtsh_version = MALLOC_TYPE_STREAM_VERSION; 930cd814b26SRobert Watson mtsh.mtsh_maxcpus = MAXCPU; 931cd814b26SRobert Watson mtsh.mtsh_count = kmemcount; 9324e657159SMatthew D Fleming (void)sbuf_bcat(&sbuf, &mtsh, sizeof(mtsh)); 933cd814b26SRobert Watson 934cd814b26SRobert Watson /* 935cd814b26SRobert Watson * Insert alternating sequence of type headers and type statistics. 936cd814b26SRobert Watson */ 937cd814b26SRobert Watson for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) { 938cd814b26SRobert Watson mtip = (struct malloc_type_internal *)mtp->ks_handle; 939cd814b26SRobert Watson 940cd814b26SRobert Watson /* 941cd814b26SRobert Watson * Insert type header. 942cd814b26SRobert Watson */ 943cd814b26SRobert Watson bzero(&mth, sizeof(mth)); 944cd814b26SRobert Watson strlcpy(mth.mth_name, mtp->ks_shortdesc, MALLOC_MAX_NAME); 9454e657159SMatthew D Fleming (void)sbuf_bcat(&sbuf, &mth, sizeof(mth)); 946cd814b26SRobert Watson 947cd814b26SRobert Watson /* 948cd814b26SRobert Watson * Insert type statistics for each CPU. 949cd814b26SRobert Watson */ 950cd814b26SRobert Watson for (i = 0; i < MAXCPU; i++) { 9514e657159SMatthew D Fleming (void)sbuf_bcat(&sbuf, &mtip->mti_stats[i], 9524e657159SMatthew D Fleming sizeof(mtip->mti_stats[i])); 953cd814b26SRobert Watson } 954cd814b26SRobert Watson } 955cd814b26SRobert Watson mtx_unlock(&malloc_mtx); 9564e657159SMatthew D Fleming error = sbuf_finish(&sbuf); 957cd814b26SRobert Watson sbuf_delete(&sbuf); 958cd814b26SRobert Watson return (error); 959cd814b26SRobert Watson } 960cd814b26SRobert Watson 961cd814b26SRobert Watson SYSCTL_PROC(_kern, OID_AUTO, malloc_stats, CTLFLAG_RD|CTLTYPE_STRUCT, 962cd814b26SRobert Watson 0, 0, sysctl_kern_malloc_stats, "s,malloc_type_ustats", 963cd814b26SRobert Watson "Return malloc types"); 964cd814b26SRobert Watson 965cd814b26SRobert Watson SYSCTL_INT(_kern, OID_AUTO, malloc_count, CTLFLAG_RD, &kmemcount, 0, 966cd814b26SRobert Watson "Count of kernel malloc types"); 967cd814b26SRobert Watson 96891dd776cSJohn Birrell void 96991dd776cSJohn Birrell malloc_type_list(malloc_type_list_func_t *func, void *arg) 97091dd776cSJohn Birrell { 97191dd776cSJohn Birrell struct malloc_type *mtp, **bufmtp; 97291dd776cSJohn Birrell int count, i; 97391dd776cSJohn Birrell size_t buflen; 97491dd776cSJohn Birrell 97591dd776cSJohn Birrell mtx_lock(&malloc_mtx); 97691dd776cSJohn Birrell restart: 97791dd776cSJohn Birrell mtx_assert(&malloc_mtx, MA_OWNED); 97891dd776cSJohn Birrell count = kmemcount; 97991dd776cSJohn Birrell mtx_unlock(&malloc_mtx); 98091dd776cSJohn Birrell 98191dd776cSJohn Birrell buflen = sizeof(struct malloc_type *) * count; 98291dd776cSJohn Birrell bufmtp = malloc(buflen, M_TEMP, M_WAITOK); 98391dd776cSJohn Birrell 98491dd776cSJohn Birrell mtx_lock(&malloc_mtx); 98591dd776cSJohn Birrell 98691dd776cSJohn Birrell if (count < kmemcount) { 98791dd776cSJohn Birrell free(bufmtp, M_TEMP); 98891dd776cSJohn Birrell goto restart; 98991dd776cSJohn Birrell } 99091dd776cSJohn Birrell 99191dd776cSJohn Birrell for (mtp = kmemstatistics, i = 0; mtp != NULL; mtp = mtp->ks_next, i++) 99291dd776cSJohn Birrell bufmtp[i] = mtp; 99391dd776cSJohn Birrell 99491dd776cSJohn Birrell mtx_unlock(&malloc_mtx); 99591dd776cSJohn Birrell 99691dd776cSJohn Birrell for (i = 0; i < count; i++) 99791dd776cSJohn Birrell (func)(bufmtp[i], arg); 99891dd776cSJohn Birrell 99991dd776cSJohn Birrell free(bufmtp, M_TEMP); 100091dd776cSJohn Birrell } 100191dd776cSJohn Birrell 1002909ed16cSRobert Watson #ifdef DDB 1003909ed16cSRobert Watson DB_SHOW_COMMAND(malloc, db_show_malloc) 1004909ed16cSRobert Watson { 1005909ed16cSRobert Watson struct malloc_type_internal *mtip; 1006909ed16cSRobert Watson struct malloc_type *mtp; 100760ae52f7SEd Schouten uint64_t allocs, frees; 100860ae52f7SEd Schouten uint64_t alloced, freed; 1009909ed16cSRobert Watson int i; 1010909ed16cSRobert Watson 101124076d13SRobert Watson db_printf("%18s %12s %12s %12s\n", "Type", "InUse", "MemUse", 101224076d13SRobert Watson "Requests"); 1013909ed16cSRobert Watson for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) { 1014909ed16cSRobert Watson mtip = (struct malloc_type_internal *)mtp->ks_handle; 1015909ed16cSRobert Watson allocs = 0; 1016909ed16cSRobert Watson frees = 0; 101724076d13SRobert Watson alloced = 0; 101824076d13SRobert Watson freed = 0; 1019909ed16cSRobert Watson for (i = 0; i < MAXCPU; i++) { 1020909ed16cSRobert Watson allocs += mtip->mti_stats[i].mts_numallocs; 1021909ed16cSRobert Watson frees += mtip->mti_stats[i].mts_numfrees; 102224076d13SRobert Watson alloced += mtip->mti_stats[i].mts_memalloced; 102324076d13SRobert Watson freed += mtip->mti_stats[i].mts_memfreed; 1024909ed16cSRobert Watson } 102524076d13SRobert Watson db_printf("%18s %12ju %12juK %12ju\n", 102624076d13SRobert Watson mtp->ks_shortdesc, allocs - frees, 102724076d13SRobert Watson (alloced - freed + 1023) / 1024, allocs); 1028687c94aaSJohn Baldwin if (db_pager_quit) 1029687c94aaSJohn Baldwin break; 1030909ed16cSRobert Watson } 1031909ed16cSRobert Watson } 1032d7854da1SMatthew D Fleming 1033d7854da1SMatthew D Fleming #if MALLOC_DEBUG_MAXZONES > 1 1034d7854da1SMatthew D Fleming DB_SHOW_COMMAND(multizone_matches, db_show_multizone_matches) 1035d7854da1SMatthew D Fleming { 1036d7854da1SMatthew D Fleming struct malloc_type_internal *mtip; 1037d7854da1SMatthew D Fleming struct malloc_type *mtp; 1038d7854da1SMatthew D Fleming u_int subzone; 1039d7854da1SMatthew D Fleming 1040d7854da1SMatthew D Fleming if (!have_addr) { 1041d7854da1SMatthew D Fleming db_printf("Usage: show multizone_matches <malloc type/addr>\n"); 1042d7854da1SMatthew D Fleming return; 1043d7854da1SMatthew D Fleming } 1044d7854da1SMatthew D Fleming mtp = (void *)addr; 1045d7854da1SMatthew D Fleming if (mtp->ks_magic != M_MAGIC) { 1046d7854da1SMatthew D Fleming db_printf("Magic %lx does not match expected %x\n", 1047d7854da1SMatthew D Fleming mtp->ks_magic, M_MAGIC); 1048d7854da1SMatthew D Fleming return; 1049d7854da1SMatthew D Fleming } 1050d7854da1SMatthew D Fleming 1051d7854da1SMatthew D Fleming mtip = mtp->ks_handle; 1052d7854da1SMatthew D Fleming subzone = mtip->mti_zone; 1053d7854da1SMatthew D Fleming 1054d7854da1SMatthew D Fleming for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) { 1055d7854da1SMatthew D Fleming mtip = mtp->ks_handle; 1056d7854da1SMatthew D Fleming if (mtip->mti_zone != subzone) 1057d7854da1SMatthew D Fleming continue; 1058d7854da1SMatthew D Fleming db_printf("%s\n", mtp->ks_shortdesc); 1059687c94aaSJohn Baldwin if (db_pager_quit) 1060687c94aaSJohn Baldwin break; 1061d7854da1SMatthew D Fleming } 1062d7854da1SMatthew D Fleming } 1063d7854da1SMatthew D Fleming #endif /* MALLOC_DEBUG_MAXZONES > 1 */ 1064d7854da1SMatthew D Fleming #endif /* DDB */ 1065909ed16cSRobert Watson 10665e914b96SJeff Roberson #ifdef MALLOC_PROFILE 10675e914b96SJeff Roberson 10685e914b96SJeff Roberson static int 10695e914b96SJeff Roberson sysctl_kern_mprof(SYSCTL_HANDLER_ARGS) 10705e914b96SJeff Roberson { 107163a7e0a3SRobert Watson struct sbuf sbuf; 10725e914b96SJeff Roberson uint64_t count; 10735e914b96SJeff Roberson uint64_t waste; 10745e914b96SJeff Roberson uint64_t mem; 10755e914b96SJeff Roberson int error; 10765e914b96SJeff Roberson int rsize; 10775e914b96SJeff Roberson int size; 10785e914b96SJeff Roberson int i; 10795e914b96SJeff Roberson 10805e914b96SJeff Roberson waste = 0; 10815e914b96SJeff Roberson mem = 0; 10825e914b96SJeff Roberson 108300f0e671SMatthew D Fleming error = sysctl_wire_old_buffer(req, 0); 108400f0e671SMatthew D Fleming if (error != 0) 108500f0e671SMatthew D Fleming return (error); 10864e657159SMatthew D Fleming sbuf_new_for_sysctl(&sbuf, NULL, 128, req); 108763a7e0a3SRobert Watson sbuf_printf(&sbuf, 10885e914b96SJeff Roberson "\n Size Requests Real Size\n"); 10895e914b96SJeff Roberson for (i = 0; i < KMEM_ZSIZE; i++) { 10905e914b96SJeff Roberson size = i << KMEM_ZSHIFT; 10915e914b96SJeff Roberson rsize = kmemzones[kmemsize[i]].kz_size; 10925e914b96SJeff Roberson count = (long long unsigned)krequests[i]; 10935e914b96SJeff Roberson 109463a7e0a3SRobert Watson sbuf_printf(&sbuf, "%6d%28llu%11d\n", size, 109563a7e0a3SRobert Watson (unsigned long long)count, rsize); 10965e914b96SJeff Roberson 10975e914b96SJeff Roberson if ((rsize * count) > (size * count)) 10985e914b96SJeff Roberson waste += (rsize * count) - (size * count); 10995e914b96SJeff Roberson mem += (rsize * count); 11005e914b96SJeff Roberson } 110163a7e0a3SRobert Watson sbuf_printf(&sbuf, 11025e914b96SJeff Roberson "\nTotal memory used:\t%30llu\nTotal Memory wasted:\t%30llu\n", 11035e914b96SJeff Roberson (unsigned long long)mem, (unsigned long long)waste); 11044e657159SMatthew D Fleming error = sbuf_finish(&sbuf); 110563a7e0a3SRobert Watson sbuf_delete(&sbuf); 11065e914b96SJeff Roberson return (error); 11075e914b96SJeff Roberson } 11085e914b96SJeff Roberson 11095e914b96SJeff Roberson SYSCTL_OID(_kern, OID_AUTO, mprof, CTLTYPE_STRING|CTLFLAG_RD, 11105e914b96SJeff Roberson NULL, 0, sysctl_kern_mprof, "A", "Malloc Profiling"); 11115e914b96SJeff Roberson #endif /* MALLOC_PROFILE */ 1112