19454b2d8SWarner Losh /*- 251369649SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 351369649SPedro F. Giffuni * 4df8bae1dSRodney W. Grimes * Copyright (c) 1987, 1991, 1993 563a7e0a3SRobert Watson * The Regents of the University of California. 6bb1c7df8SRobert Watson * Copyright (c) 2005-2009 Robert N. M. Watson 7fd91e076SKristof Provost * Copyright (c) 2008 Otto Moerbeek <otto@drijf.net> (mallocarray) 863a7e0a3SRobert Watson * All rights reserved. 9df8bae1dSRodney W. Grimes * 10df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 11df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 12df8bae1dSRodney W. Grimes * are met: 13df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 14df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 15df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 16df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 17df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 1869a28758SEd Maste * 3. Neither the name of the University nor the names of its contributors 19df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 20df8bae1dSRodney W. Grimes * without specific prior written permission. 21df8bae1dSRodney W. Grimes * 22df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32df8bae1dSRodney W. Grimes * SUCH DAMAGE. 33df8bae1dSRodney W. Grimes * 34df8bae1dSRodney W. Grimes * @(#)kern_malloc.c 8.3 (Berkeley) 1/4/94 35df8bae1dSRodney W. Grimes */ 36df8bae1dSRodney W. Grimes 370ce3f16dSRobert Watson /* 380ce3f16dSRobert Watson * Kernel malloc(9) implementation -- general purpose kernel memory allocator 390ce3f16dSRobert Watson * based on memory types. Back end is implemented using the UMA(9) zone 400ce3f16dSRobert Watson * allocator. A set of fixed-size buckets are used for smaller allocations, 410ce3f16dSRobert Watson * and a special UMA allocation interface is used for larger allocations. 420ce3f16dSRobert Watson * Callers declare memory types, and statistics are maintained independently 430ce3f16dSRobert Watson * for each memory type. Statistics are maintained per-CPU for performance 440ce3f16dSRobert Watson * reasons. See malloc(9) and comments in malloc.h for a detailed 450ce3f16dSRobert Watson * description. 460ce3f16dSRobert Watson */ 470ce3f16dSRobert Watson 48677b542eSDavid E. O'Brien #include <sys/cdefs.h> 49677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 50677b542eSDavid E. O'Brien 51909ed16cSRobert Watson #include "opt_ddb.h" 528a58a9f6SJohn Dyson #include "opt_vm.h" 538a58a9f6SJohn Dyson 54df8bae1dSRodney W. Grimes #include <sys/param.h> 5526f9a767SRodney W. Grimes #include <sys/systm.h> 562d50560aSMarcel Moolenaar #include <sys/kdb.h> 57df8bae1dSRodney W. Grimes #include <sys/kernel.h> 58fb919e4dSMark Murray #include <sys/lock.h> 59df8bae1dSRodney W. Grimes #include <sys/malloc.h> 60eec258d2SJohn Baldwin #include <sys/mutex.h> 61efeaf95aSDavid Greenman #include <sys/vmmeter.h> 62a448b62aSJake Burkholder #include <sys/proc.h> 6363a7e0a3SRobert Watson #include <sys/sbuf.h> 646f267175SJeff Roberson #include <sys/sysctl.h> 651fb14a47SPoul-Henning Kamp #include <sys/time.h> 665df87b21SJeff Roberson #include <sys/vmem.h> 679a02e8c6SJason Evans 68df8bae1dSRodney W. Grimes #include <vm/vm.h> 6999571dc3SJeff Roberson #include <vm/pmap.h> 705df87b21SJeff Roberson #include <vm/vm_pageout.h> 71efeaf95aSDavid Greenman #include <vm/vm_param.h> 72df8bae1dSRodney W. Grimes #include <vm/vm_kern.h> 73efeaf95aSDavid Greenman #include <vm/vm_extern.h> 743075778bSJohn Dyson #include <vm/vm_map.h> 7599571dc3SJeff Roberson #include <vm/vm_page.h> 768355f576SJeff Roberson #include <vm/uma.h> 778355f576SJeff Roberson #include <vm/uma_int.h> 788efc4effSJeff Roberson #include <vm/uma_dbg.h> 79df8bae1dSRodney W. Grimes 80e4eb384bSBosko Milekic #ifdef DEBUG_MEMGUARD 81e4eb384bSBosko Milekic #include <vm/memguard.h> 82e4eb384bSBosko Milekic #endif 83847a2a17SPawel Jakub Dawidek #ifdef DEBUG_REDZONE 84847a2a17SPawel Jakub Dawidek #include <vm/redzone.h> 85847a2a17SPawel Jakub Dawidek #endif 86e4eb384bSBosko Milekic 87984982d6SPoul-Henning Kamp #if defined(INVARIANTS) && defined(__i386__) 88984982d6SPoul-Henning Kamp #include <machine/cpu.h> 89984982d6SPoul-Henning Kamp #endif 90984982d6SPoul-Henning Kamp 91909ed16cSRobert Watson #include <ddb/ddb.h> 92909ed16cSRobert Watson 9391dd776cSJohn Birrell #ifdef KDTRACE_HOOKS 9491dd776cSJohn Birrell #include <sys/dtrace_bsd.h> 9591dd776cSJohn Birrell 9691dd776cSJohn Birrell dtrace_malloc_probe_func_t dtrace_malloc_probe; 9791dd776cSJohn Birrell #endif 9891dd776cSJohn Birrell 99*ab3185d1SJeff Roberson #if defined(INVARIANTS) || defined(MALLOC_MAKE_FAILURES) || \ 100*ab3185d1SJeff Roberson defined(DEBUG_MEMGUARD) || defined(DEBUG_REDZONE) 101*ab3185d1SJeff Roberson #define MALLOC_DEBUG 1 102*ab3185d1SJeff Roberson #endif 103*ab3185d1SJeff Roberson 10444a8ff31SArchie Cobbs /* 10544a8ff31SArchie Cobbs * When realloc() is called, if the new size is sufficiently smaller than 10644a8ff31SArchie Cobbs * the old size, realloc() will allocate a new, smaller block to avoid 10744a8ff31SArchie Cobbs * wasting memory. 'Sufficiently smaller' is defined as: newsize <= 10844a8ff31SArchie Cobbs * oldsize / 2^n, where REALLOC_FRACTION defines the value of 'n'. 10944a8ff31SArchie Cobbs */ 11044a8ff31SArchie Cobbs #ifndef REALLOC_FRACTION 11144a8ff31SArchie Cobbs #define REALLOC_FRACTION 1 /* new block if <= half the size */ 11244a8ff31SArchie Cobbs #endif 11344a8ff31SArchie Cobbs 1140ce3f16dSRobert Watson /* 1150ce3f16dSRobert Watson * Centrally define some common malloc types. 1160ce3f16dSRobert Watson */ 1173b6fb885SPoul-Henning Kamp MALLOC_DEFINE(M_CACHE, "cache", "Various Dynamically allocated caches"); 1189ef246c6SBruce Evans MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory"); 1199ef246c6SBruce Evans MALLOC_DEFINE(M_TEMP, "temp", "misc temporary data buffers"); 1209ef246c6SBruce Evans 121db669378SPeter Wemm static struct malloc_type *kmemstatistics; 122cd814b26SRobert Watson static int kmemcount; 1231f6889a1SMatthew Dillon 1248355f576SJeff Roberson #define KMEM_ZSHIFT 4 1258355f576SJeff Roberson #define KMEM_ZBASE 16 1268355f576SJeff Roberson #define KMEM_ZMASK (KMEM_ZBASE - 1) 1278355f576SJeff Roberson 128bda06553SXin LI #define KMEM_ZMAX 65536 1298355f576SJeff Roberson #define KMEM_ZSIZE (KMEM_ZMAX >> KMEM_ZSHIFT) 13060ae52f7SEd Schouten static uint8_t kmemsize[KMEM_ZSIZE + 1]; 1316f267175SJeff Roberson 132d7854da1SMatthew D Fleming #ifndef MALLOC_DEBUG_MAXZONES 133d7854da1SMatthew D Fleming #define MALLOC_DEBUG_MAXZONES 1 134d7854da1SMatthew D Fleming #endif 135d7854da1SMatthew D Fleming static int numzones = MALLOC_DEBUG_MAXZONES; 136d7854da1SMatthew D Fleming 1370ce3f16dSRobert Watson /* 1380ce3f16dSRobert Watson * Small malloc(9) memory allocations are allocated from a set of UMA buckets 1390ce3f16dSRobert Watson * of various sizes. 1400ce3f16dSRobert Watson * 1410ce3f16dSRobert Watson * XXX: The comment here used to read "These won't be powers of two for 1420ce3f16dSRobert Watson * long." It's possible that a significant amount of wasted memory could be 1430ce3f16dSRobert Watson * recovered by tuning the sizes of these buckets. 1440ce3f16dSRobert Watson */ 1458355f576SJeff Roberson struct { 1466f267175SJeff Roberson int kz_size; 1476f267175SJeff Roberson char *kz_name; 148d7854da1SMatthew D Fleming uma_zone_t kz_zone[MALLOC_DEBUG_MAXZONES]; 1496f267175SJeff Roberson } kmemzones[] = { 150d7854da1SMatthew D Fleming {16, "16", }, 151d7854da1SMatthew D Fleming {32, "32", }, 152d7854da1SMatthew D Fleming {64, "64", }, 153d7854da1SMatthew D Fleming {128, "128", }, 154d7854da1SMatthew D Fleming {256, "256", }, 155d7854da1SMatthew D Fleming {512, "512", }, 156d7854da1SMatthew D Fleming {1024, "1024", }, 157d7854da1SMatthew D Fleming {2048, "2048", }, 158d7854da1SMatthew D Fleming {4096, "4096", }, 159d7854da1SMatthew D Fleming {8192, "8192", }, 160d7854da1SMatthew D Fleming {16384, "16384", }, 161d7854da1SMatthew D Fleming {32768, "32768", }, 162d7854da1SMatthew D Fleming {65536, "65536", }, 1638355f576SJeff Roberson {0, NULL}, 1648355f576SJeff Roberson }; 1658355f576SJeff Roberson 1660ce3f16dSRobert Watson /* 1670ce3f16dSRobert Watson * Zone to allocate malloc type descriptions from. For ABI reasons, memory 1680ce3f16dSRobert Watson * types are described by a data structure passed by the declaring code, but 1690ce3f16dSRobert Watson * the malloc(9) implementation has its own data structure describing the 1700ce3f16dSRobert Watson * type and statistics. This permits the malloc(9)-internal data structures 1710ce3f16dSRobert Watson * to be modified without breaking binary-compiled kernel modules that 1720ce3f16dSRobert Watson * declare malloc types. 1730ce3f16dSRobert Watson */ 17463a7e0a3SRobert Watson static uma_zone_t mt_zone; 17563a7e0a3SRobert Watson 176b89eaf4eSAlan Cox u_long vm_kmem_size; 177d801e824SAndriy Gapon SYSCTL_ULONG(_vm, OID_AUTO, kmem_size, CTLFLAG_RDTUN, &vm_kmem_size, 0, 17884344f9fSDag-Erling Smørgrav "Size of kernel memory"); 1795a34a9f0SJeff Roberson 1807001d850SXin LI static u_long kmem_zmax = KMEM_ZMAX; 1817001d850SXin LI SYSCTL_ULONG(_vm, OID_AUTO, kmem_zmax, CTLFLAG_RDTUN, &kmem_zmax, 0, 1827001d850SXin LI "Maximum allocation size that malloc(9) would use UMA as backend"); 1837001d850SXin LI 184b89eaf4eSAlan Cox static u_long vm_kmem_size_min; 185d801e824SAndriy Gapon SYSCTL_ULONG(_vm, OID_AUTO, kmem_size_min, CTLFLAG_RDTUN, &vm_kmem_size_min, 0, 1860e5179e4SStephane E. Potvin "Minimum size of kernel memory"); 1870e5179e4SStephane E. Potvin 188b89eaf4eSAlan Cox static u_long vm_kmem_size_max; 189d801e824SAndriy Gapon SYSCTL_ULONG(_vm, OID_AUTO, kmem_size_max, CTLFLAG_RDTUN, &vm_kmem_size_max, 0, 190479439b4SDag-Erling Smørgrav "Maximum size of kernel memory"); 191479439b4SDag-Erling Smørgrav 1924813ad54SHans Petter Selasky static u_int vm_kmem_size_scale; 193d801e824SAndriy Gapon SYSCTL_UINT(_vm, OID_AUTO, kmem_size_scale, CTLFLAG_RDTUN, &vm_kmem_size_scale, 0, 194479439b4SDag-Erling Smørgrav "Scale factor for kernel memory size"); 195479439b4SDag-Erling Smørgrav 1967814c80aSAndriy Gapon static int sysctl_kmem_map_size(SYSCTL_HANDLER_ARGS); 1977814c80aSAndriy Gapon SYSCTL_PROC(_vm, OID_AUTO, kmem_map_size, 1987814c80aSAndriy Gapon CTLFLAG_RD | CTLTYPE_ULONG | CTLFLAG_MPSAFE, NULL, 0, 1995df87b21SJeff Roberson sysctl_kmem_map_size, "LU", "Current kmem allocation size"); 2007814c80aSAndriy Gapon 20195bb9d38SAndriy Gapon static int sysctl_kmem_map_free(SYSCTL_HANDLER_ARGS); 20295bb9d38SAndriy Gapon SYSCTL_PROC(_vm, OID_AUTO, kmem_map_free, 20395bb9d38SAndriy Gapon CTLFLAG_RD | CTLTYPE_ULONG | CTLFLAG_MPSAFE, NULL, 0, 2045df87b21SJeff Roberson sysctl_kmem_map_free, "LU", "Free space in kmem"); 20595bb9d38SAndriy Gapon 2065a34a9f0SJeff Roberson /* 20799571dc3SJeff Roberson * The malloc_mtx protects the kmemstatistics linked list. 2085a34a9f0SJeff Roberson */ 2095a34a9f0SJeff Roberson struct mtx malloc_mtx; 21069ef67f9SJason Evans 2115e914b96SJeff Roberson #ifdef MALLOC_PROFILE 2125e914b96SJeff Roberson uint64_t krequests[KMEM_ZSIZE + 1]; 2136f267175SJeff Roberson 2145e914b96SJeff Roberson static int sysctl_kern_mprof(SYSCTL_HANDLER_ARGS); 2155e914b96SJeff Roberson #endif 2165e914b96SJeff Roberson 217cd814b26SRobert Watson static int sysctl_kern_malloc_stats(SYSCTL_HANDLER_ARGS); 218df8bae1dSRodney W. Grimes 2190ce3f16dSRobert Watson /* 2200ce3f16dSRobert Watson * time_uptime of the last malloc(9) failure (induced or real). 2210ce3f16dSRobert Watson */ 2221fb14a47SPoul-Henning Kamp static time_t t_malloc_fail; 2231fb14a47SPoul-Henning Kamp 224d7854da1SMatthew D Fleming #if defined(MALLOC_MAKE_FAILURES) || (MALLOC_DEBUG_MAXZONES > 1) 2256472ac3dSEd Schouten static SYSCTL_NODE(_debug, OID_AUTO, malloc, CTLFLAG_RD, 0, 226d7854da1SMatthew D Fleming "Kernel malloc debugging options"); 227d7854da1SMatthew D Fleming #endif 228d7854da1SMatthew D Fleming 229eae870cdSRobert Watson /* 2300ce3f16dSRobert Watson * malloc(9) fault injection -- cause malloc failures every (n) mallocs when 2310ce3f16dSRobert Watson * the caller specifies M_NOWAIT. If set to 0, no failures are caused. 232eae870cdSRobert Watson */ 2330ce3f16dSRobert Watson #ifdef MALLOC_MAKE_FAILURES 234eae870cdSRobert Watson static int malloc_failure_rate; 235eae870cdSRobert Watson static int malloc_nowait_count; 236eae870cdSRobert Watson static int malloc_failure_count; 237af3b2549SHans Petter Selasky SYSCTL_INT(_debug_malloc, OID_AUTO, failure_rate, CTLFLAG_RWTUN, 238eae870cdSRobert Watson &malloc_failure_rate, 0, "Every (n) mallocs with M_NOWAIT will fail"); 239eae870cdSRobert Watson SYSCTL_INT(_debug_malloc, OID_AUTO, failure_count, CTLFLAG_RD, 240eae870cdSRobert Watson &malloc_failure_count, 0, "Number of imposed M_NOWAIT malloc failures"); 241eae870cdSRobert Watson #endif 242eae870cdSRobert Watson 2437814c80aSAndriy Gapon static int 2447814c80aSAndriy Gapon sysctl_kmem_map_size(SYSCTL_HANDLER_ARGS) 2457814c80aSAndriy Gapon { 2467814c80aSAndriy Gapon u_long size; 2477814c80aSAndriy Gapon 2482e47807cSJeff Roberson size = uma_size(); 2497814c80aSAndriy Gapon return (sysctl_handle_long(oidp, &size, 0, req)); 2507814c80aSAndriy Gapon } 2517814c80aSAndriy Gapon 25295bb9d38SAndriy Gapon static int 25395bb9d38SAndriy Gapon sysctl_kmem_map_free(SYSCTL_HANDLER_ARGS) 25495bb9d38SAndriy Gapon { 2552e47807cSJeff Roberson u_long size, limit; 25695bb9d38SAndriy Gapon 2572e47807cSJeff Roberson /* The sysctl is unsigned, implement as a saturation value. */ 2582e47807cSJeff Roberson size = uma_size(); 2592e47807cSJeff Roberson limit = uma_limit(); 2602e47807cSJeff Roberson if (size > limit) 2612e47807cSJeff Roberson size = 0; 2622e47807cSJeff Roberson else 2632e47807cSJeff Roberson size = limit - size; 26495bb9d38SAndriy Gapon return (sysctl_handle_long(oidp, &size, 0, req)); 26595bb9d38SAndriy Gapon } 26695bb9d38SAndriy Gapon 267d7854da1SMatthew D Fleming /* 268d7854da1SMatthew D Fleming * malloc(9) uma zone separation -- sub-page buffer overruns in one 269d7854da1SMatthew D Fleming * malloc type will affect only a subset of other malloc types. 270d7854da1SMatthew D Fleming */ 271d7854da1SMatthew D Fleming #if MALLOC_DEBUG_MAXZONES > 1 272d7854da1SMatthew D Fleming static void 273d7854da1SMatthew D Fleming tunable_set_numzones(void) 274d7854da1SMatthew D Fleming { 275d7854da1SMatthew D Fleming 276d7854da1SMatthew D Fleming TUNABLE_INT_FETCH("debug.malloc.numzones", 277d7854da1SMatthew D Fleming &numzones); 278d7854da1SMatthew D Fleming 279d7854da1SMatthew D Fleming /* Sanity check the number of malloc uma zones. */ 280d7854da1SMatthew D Fleming if (numzones <= 0) 281d7854da1SMatthew D Fleming numzones = 1; 282d7854da1SMatthew D Fleming if (numzones > MALLOC_DEBUG_MAXZONES) 283d7854da1SMatthew D Fleming numzones = MALLOC_DEBUG_MAXZONES; 284d7854da1SMatthew D Fleming } 285d7854da1SMatthew D Fleming SYSINIT(numzones, SI_SUB_TUNABLES, SI_ORDER_ANY, tunable_set_numzones, NULL); 286af3b2549SHans Petter Selasky SYSCTL_INT(_debug_malloc, OID_AUTO, numzones, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, 287d7854da1SMatthew D Fleming &numzones, 0, "Number of malloc uma subzones"); 288d7854da1SMatthew D Fleming 289d7854da1SMatthew D Fleming /* 290d7854da1SMatthew D Fleming * Any number that changes regularly is an okay choice for the 291d7854da1SMatthew D Fleming * offset. Build numbers are pretty good of you have them. 292d7854da1SMatthew D Fleming */ 293d7854da1SMatthew D Fleming static u_int zone_offset = __FreeBSD_version; 294d7854da1SMatthew D Fleming TUNABLE_INT("debug.malloc.zone_offset", &zone_offset); 295d7854da1SMatthew D Fleming SYSCTL_UINT(_debug_malloc, OID_AUTO, zone_offset, CTLFLAG_RDTUN, 296d7854da1SMatthew D Fleming &zone_offset, 0, "Separate malloc types by examining the " 297d7854da1SMatthew D Fleming "Nth character in the malloc type short description."); 298d7854da1SMatthew D Fleming 299d7854da1SMatthew D Fleming static u_int 300d7854da1SMatthew D Fleming mtp_get_subzone(const char *desc) 301d7854da1SMatthew D Fleming { 302d7854da1SMatthew D Fleming size_t len; 303d7854da1SMatthew D Fleming u_int val; 304d7854da1SMatthew D Fleming 305d7854da1SMatthew D Fleming if (desc == NULL || (len = strlen(desc)) == 0) 306d7854da1SMatthew D Fleming return (0); 307d7854da1SMatthew D Fleming val = desc[zone_offset % len]; 308d7854da1SMatthew D Fleming return (val % numzones); 309d7854da1SMatthew D Fleming } 310d7854da1SMatthew D Fleming #elif MALLOC_DEBUG_MAXZONES == 0 311d7854da1SMatthew D Fleming #error "MALLOC_DEBUG_MAXZONES must be positive." 312d7854da1SMatthew D Fleming #else 313d7854da1SMatthew D Fleming static inline u_int 314d7854da1SMatthew D Fleming mtp_get_subzone(const char *desc) 315d7854da1SMatthew D Fleming { 316d7854da1SMatthew D Fleming 317d7854da1SMatthew D Fleming return (0); 318d7854da1SMatthew D Fleming } 319d7854da1SMatthew D Fleming #endif /* MALLOC_DEBUG_MAXZONES > 1 */ 320d7854da1SMatthew D Fleming 3211fb14a47SPoul-Henning Kamp int 3221fb14a47SPoul-Henning Kamp malloc_last_fail(void) 3231fb14a47SPoul-Henning Kamp { 3241fb14a47SPoul-Henning Kamp 3251fb14a47SPoul-Henning Kamp return (time_uptime - t_malloc_fail); 3261fb14a47SPoul-Henning Kamp } 3271fb14a47SPoul-Henning Kamp 328df8bae1dSRodney W. Grimes /* 3290ce3f16dSRobert Watson * An allocation has succeeded -- update malloc type statistics for the 3300ce3f16dSRobert Watson * amount of bucket size. Occurs within a critical section so that the 3310ce3f16dSRobert Watson * thread isn't preempted and doesn't migrate while updating per-PCU 3320ce3f16dSRobert Watson * statistics. 3334362fadaSBrian Feldman */ 3344362fadaSBrian Feldman static void 33563a7e0a3SRobert Watson malloc_type_zone_allocated(struct malloc_type *mtp, unsigned long size, 3364362fadaSBrian Feldman int zindx) 3374362fadaSBrian Feldman { 33863a7e0a3SRobert Watson struct malloc_type_internal *mtip; 33963a7e0a3SRobert Watson struct malloc_type_stats *mtsp; 34063a7e0a3SRobert Watson 34163a7e0a3SRobert Watson critical_enter(); 34263a7e0a3SRobert Watson mtip = mtp->ks_handle; 34363a7e0a3SRobert Watson mtsp = &mtip->mti_stats[curcpu]; 34473864adbSPawel Jakub Dawidek if (size > 0) { 34563a7e0a3SRobert Watson mtsp->mts_memalloced += size; 34663a7e0a3SRobert Watson mtsp->mts_numallocs++; 34773864adbSPawel Jakub Dawidek } 3484362fadaSBrian Feldman if (zindx != -1) 34963a7e0a3SRobert Watson mtsp->mts_size |= 1 << zindx; 35091dd776cSJohn Birrell 35191dd776cSJohn Birrell #ifdef KDTRACE_HOOKS 35291dd776cSJohn Birrell if (dtrace_malloc_probe != NULL) { 35391dd776cSJohn Birrell uint32_t probe_id = mtip->mti_probes[DTMALLOC_PROBE_MALLOC]; 35491dd776cSJohn Birrell if (probe_id != 0) 35591dd776cSJohn Birrell (dtrace_malloc_probe)(probe_id, 35691dd776cSJohn Birrell (uintptr_t) mtp, (uintptr_t) mtip, 35791dd776cSJohn Birrell (uintptr_t) mtsp, size, zindx); 35891dd776cSJohn Birrell } 35991dd776cSJohn Birrell #endif 36091dd776cSJohn Birrell 36163a7e0a3SRobert Watson critical_exit(); 3624362fadaSBrian Feldman } 3634362fadaSBrian Feldman 3644362fadaSBrian Feldman void 36563a7e0a3SRobert Watson malloc_type_allocated(struct malloc_type *mtp, unsigned long size) 3664362fadaSBrian Feldman { 36763a7e0a3SRobert Watson 36873864adbSPawel Jakub Dawidek if (size > 0) 36963a7e0a3SRobert Watson malloc_type_zone_allocated(mtp, size, -1); 3704362fadaSBrian Feldman } 3714362fadaSBrian Feldman 3724362fadaSBrian Feldman /* 3733805385eSRobert Watson * A free operation has occurred -- update malloc type statistics for the 3740ce3f16dSRobert Watson * amount of the bucket size. Occurs within a critical section so that the 3750ce3f16dSRobert Watson * thread isn't preempted and doesn't migrate while updating per-CPU 3760ce3f16dSRobert Watson * statistics. 3774362fadaSBrian Feldman */ 3784362fadaSBrian Feldman void 37963a7e0a3SRobert Watson malloc_type_freed(struct malloc_type *mtp, unsigned long size) 3804362fadaSBrian Feldman { 38163a7e0a3SRobert Watson struct malloc_type_internal *mtip; 38263a7e0a3SRobert Watson struct malloc_type_stats *mtsp; 38363a7e0a3SRobert Watson 38463a7e0a3SRobert Watson critical_enter(); 38563a7e0a3SRobert Watson mtip = mtp->ks_handle; 38663a7e0a3SRobert Watson mtsp = &mtip->mti_stats[curcpu]; 38763a7e0a3SRobert Watson mtsp->mts_memfreed += size; 38863a7e0a3SRobert Watson mtsp->mts_numfrees++; 38991dd776cSJohn Birrell 39091dd776cSJohn Birrell #ifdef KDTRACE_HOOKS 39191dd776cSJohn Birrell if (dtrace_malloc_probe != NULL) { 39291dd776cSJohn Birrell uint32_t probe_id = mtip->mti_probes[DTMALLOC_PROBE_FREE]; 39391dd776cSJohn Birrell if (probe_id != 0) 39491dd776cSJohn Birrell (dtrace_malloc_probe)(probe_id, 39591dd776cSJohn Birrell (uintptr_t) mtp, (uintptr_t) mtip, 39691dd776cSJohn Birrell (uintptr_t) mtsp, size, 0); 39791dd776cSJohn Birrell } 39891dd776cSJohn Birrell #endif 39991dd776cSJohn Birrell 40063a7e0a3SRobert Watson critical_exit(); 4014362fadaSBrian Feldman } 4024362fadaSBrian Feldman 4034362fadaSBrian Feldman /* 404f346986bSAlan Cox * contigmalloc: 405f346986bSAlan Cox * 406f346986bSAlan Cox * Allocate a block of physically contiguous memory. 407f346986bSAlan Cox * 408f346986bSAlan Cox * If M_NOWAIT is set, this routine will not block and return NULL if 409f346986bSAlan Cox * the allocation fails. 410f346986bSAlan Cox */ 411f346986bSAlan Cox void * 412f346986bSAlan Cox contigmalloc(unsigned long size, struct malloc_type *type, int flags, 413f346986bSAlan Cox vm_paddr_t low, vm_paddr_t high, unsigned long alignment, 414831ce4cbSJohn Baldwin vm_paddr_t boundary) 415f346986bSAlan Cox { 416f346986bSAlan Cox void *ret; 417f346986bSAlan Cox 4185df87b21SJeff Roberson ret = (void *)kmem_alloc_contig(kernel_arena, size, flags, low, high, 419f346986bSAlan Cox alignment, boundary, VM_MEMATTR_DEFAULT); 420f346986bSAlan Cox if (ret != NULL) 421f346986bSAlan Cox malloc_type_allocated(type, round_page(size)); 422f346986bSAlan Cox return (ret); 423f346986bSAlan Cox } 424f346986bSAlan Cox 425*ab3185d1SJeff Roberson void * 426*ab3185d1SJeff Roberson contigmalloc_domain(unsigned long size, struct malloc_type *type, 427*ab3185d1SJeff Roberson int domain, int flags, vm_paddr_t low, vm_paddr_t high, 428*ab3185d1SJeff Roberson unsigned long alignment, vm_paddr_t boundary) 429*ab3185d1SJeff Roberson { 430*ab3185d1SJeff Roberson void *ret; 431*ab3185d1SJeff Roberson 432*ab3185d1SJeff Roberson ret = (void *)kmem_alloc_contig_domain(domain, size, flags, low, high, 433*ab3185d1SJeff Roberson alignment, boundary, VM_MEMATTR_DEFAULT); 434*ab3185d1SJeff Roberson if (ret != NULL) 435*ab3185d1SJeff Roberson malloc_type_allocated(type, round_page(size)); 436*ab3185d1SJeff Roberson return (ret); 437*ab3185d1SJeff Roberson } 438*ab3185d1SJeff Roberson 439f346986bSAlan Cox /* 440f346986bSAlan Cox * contigfree: 441f346986bSAlan Cox * 442f346986bSAlan Cox * Free a block of memory allocated by contigmalloc. 443f346986bSAlan Cox * 444f346986bSAlan Cox * This routine may not block. 445f346986bSAlan Cox */ 446f346986bSAlan Cox void 447f346986bSAlan Cox contigfree(void *addr, unsigned long size, struct malloc_type *type) 448f346986bSAlan Cox { 449f346986bSAlan Cox 4505df87b21SJeff Roberson kmem_free(kernel_arena, (vm_offset_t)addr, size); 451f346986bSAlan Cox malloc_type_freed(type, round_page(size)); 452f346986bSAlan Cox } 453f346986bSAlan Cox 454*ab3185d1SJeff Roberson #ifdef MALLOC_DEBUG 455*ab3185d1SJeff Roberson static int 456*ab3185d1SJeff Roberson malloc_dbg(caddr_t *vap, unsigned long *sizep, struct malloc_type *mtp, 457*ab3185d1SJeff Roberson int flags) 458df8bae1dSRodney W. Grimes { 459194a0abfSPoul-Henning Kamp #ifdef INVARIANTS 460*ab3185d1SJeff Roberson int indx; 461*ab3185d1SJeff Roberson 462bb1c7df8SRobert Watson KASSERT(mtp->ks_magic == M_MAGIC, ("malloc: bad malloc type magic")); 463d3c11994SPoul-Henning Kamp /* 46423198357SRuslan Ermilov * Check that exactly one of M_WAITOK or M_NOWAIT is specified. 465d3c11994SPoul-Henning Kamp */ 46623198357SRuslan Ermilov indx = flags & (M_WAITOK | M_NOWAIT); 467d3c11994SPoul-Henning Kamp if (indx != M_NOWAIT && indx != M_WAITOK) { 468d3c11994SPoul-Henning Kamp static struct timeval lasterr; 469d3c11994SPoul-Henning Kamp static int curerr, once; 470d3c11994SPoul-Henning Kamp if (once == 0 && ppsratecheck(&lasterr, &curerr, 1)) { 471d3c11994SPoul-Henning Kamp printf("Bad malloc flags: %x\n", indx); 4722d50560aSMarcel Moolenaar kdb_backtrace(); 473d3c11994SPoul-Henning Kamp flags |= M_WAITOK; 474d3c11994SPoul-Henning Kamp once++; 475d3c11994SPoul-Henning Kamp } 476d3c11994SPoul-Henning Kamp } 477194a0abfSPoul-Henning Kamp #endif 478eae870cdSRobert Watson #ifdef MALLOC_MAKE_FAILURES 479eae870cdSRobert Watson if ((flags & M_NOWAIT) && (malloc_failure_rate != 0)) { 480eae870cdSRobert Watson atomic_add_int(&malloc_nowait_count, 1); 481eae870cdSRobert Watson if ((malloc_nowait_count % malloc_failure_rate) == 0) { 482eae870cdSRobert Watson atomic_add_int(&malloc_failure_count, 1); 4833f6ee876SPoul-Henning Kamp t_malloc_fail = time_uptime; 484*ab3185d1SJeff Roberson *vap = NULL; 485*ab3185d1SJeff Roberson return (EJUSTRETURN); 486eae870cdSRobert Watson } 487eae870cdSRobert Watson } 488eae870cdSRobert Watson #endif 489d3c11994SPoul-Henning Kamp if (flags & M_WAITOK) 490b40ce416SJulian Elischer KASSERT(curthread->td_intr_nesting_level == 0, 491a163d034SWarner Losh ("malloc(M_WAITOK) in interrupt context")); 492d9e2e68dSMark Johnston KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 4931067a2baSJonathan T. Looney ("malloc: called with spinlock or critical section held")); 4941067a2baSJonathan T. Looney 495e4eb384bSBosko Milekic #ifdef DEBUG_MEMGUARD 496*ab3185d1SJeff Roberson if (memguard_cmp_mtp(mtp, *sizep)) { 497*ab3185d1SJeff Roberson *vap = memguard_alloc(*sizep, flags); 498*ab3185d1SJeff Roberson if (*vap != NULL) 499*ab3185d1SJeff Roberson return (EJUSTRETURN); 500e3813573SMatthew D Fleming /* This is unfortunate but should not be fatal. */ 501e3813573SMatthew D Fleming } 502e4eb384bSBosko Milekic #endif 503e4eb384bSBosko Milekic 504847a2a17SPawel Jakub Dawidek #ifdef DEBUG_REDZONE 505*ab3185d1SJeff Roberson *sizep = redzone_size_ntor(*sizep); 506*ab3185d1SJeff Roberson #endif 507*ab3185d1SJeff Roberson 508*ab3185d1SJeff Roberson return (0); 509*ab3185d1SJeff Roberson } 510*ab3185d1SJeff Roberson #endif 511*ab3185d1SJeff Roberson 512*ab3185d1SJeff Roberson /* 513*ab3185d1SJeff Roberson * malloc: 514*ab3185d1SJeff Roberson * 515*ab3185d1SJeff Roberson * Allocate a block of memory. 516*ab3185d1SJeff Roberson * 517*ab3185d1SJeff Roberson * If M_NOWAIT is set, this routine will not block and return NULL if 518*ab3185d1SJeff Roberson * the allocation fails. 519*ab3185d1SJeff Roberson */ 520*ab3185d1SJeff Roberson void * 521*ab3185d1SJeff Roberson malloc(unsigned long size, struct malloc_type *mtp, int flags) 522*ab3185d1SJeff Roberson { 523*ab3185d1SJeff Roberson int indx; 524*ab3185d1SJeff Roberson struct malloc_type_internal *mtip; 525*ab3185d1SJeff Roberson caddr_t va; 526*ab3185d1SJeff Roberson uma_zone_t zone; 527*ab3185d1SJeff Roberson #if defined(DEBUG_REDZONE) 528*ab3185d1SJeff Roberson unsigned long osize = size; 529*ab3185d1SJeff Roberson #endif 530*ab3185d1SJeff Roberson 531*ab3185d1SJeff Roberson #ifdef MALLOC_DEBUG 532*ab3185d1SJeff Roberson if (malloc_dbg(&va, &size, mtp, flags) != 0) 533*ab3185d1SJeff Roberson return (va); 534847a2a17SPawel Jakub Dawidek #endif 535847a2a17SPawel Jakub Dawidek 5367001d850SXin LI if (size <= kmem_zmax) { 537d7854da1SMatthew D Fleming mtip = mtp->ks_handle; 5386f267175SJeff Roberson if (size & KMEM_ZMASK) 5396f267175SJeff Roberson size = (size & ~KMEM_ZMASK) + KMEM_ZBASE; 5406f267175SJeff Roberson indx = kmemsize[size >> KMEM_ZSHIFT]; 541d7854da1SMatthew D Fleming KASSERT(mtip->mti_zone < numzones, 542d7854da1SMatthew D Fleming ("mti_zone %u out of range %d", 543d7854da1SMatthew D Fleming mtip->mti_zone, numzones)); 544d7854da1SMatthew D Fleming zone = kmemzones[indx].kz_zone[mtip->mti_zone]; 5456f267175SJeff Roberson #ifdef MALLOC_PROFILE 5466f267175SJeff Roberson krequests[size >> KMEM_ZSHIFT]++; 5476f267175SJeff Roberson #endif 5488355f576SJeff Roberson va = uma_zalloc(zone, flags); 5494362fadaSBrian Feldman if (va != NULL) 550e20a199fSJeff Roberson size = zone->uz_size; 55163a7e0a3SRobert Watson malloc_type_zone_allocated(mtp, va == NULL ? 0 : size, indx); 5528355f576SJeff Roberson } else { 5536f267175SJeff Roberson size = roundup(size, PAGE_SIZE); 5548355f576SJeff Roberson zone = NULL; 5558355f576SJeff Roberson va = uma_large_malloc(size, flags); 55663a7e0a3SRobert Watson malloc_type_allocated(mtp, va == NULL ? 0 : size); 557df8bae1dSRodney W. Grimes } 5581282e9acSPoul-Henning Kamp if (flags & M_WAITOK) 559a163d034SWarner Losh KASSERT(va != NULL, ("malloc(M_WAITOK) returned NULL")); 5601282e9acSPoul-Henning Kamp else if (va == NULL) 5611fb14a47SPoul-Henning Kamp t_malloc_fail = time_uptime; 562*ab3185d1SJeff Roberson #ifdef DEBUG_REDZONE 563*ab3185d1SJeff Roberson if (va != NULL) 564*ab3185d1SJeff Roberson va = redzone_setup(va, osize); 5654db4f5c8SPoul-Henning Kamp #endif 566*ab3185d1SJeff Roberson return ((void *) va); 567*ab3185d1SJeff Roberson } 568*ab3185d1SJeff Roberson 569*ab3185d1SJeff Roberson void * 570*ab3185d1SJeff Roberson malloc_domain(unsigned long size, struct malloc_type *mtp, int domain, 571*ab3185d1SJeff Roberson int flags) 572*ab3185d1SJeff Roberson { 573*ab3185d1SJeff Roberson int indx; 574*ab3185d1SJeff Roberson struct malloc_type_internal *mtip; 575*ab3185d1SJeff Roberson caddr_t va; 576*ab3185d1SJeff Roberson uma_zone_t zone; 577*ab3185d1SJeff Roberson #if defined(DEBUG_REDZONE) 578*ab3185d1SJeff Roberson unsigned long osize = size; 579*ab3185d1SJeff Roberson #endif 580*ab3185d1SJeff Roberson 581*ab3185d1SJeff Roberson #ifdef MALLOC_DEBUG 582*ab3185d1SJeff Roberson if (malloc_dbg(&va, &size, mtp, flags) != 0) 583*ab3185d1SJeff Roberson return (va); 584*ab3185d1SJeff Roberson #endif 585*ab3185d1SJeff Roberson if (size <= kmem_zmax) { 586*ab3185d1SJeff Roberson mtip = mtp->ks_handle; 587*ab3185d1SJeff Roberson if (size & KMEM_ZMASK) 588*ab3185d1SJeff Roberson size = (size & ~KMEM_ZMASK) + KMEM_ZBASE; 589*ab3185d1SJeff Roberson indx = kmemsize[size >> KMEM_ZSHIFT]; 590*ab3185d1SJeff Roberson KASSERT(mtip->mti_zone < numzones, 591*ab3185d1SJeff Roberson ("mti_zone %u out of range %d", 592*ab3185d1SJeff Roberson mtip->mti_zone, numzones)); 593*ab3185d1SJeff Roberson zone = kmemzones[indx].kz_zone[mtip->mti_zone]; 594*ab3185d1SJeff Roberson #ifdef MALLOC_PROFILE 595*ab3185d1SJeff Roberson krequests[size >> KMEM_ZSHIFT]++; 596*ab3185d1SJeff Roberson #endif 597*ab3185d1SJeff Roberson va = uma_zalloc_domain(zone, NULL, domain, flags); 598*ab3185d1SJeff Roberson if (va != NULL) 599*ab3185d1SJeff Roberson size = zone->uz_size; 600*ab3185d1SJeff Roberson malloc_type_zone_allocated(mtp, va == NULL ? 0 : size, indx); 601*ab3185d1SJeff Roberson } else { 602*ab3185d1SJeff Roberson size = roundup(size, PAGE_SIZE); 603*ab3185d1SJeff Roberson zone = NULL; 604*ab3185d1SJeff Roberson va = uma_large_malloc_domain(size, domain, flags); 605*ab3185d1SJeff Roberson malloc_type_allocated(mtp, va == NULL ? 0 : size); 606*ab3185d1SJeff Roberson } 607*ab3185d1SJeff Roberson if (flags & M_WAITOK) 608*ab3185d1SJeff Roberson KASSERT(va != NULL, ("malloc(M_WAITOK) returned NULL")); 609*ab3185d1SJeff Roberson else if (va == NULL) 610*ab3185d1SJeff Roberson t_malloc_fail = time_uptime; 611847a2a17SPawel Jakub Dawidek #ifdef DEBUG_REDZONE 612847a2a17SPawel Jakub Dawidek if (va != NULL) 613847a2a17SPawel Jakub Dawidek va = redzone_setup(va, osize); 614847a2a17SPawel Jakub Dawidek #endif 615df8bae1dSRodney W. Grimes return ((void *) va); 616df8bae1dSRodney W. Grimes } 617df8bae1dSRodney W. Grimes 618fd91e076SKristof Provost void * 619fd91e076SKristof Provost mallocarray(size_t nmemb, size_t size, struct malloc_type *type, int flags) 620fd91e076SKristof Provost { 621fd91e076SKristof Provost 622c02fc960SConrad Meyer if (WOULD_OVERFLOW(nmemb, size)) 623c02fc960SConrad Meyer panic("mallocarray: %zu * %zu overflowed", nmemb, size); 624fd91e076SKristof Provost 625fd91e076SKristof Provost return (malloc(size * nmemb, type, flags)); 626fd91e076SKristof Provost } 627fd91e076SKristof Provost 628*ab3185d1SJeff Roberson #ifdef INVARIANTS 629*ab3185d1SJeff Roberson static void 630*ab3185d1SJeff Roberson free_save_type(void *addr, struct malloc_type *mtp, u_long size) 631*ab3185d1SJeff Roberson { 632*ab3185d1SJeff Roberson struct malloc_type **mtpp = addr; 633*ab3185d1SJeff Roberson 634*ab3185d1SJeff Roberson /* 635*ab3185d1SJeff Roberson * Cache a pointer to the malloc_type that most recently freed 636*ab3185d1SJeff Roberson * this memory here. This way we know who is most likely to 637*ab3185d1SJeff Roberson * have stepped on it later. 638*ab3185d1SJeff Roberson * 639*ab3185d1SJeff Roberson * This code assumes that size is a multiple of 8 bytes for 640*ab3185d1SJeff Roberson * 64 bit machines 641*ab3185d1SJeff Roberson */ 642*ab3185d1SJeff Roberson mtpp = (struct malloc_type **) ((unsigned long)mtpp & ~UMA_ALIGN_PTR); 643*ab3185d1SJeff Roberson mtpp += (size - sizeof(struct malloc_type *)) / 644*ab3185d1SJeff Roberson sizeof(struct malloc_type *); 645*ab3185d1SJeff Roberson *mtpp = mtp; 646*ab3185d1SJeff Roberson } 647*ab3185d1SJeff Roberson #endif 648*ab3185d1SJeff Roberson 649*ab3185d1SJeff Roberson #ifdef MALLOC_DEBUG 650*ab3185d1SJeff Roberson static int 651*ab3185d1SJeff Roberson free_dbg(void **addrp, struct malloc_type *mtp) 652*ab3185d1SJeff Roberson { 653*ab3185d1SJeff Roberson void *addr; 654*ab3185d1SJeff Roberson 655*ab3185d1SJeff Roberson addr = *addrp; 656*ab3185d1SJeff Roberson KASSERT(mtp->ks_magic == M_MAGIC, ("free: bad malloc type magic")); 657*ab3185d1SJeff Roberson KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 658*ab3185d1SJeff Roberson ("free: called with spinlock or critical section held")); 659*ab3185d1SJeff Roberson 660*ab3185d1SJeff Roberson /* free(NULL, ...) does nothing */ 661*ab3185d1SJeff Roberson if (addr == NULL) 662*ab3185d1SJeff Roberson return (EJUSTRETURN); 663*ab3185d1SJeff Roberson 664*ab3185d1SJeff Roberson #ifdef DEBUG_MEMGUARD 665*ab3185d1SJeff Roberson if (is_memguard_addr(addr)) { 666*ab3185d1SJeff Roberson memguard_free(addr); 667*ab3185d1SJeff Roberson return (EJUSTRETURN); 668*ab3185d1SJeff Roberson } 669*ab3185d1SJeff Roberson #endif 670*ab3185d1SJeff Roberson 671*ab3185d1SJeff Roberson #ifdef DEBUG_REDZONE 672*ab3185d1SJeff Roberson redzone_check(addr); 673*ab3185d1SJeff Roberson *addrp = redzone_addr_ntor(addr); 674*ab3185d1SJeff Roberson #endif 675*ab3185d1SJeff Roberson 676*ab3185d1SJeff Roberson return (0); 677*ab3185d1SJeff Roberson } 678*ab3185d1SJeff Roberson #endif 679*ab3185d1SJeff Roberson 680fd91e076SKristof Provost /* 6811c7c3c6aSMatthew Dillon * free: 6821c7c3c6aSMatthew Dillon * 683df8bae1dSRodney W. Grimes * Free a block of memory allocated by malloc. 6841c7c3c6aSMatthew Dillon * 6851c7c3c6aSMatthew Dillon * This routine may not block. 686df8bae1dSRodney W. Grimes */ 687df8bae1dSRodney W. Grimes void 68863a7e0a3SRobert Watson free(void *addr, struct malloc_type *mtp) 689df8bae1dSRodney W. Grimes { 69099571dc3SJeff Roberson uma_slab_t slab; 69199571dc3SJeff Roberson u_long size; 692254c6cb3SPoul-Henning Kamp 693*ab3185d1SJeff Roberson #ifdef MALLOC_DEBUG 694*ab3185d1SJeff Roberson if (free_dbg(&addr, mtp) != 0) 695*ab3185d1SJeff Roberson return; 696*ab3185d1SJeff Roberson #endif 69744a8ff31SArchie Cobbs /* free(NULL, ...) does nothing */ 69844a8ff31SArchie Cobbs if (addr == NULL) 69944a8ff31SArchie Cobbs return; 70044a8ff31SArchie Cobbs 70199571dc3SJeff Roberson slab = vtoslab((vm_offset_t)addr & (~UMA_SLAB_MASK)); 7028355f576SJeff Roberson if (slab == NULL) 7036f267175SJeff Roberson panic("free: address %p(%p) has not been allocated.\n", 70499571dc3SJeff Roberson addr, (void *)((u_long)addr & (~UMA_SLAB_MASK))); 70599571dc3SJeff Roberson 7068355f576SJeff Roberson if (!(slab->us_flags & UMA_SLAB_MALLOC)) { 707099a0e58SBosko Milekic size = slab->us_keg->uk_size; 7088f70816cSJeff Roberson #ifdef INVARIANTS 709*ab3185d1SJeff Roberson free_save_type(addr, mtp, size); 7108f70816cSJeff Roberson #endif 711099a0e58SBosko Milekic uma_zfree_arg(LIST_FIRST(&slab->us_keg->uk_zones), addr, slab); 71214bf02f8SJohn Dyson } else { 7138355f576SJeff Roberson size = slab->us_size; 7148355f576SJeff Roberson uma_large_free(slab); 71514bf02f8SJohn Dyson } 71663a7e0a3SRobert Watson malloc_type_freed(mtp, size); 717df8bae1dSRodney W. Grimes } 718df8bae1dSRodney W. Grimes 719*ab3185d1SJeff Roberson void 720*ab3185d1SJeff Roberson free_domain(void *addr, struct malloc_type *mtp) 721*ab3185d1SJeff Roberson { 722*ab3185d1SJeff Roberson uma_slab_t slab; 723*ab3185d1SJeff Roberson u_long size; 724*ab3185d1SJeff Roberson 725*ab3185d1SJeff Roberson #ifdef MALLOC_DEBUG 726*ab3185d1SJeff Roberson if (free_dbg(&addr, mtp) != 0) 727*ab3185d1SJeff Roberson return; 728*ab3185d1SJeff Roberson #endif 729*ab3185d1SJeff Roberson 730*ab3185d1SJeff Roberson /* free(NULL, ...) does nothing */ 731*ab3185d1SJeff Roberson if (addr == NULL) 732*ab3185d1SJeff Roberson return; 733*ab3185d1SJeff Roberson 734*ab3185d1SJeff Roberson slab = vtoslab((vm_offset_t)addr & (~UMA_SLAB_MASK)); 735*ab3185d1SJeff Roberson if (slab == NULL) 736*ab3185d1SJeff Roberson panic("free_domain: address %p(%p) has not been allocated.\n", 737*ab3185d1SJeff Roberson addr, (void *)((u_long)addr & (~UMA_SLAB_MASK))); 738*ab3185d1SJeff Roberson 739*ab3185d1SJeff Roberson if (!(slab->us_flags & UMA_SLAB_MALLOC)) { 740*ab3185d1SJeff Roberson size = slab->us_keg->uk_size; 741*ab3185d1SJeff Roberson #ifdef INVARIANTS 742*ab3185d1SJeff Roberson free_save_type(addr, mtp, size); 743*ab3185d1SJeff Roberson #endif 744*ab3185d1SJeff Roberson uma_zfree_domain(LIST_FIRST(&slab->us_keg->uk_zones), 745*ab3185d1SJeff Roberson addr, slab); 746*ab3185d1SJeff Roberson } else { 747*ab3185d1SJeff Roberson size = slab->us_size; 748*ab3185d1SJeff Roberson uma_large_free(slab); 749*ab3185d1SJeff Roberson } 750*ab3185d1SJeff Roberson malloc_type_freed(mtp, size); 751*ab3185d1SJeff Roberson } 752*ab3185d1SJeff Roberson 753df8bae1dSRodney W. Grimes /* 75444a8ff31SArchie Cobbs * realloc: change the size of a memory block 75544a8ff31SArchie Cobbs */ 75644a8ff31SArchie Cobbs void * 75763a7e0a3SRobert Watson realloc(void *addr, unsigned long size, struct malloc_type *mtp, int flags) 75844a8ff31SArchie Cobbs { 7598355f576SJeff Roberson uma_slab_t slab; 76044a8ff31SArchie Cobbs unsigned long alloc; 76144a8ff31SArchie Cobbs void *newaddr; 76244a8ff31SArchie Cobbs 763bb1c7df8SRobert Watson KASSERT(mtp->ks_magic == M_MAGIC, 764bb1c7df8SRobert Watson ("realloc: bad malloc type magic")); 765d9e2e68dSMark Johnston KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 7661067a2baSJonathan T. Looney ("realloc: called with spinlock or critical section held")); 7671067a2baSJonathan T. Looney 76844a8ff31SArchie Cobbs /* realloc(NULL, ...) is equivalent to malloc(...) */ 76944a8ff31SArchie Cobbs if (addr == NULL) 77063a7e0a3SRobert Watson return (malloc(size, mtp, flags)); 77163a7e0a3SRobert Watson 77263a7e0a3SRobert Watson /* 77363a7e0a3SRobert Watson * XXX: Should report free of old memory and alloc of new memory to 77463a7e0a3SRobert Watson * per-CPU stats. 77563a7e0a3SRobert Watson */ 77644a8ff31SArchie Cobbs 777e4eb384bSBosko Milekic #ifdef DEBUG_MEMGUARD 7786d3ed393SMatthew D Fleming if (is_memguard_addr(addr)) 7796d3ed393SMatthew D Fleming return (memguard_realloc(addr, size, mtp, flags)); 780e4eb384bSBosko Milekic #endif 781e4eb384bSBosko Milekic 782847a2a17SPawel Jakub Dawidek #ifdef DEBUG_REDZONE 783847a2a17SPawel Jakub Dawidek slab = NULL; 784847a2a17SPawel Jakub Dawidek alloc = redzone_get_size(addr); 785847a2a17SPawel Jakub Dawidek #else 78699571dc3SJeff Roberson slab = vtoslab((vm_offset_t)addr & ~(UMA_SLAB_MASK)); 7878355f576SJeff Roberson 78844a8ff31SArchie Cobbs /* Sanity check */ 7898355f576SJeff Roberson KASSERT(slab != NULL, 79044a8ff31SArchie Cobbs ("realloc: address %p out of range", (void *)addr)); 79144a8ff31SArchie Cobbs 79244a8ff31SArchie Cobbs /* Get the size of the original block */ 793619f2841SPawel Jakub Dawidek if (!(slab->us_flags & UMA_SLAB_MALLOC)) 794099a0e58SBosko Milekic alloc = slab->us_keg->uk_size; 7958355f576SJeff Roberson else 7968355f576SJeff Roberson alloc = slab->us_size; 79744a8ff31SArchie Cobbs 79844a8ff31SArchie Cobbs /* Reuse the original block if appropriate */ 79944a8ff31SArchie Cobbs if (size <= alloc 80044a8ff31SArchie Cobbs && (size > (alloc >> REALLOC_FRACTION) || alloc == MINALLOCSIZE)) 80144a8ff31SArchie Cobbs return (addr); 802847a2a17SPawel Jakub Dawidek #endif /* !DEBUG_REDZONE */ 80344a8ff31SArchie Cobbs 80444a8ff31SArchie Cobbs /* Allocate a new, bigger (or smaller) block */ 80563a7e0a3SRobert Watson if ((newaddr = malloc(size, mtp, flags)) == NULL) 80644a8ff31SArchie Cobbs return (NULL); 80744a8ff31SArchie Cobbs 80844a8ff31SArchie Cobbs /* Copy over original contents */ 80944a8ff31SArchie Cobbs bcopy(addr, newaddr, min(size, alloc)); 81063a7e0a3SRobert Watson free(addr, mtp); 81144a8ff31SArchie Cobbs return (newaddr); 81244a8ff31SArchie Cobbs } 81344a8ff31SArchie Cobbs 81444a8ff31SArchie Cobbs /* 81544a8ff31SArchie Cobbs * reallocf: same as realloc() but free memory on failure. 81644a8ff31SArchie Cobbs */ 81744a8ff31SArchie Cobbs void * 81863a7e0a3SRobert Watson reallocf(void *addr, unsigned long size, struct malloc_type *mtp, int flags) 81944a8ff31SArchie Cobbs { 82044a8ff31SArchie Cobbs void *mem; 82144a8ff31SArchie Cobbs 82263a7e0a3SRobert Watson if ((mem = realloc(addr, size, mtp, flags)) == NULL) 82363a7e0a3SRobert Watson free(addr, mtp); 82444a8ff31SArchie Cobbs return (mem); 82544a8ff31SArchie Cobbs } 82644a8ff31SArchie Cobbs 827f9d498adSDimitry Andric #ifndef __sparc64__ 828c70af487SAlan Cox CTASSERT(VM_KMEM_SIZE_SCALE >= 1); 829f9d498adSDimitry Andric #endif 830c70af487SAlan Cox 8315df87b21SJeff Roberson /* 832c70af487SAlan Cox * Initialize the kernel memory (kmem) arena. 8335df87b21SJeff Roberson */ 8345df87b21SJeff Roberson void 8355df87b21SJeff Roberson kmeminit(void) 8365df87b21SJeff Roberson { 837af3b2549SHans Petter Selasky u_long mem_size; 838af3b2549SHans Petter Selasky u_long tmp; 83969ef67f9SJason Evans 840af3b2549SHans Petter Selasky #ifdef VM_KMEM_SIZE 841af3b2549SHans Petter Selasky if (vm_kmem_size == 0) 842af3b2549SHans Petter Selasky vm_kmem_size = VM_KMEM_SIZE; 843af3b2549SHans Petter Selasky #endif 844af3b2549SHans Petter Selasky #ifdef VM_KMEM_SIZE_MIN 845af3b2549SHans Petter Selasky if (vm_kmem_size_min == 0) 846af3b2549SHans Petter Selasky vm_kmem_size_min = VM_KMEM_SIZE_MIN; 847af3b2549SHans Petter Selasky #endif 848af3b2549SHans Petter Selasky #ifdef VM_KMEM_SIZE_MAX 849af3b2549SHans Petter Selasky if (vm_kmem_size_max == 0) 850af3b2549SHans Petter Selasky vm_kmem_size_max = VM_KMEM_SIZE_MAX; 851af3b2549SHans Petter Selasky #endif 8528a58a9f6SJohn Dyson /* 853c70af487SAlan Cox * Calculate the amount of kernel virtual address (KVA) space that is 854c70af487SAlan Cox * preallocated to the kmem arena. In order to support a wide range 855c70af487SAlan Cox * of machines, it is a function of the physical memory size, 856c70af487SAlan Cox * specifically, 8578a58a9f6SJohn Dyson * 858c70af487SAlan Cox * min(max(physical memory size / VM_KMEM_SIZE_SCALE, 859c70af487SAlan Cox * VM_KMEM_SIZE_MIN), VM_KMEM_SIZE_MAX) 860c70af487SAlan Cox * 861c70af487SAlan Cox * Every architecture must define an integral value for 862c70af487SAlan Cox * VM_KMEM_SIZE_SCALE. However, the definitions of VM_KMEM_SIZE_MIN 863c70af487SAlan Cox * and VM_KMEM_SIZE_MAX, which represent respectively the floor and 864c70af487SAlan Cox * ceiling on this preallocation, are optional. Typically, 865c70af487SAlan Cox * VM_KMEM_SIZE_MAX is itself a function of the available KVA space on 866c70af487SAlan Cox * a given architecture. 8678a58a9f6SJohn Dyson */ 86844f1c916SBryan Drewery mem_size = vm_cnt.v_page_count; 8697c51714eSSean Bruno if (mem_size <= 32768) /* delphij XXX 128MB */ 8707c51714eSSean Bruno kmem_zmax = PAGE_SIZE; 8718a58a9f6SJohn Dyson 872c70af487SAlan Cox if (vm_kmem_size_scale < 1) 873c70af487SAlan Cox vm_kmem_size_scale = VM_KMEM_SIZE_SCALE; 874c70af487SAlan Cox 875af3b2549SHans Petter Selasky /* 876af3b2549SHans Petter Selasky * Check if we should use defaults for the "vm_kmem_size" 877af3b2549SHans Petter Selasky * variable: 878af3b2549SHans Petter Selasky */ 879af3b2549SHans Petter Selasky if (vm_kmem_size == 0) { 880479439b4SDag-Erling Smørgrav vm_kmem_size = (mem_size / vm_kmem_size_scale) * PAGE_SIZE; 8818a58a9f6SJohn Dyson 882c70af487SAlan Cox if (vm_kmem_size_min > 0 && vm_kmem_size < vm_kmem_size_min) 8830e5179e4SStephane E. Potvin vm_kmem_size = vm_kmem_size_min; 884479439b4SDag-Erling Smørgrav if (vm_kmem_size_max > 0 && vm_kmem_size >= vm_kmem_size_max) 885479439b4SDag-Erling Smørgrav vm_kmem_size = vm_kmem_size_max; 886af3b2549SHans Petter Selasky } 8878a58a9f6SJohn Dyson 88827b8623fSDavid Greenman /* 889af3b2549SHans Petter Selasky * The amount of KVA space that is preallocated to the 890c70af487SAlan Cox * kmem arena can be set statically at compile-time or manually 891c70af487SAlan Cox * through the kernel environment. However, it is still limited to 892c70af487SAlan Cox * twice the physical memory size, which has been sufficient to handle 893c70af487SAlan Cox * the most severe cases of external fragmentation in the kmem arena. 89427b8623fSDavid Greenman */ 895c749c003SAlan Cox if (vm_kmem_size / 2 / PAGE_SIZE > mem_size) 896c749c003SAlan Cox vm_kmem_size = 2 * mem_size * PAGE_SIZE; 8978a58a9f6SJohn Dyson 898e137643eSOlivier Houchard vm_kmem_size = round_page(vm_kmem_size); 899e3813573SMatthew D Fleming #ifdef DEBUG_MEMGUARD 900f806cdcfSMatthew D Fleming tmp = memguard_fudge(vm_kmem_size, kernel_map); 901e3813573SMatthew D Fleming #else 902e3813573SMatthew D Fleming tmp = vm_kmem_size; 903e3813573SMatthew D Fleming #endif 9042e47807cSJeff Roberson uma_set_limit(tmp); 9058355f576SJeff Roberson 906e4eb384bSBosko Milekic #ifdef DEBUG_MEMGUARD 907e4eb384bSBosko Milekic /* 908e4eb384bSBosko Milekic * Initialize MemGuard if support compiled in. MemGuard is a 909e4eb384bSBosko Milekic * replacement allocator used for detecting tamper-after-free 910e4eb384bSBosko Milekic * scenarios as they occur. It is only used for debugging. 911e4eb384bSBosko Milekic */ 9122e47807cSJeff Roberson memguard_init(kernel_arena); 913e4eb384bSBosko Milekic #endif 9145df87b21SJeff Roberson } 9155df87b21SJeff Roberson 9165df87b21SJeff Roberson /* 9175df87b21SJeff Roberson * Initialize the kernel memory allocator 9185df87b21SJeff Roberson */ 9195df87b21SJeff Roberson /* ARGSUSED*/ 9205df87b21SJeff Roberson static void 9215df87b21SJeff Roberson mallocinit(void *dummy) 9225df87b21SJeff Roberson { 9235df87b21SJeff Roberson int i; 9245df87b21SJeff Roberson uint8_t indx; 9255df87b21SJeff Roberson 9265df87b21SJeff Roberson mtx_init(&malloc_mtx, "malloc", NULL, MTX_DEF); 9275df87b21SJeff Roberson 9285df87b21SJeff Roberson kmeminit(); 929e4eb384bSBosko Milekic 93099571dc3SJeff Roberson uma_startup2(); 9318355f576SJeff Roberson 9327001d850SXin LI if (kmem_zmax < PAGE_SIZE || kmem_zmax > KMEM_ZMAX) 9337001d850SXin LI kmem_zmax = KMEM_ZMAX; 9347001d850SXin LI 93563a7e0a3SRobert Watson mt_zone = uma_zcreate("mt_zone", sizeof(struct malloc_type_internal), 93663a7e0a3SRobert Watson #ifdef INVARIANTS 93763a7e0a3SRobert Watson mtrash_ctor, mtrash_dtor, mtrash_init, mtrash_fini, 93863a7e0a3SRobert Watson #else 93963a7e0a3SRobert Watson NULL, NULL, NULL, NULL, 94063a7e0a3SRobert Watson #endif 94163a7e0a3SRobert Watson UMA_ALIGN_PTR, UMA_ZONE_MALLOC); 9426f267175SJeff Roberson for (i = 0, indx = 0; kmemzones[indx].kz_size != 0; indx++) { 9436f267175SJeff Roberson int size = kmemzones[indx].kz_size; 9446f267175SJeff Roberson char *name = kmemzones[indx].kz_name; 945d7854da1SMatthew D Fleming int subzone; 9468355f576SJeff Roberson 947d7854da1SMatthew D Fleming for (subzone = 0; subzone < numzones; subzone++) { 948d7854da1SMatthew D Fleming kmemzones[indx].kz_zone[subzone] = 949d7854da1SMatthew D Fleming uma_zcreate(name, size, 9508efc4effSJeff Roberson #ifdef INVARIANTS 9518f70816cSJeff Roberson mtrash_ctor, mtrash_dtor, mtrash_init, mtrash_fini, 9528efc4effSJeff Roberson #else 9538efc4effSJeff Roberson NULL, NULL, NULL, NULL, 9548efc4effSJeff Roberson #endif 9558efc4effSJeff Roberson UMA_ALIGN_PTR, UMA_ZONE_MALLOC); 956d7854da1SMatthew D Fleming } 9578355f576SJeff Roberson for (;i <= size; i+= KMEM_ZBASE) 9586f267175SJeff Roberson kmemsize[i >> KMEM_ZSHIFT] = indx; 9598355f576SJeff Roberson 960df8bae1dSRodney W. Grimes } 961254c6cb3SPoul-Henning Kamp } 962af3b2549SHans Petter Selasky SYSINIT(kmem, SI_SUB_KMEM, SI_ORDER_SECOND, mallocinit, NULL); 963254c6cb3SPoul-Henning Kamp 964db669378SPeter Wemm void 96587efd4d5SRobert Watson malloc_init(void *data) 966254c6cb3SPoul-Henning Kamp { 96763a7e0a3SRobert Watson struct malloc_type_internal *mtip; 96863a7e0a3SRobert Watson struct malloc_type *mtp; 96963a7e0a3SRobert Watson 97044f1c916SBryan Drewery KASSERT(vm_cnt.v_page_count != 0, ("malloc_register before vm_init")); 97163a7e0a3SRobert Watson 97263a7e0a3SRobert Watson mtp = data; 973f121baaaSBrian Somers if (mtp->ks_magic != M_MAGIC) 974f121baaaSBrian Somers panic("malloc_init: bad malloc type magic"); 975bb1c7df8SRobert Watson 97663a7e0a3SRobert Watson mtip = uma_zalloc(mt_zone, M_WAITOK | M_ZERO); 97763a7e0a3SRobert Watson mtp->ks_handle = mtip; 978d7854da1SMatthew D Fleming mtip->mti_zone = mtp_get_subzone(mtp->ks_shortdesc); 979254c6cb3SPoul-Henning Kamp 9806f267175SJeff Roberson mtx_lock(&malloc_mtx); 98163a7e0a3SRobert Watson mtp->ks_next = kmemstatistics; 98263a7e0a3SRobert Watson kmemstatistics = mtp; 983cd814b26SRobert Watson kmemcount++; 9846f267175SJeff Roberson mtx_unlock(&malloc_mtx); 985df8bae1dSRodney W. Grimes } 986db669378SPeter Wemm 987db669378SPeter Wemm void 98887efd4d5SRobert Watson malloc_uninit(void *data) 989db669378SPeter Wemm { 99063a7e0a3SRobert Watson struct malloc_type_internal *mtip; 9912a143d5bSPawel Jakub Dawidek struct malloc_type_stats *mtsp; 99263a7e0a3SRobert Watson struct malloc_type *mtp, *temp; 99345d48bdaSPaul Saab uma_slab_t slab; 9942a143d5bSPawel Jakub Dawidek long temp_allocs, temp_bytes; 9952a143d5bSPawel Jakub Dawidek int i; 996db669378SPeter Wemm 99763a7e0a3SRobert Watson mtp = data; 998bb1c7df8SRobert Watson KASSERT(mtp->ks_magic == M_MAGIC, 999bb1c7df8SRobert Watson ("malloc_uninit: bad malloc type magic")); 100063a7e0a3SRobert Watson KASSERT(mtp->ks_handle != NULL, ("malloc_deregister: cookie NULL")); 1001bb1c7df8SRobert Watson 10026f267175SJeff Roberson mtx_lock(&malloc_mtx); 100363a7e0a3SRobert Watson mtip = mtp->ks_handle; 100463a7e0a3SRobert Watson mtp->ks_handle = NULL; 100563a7e0a3SRobert Watson if (mtp != kmemstatistics) { 100663a7e0a3SRobert Watson for (temp = kmemstatistics; temp != NULL; 100763a7e0a3SRobert Watson temp = temp->ks_next) { 1008f121baaaSBrian Somers if (temp->ks_next == mtp) { 100963a7e0a3SRobert Watson temp->ks_next = mtp->ks_next; 1010f121baaaSBrian Somers break; 1011db669378SPeter Wemm } 1012f121baaaSBrian Somers } 1013f121baaaSBrian Somers KASSERT(temp, 1014f121baaaSBrian Somers ("malloc_uninit: type '%s' not found", mtp->ks_shortdesc)); 101563a7e0a3SRobert Watson } else 101663a7e0a3SRobert Watson kmemstatistics = mtp->ks_next; 1017cd814b26SRobert Watson kmemcount--; 10186f267175SJeff Roberson mtx_unlock(&malloc_mtx); 10192a143d5bSPawel Jakub Dawidek 10202a143d5bSPawel Jakub Dawidek /* 10212a143d5bSPawel Jakub Dawidek * Look for memory leaks. 10222a143d5bSPawel Jakub Dawidek */ 10232a143d5bSPawel Jakub Dawidek temp_allocs = temp_bytes = 0; 10242a143d5bSPawel Jakub Dawidek for (i = 0; i < MAXCPU; i++) { 10252a143d5bSPawel Jakub Dawidek mtsp = &mtip->mti_stats[i]; 10262a143d5bSPawel Jakub Dawidek temp_allocs += mtsp->mts_numallocs; 10272a143d5bSPawel Jakub Dawidek temp_allocs -= mtsp->mts_numfrees; 10282a143d5bSPawel Jakub Dawidek temp_bytes += mtsp->mts_memalloced; 10292a143d5bSPawel Jakub Dawidek temp_bytes -= mtsp->mts_memfreed; 10302a143d5bSPawel Jakub Dawidek } 10312a143d5bSPawel Jakub Dawidek if (temp_allocs > 0 || temp_bytes > 0) { 10322a143d5bSPawel Jakub Dawidek printf("Warning: memory type %s leaked memory on destroy " 10332a143d5bSPawel Jakub Dawidek "(%ld allocations, %ld bytes leaked).\n", mtp->ks_shortdesc, 10342a143d5bSPawel Jakub Dawidek temp_allocs, temp_bytes); 10352a143d5bSPawel Jakub Dawidek } 10362a143d5bSPawel Jakub Dawidek 103745d48bdaSPaul Saab slab = vtoslab((vm_offset_t) mtip & (~UMA_SLAB_MASK)); 103845d48bdaSPaul Saab uma_zfree_arg(mt_zone, mtip, slab); 1039db669378SPeter Wemm } 10406f267175SJeff Roberson 1041d362c40dSPawel Jakub Dawidek struct malloc_type * 1042d362c40dSPawel Jakub Dawidek malloc_desc2type(const char *desc) 1043d362c40dSPawel Jakub Dawidek { 1044d362c40dSPawel Jakub Dawidek struct malloc_type *mtp; 1045d362c40dSPawel Jakub Dawidek 1046d362c40dSPawel Jakub Dawidek mtx_assert(&malloc_mtx, MA_OWNED); 1047d362c40dSPawel Jakub Dawidek for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) { 1048d362c40dSPawel Jakub Dawidek if (strcmp(mtp->ks_shortdesc, desc) == 0) 1049d362c40dSPawel Jakub Dawidek return (mtp); 1050d362c40dSPawel Jakub Dawidek } 1051d362c40dSPawel Jakub Dawidek return (NULL); 1052d362c40dSPawel Jakub Dawidek } 1053d362c40dSPawel Jakub Dawidek 10546f267175SJeff Roberson static int 1055cd814b26SRobert Watson sysctl_kern_malloc_stats(SYSCTL_HANDLER_ARGS) 1056cd814b26SRobert Watson { 1057cd814b26SRobert Watson struct malloc_type_stream_header mtsh; 1058cd814b26SRobert Watson struct malloc_type_internal *mtip; 1059cd814b26SRobert Watson struct malloc_type_header mth; 1060cd814b26SRobert Watson struct malloc_type *mtp; 10614e657159SMatthew D Fleming int error, i; 1062cd814b26SRobert Watson struct sbuf sbuf; 1063cd814b26SRobert Watson 106400f0e671SMatthew D Fleming error = sysctl_wire_old_buffer(req, 0); 106500f0e671SMatthew D Fleming if (error != 0) 106600f0e671SMatthew D Fleming return (error); 10674e657159SMatthew D Fleming sbuf_new_for_sysctl(&sbuf, NULL, 128, req); 10681eafc078SIan Lepore sbuf_clear_flags(&sbuf, SBUF_INCLUDENUL); 1069cd814b26SRobert Watson mtx_lock(&malloc_mtx); 1070cd814b26SRobert Watson 1071cd814b26SRobert Watson /* 1072cd814b26SRobert Watson * Insert stream header. 1073cd814b26SRobert Watson */ 1074cd814b26SRobert Watson bzero(&mtsh, sizeof(mtsh)); 1075cd814b26SRobert Watson mtsh.mtsh_version = MALLOC_TYPE_STREAM_VERSION; 1076cd814b26SRobert Watson mtsh.mtsh_maxcpus = MAXCPU; 1077cd814b26SRobert Watson mtsh.mtsh_count = kmemcount; 10784e657159SMatthew D Fleming (void)sbuf_bcat(&sbuf, &mtsh, sizeof(mtsh)); 1079cd814b26SRobert Watson 1080cd814b26SRobert Watson /* 1081cd814b26SRobert Watson * Insert alternating sequence of type headers and type statistics. 1082cd814b26SRobert Watson */ 1083cd814b26SRobert Watson for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) { 1084cd814b26SRobert Watson mtip = (struct malloc_type_internal *)mtp->ks_handle; 1085cd814b26SRobert Watson 1086cd814b26SRobert Watson /* 1087cd814b26SRobert Watson * Insert type header. 1088cd814b26SRobert Watson */ 1089cd814b26SRobert Watson bzero(&mth, sizeof(mth)); 1090cd814b26SRobert Watson strlcpy(mth.mth_name, mtp->ks_shortdesc, MALLOC_MAX_NAME); 10914e657159SMatthew D Fleming (void)sbuf_bcat(&sbuf, &mth, sizeof(mth)); 1092cd814b26SRobert Watson 1093cd814b26SRobert Watson /* 1094cd814b26SRobert Watson * Insert type statistics for each CPU. 1095cd814b26SRobert Watson */ 1096cd814b26SRobert Watson for (i = 0; i < MAXCPU; i++) { 10974e657159SMatthew D Fleming (void)sbuf_bcat(&sbuf, &mtip->mti_stats[i], 10984e657159SMatthew D Fleming sizeof(mtip->mti_stats[i])); 1099cd814b26SRobert Watson } 1100cd814b26SRobert Watson } 1101cd814b26SRobert Watson mtx_unlock(&malloc_mtx); 11024e657159SMatthew D Fleming error = sbuf_finish(&sbuf); 1103cd814b26SRobert Watson sbuf_delete(&sbuf); 1104cd814b26SRobert Watson return (error); 1105cd814b26SRobert Watson } 1106cd814b26SRobert Watson 1107cd814b26SRobert Watson SYSCTL_PROC(_kern, OID_AUTO, malloc_stats, CTLFLAG_RD|CTLTYPE_STRUCT, 1108cd814b26SRobert Watson 0, 0, sysctl_kern_malloc_stats, "s,malloc_type_ustats", 1109cd814b26SRobert Watson "Return malloc types"); 1110cd814b26SRobert Watson 1111cd814b26SRobert Watson SYSCTL_INT(_kern, OID_AUTO, malloc_count, CTLFLAG_RD, &kmemcount, 0, 1112cd814b26SRobert Watson "Count of kernel malloc types"); 1113cd814b26SRobert Watson 111491dd776cSJohn Birrell void 111591dd776cSJohn Birrell malloc_type_list(malloc_type_list_func_t *func, void *arg) 111691dd776cSJohn Birrell { 111791dd776cSJohn Birrell struct malloc_type *mtp, **bufmtp; 111891dd776cSJohn Birrell int count, i; 111991dd776cSJohn Birrell size_t buflen; 112091dd776cSJohn Birrell 112191dd776cSJohn Birrell mtx_lock(&malloc_mtx); 112291dd776cSJohn Birrell restart: 112391dd776cSJohn Birrell mtx_assert(&malloc_mtx, MA_OWNED); 112491dd776cSJohn Birrell count = kmemcount; 112591dd776cSJohn Birrell mtx_unlock(&malloc_mtx); 112691dd776cSJohn Birrell 112791dd776cSJohn Birrell buflen = sizeof(struct malloc_type *) * count; 112891dd776cSJohn Birrell bufmtp = malloc(buflen, M_TEMP, M_WAITOK); 112991dd776cSJohn Birrell 113091dd776cSJohn Birrell mtx_lock(&malloc_mtx); 113191dd776cSJohn Birrell 113291dd776cSJohn Birrell if (count < kmemcount) { 113391dd776cSJohn Birrell free(bufmtp, M_TEMP); 113491dd776cSJohn Birrell goto restart; 113591dd776cSJohn Birrell } 113691dd776cSJohn Birrell 113791dd776cSJohn Birrell for (mtp = kmemstatistics, i = 0; mtp != NULL; mtp = mtp->ks_next, i++) 113891dd776cSJohn Birrell bufmtp[i] = mtp; 113991dd776cSJohn Birrell 114091dd776cSJohn Birrell mtx_unlock(&malloc_mtx); 114191dd776cSJohn Birrell 114291dd776cSJohn Birrell for (i = 0; i < count; i++) 114391dd776cSJohn Birrell (func)(bufmtp[i], arg); 114491dd776cSJohn Birrell 114591dd776cSJohn Birrell free(bufmtp, M_TEMP); 114691dd776cSJohn Birrell } 114791dd776cSJohn Birrell 1148909ed16cSRobert Watson #ifdef DDB 1149909ed16cSRobert Watson DB_SHOW_COMMAND(malloc, db_show_malloc) 1150909ed16cSRobert Watson { 1151909ed16cSRobert Watson struct malloc_type_internal *mtip; 1152909ed16cSRobert Watson struct malloc_type *mtp; 115360ae52f7SEd Schouten uint64_t allocs, frees; 115460ae52f7SEd Schouten uint64_t alloced, freed; 1155909ed16cSRobert Watson int i; 1156909ed16cSRobert Watson 115724076d13SRobert Watson db_printf("%18s %12s %12s %12s\n", "Type", "InUse", "MemUse", 115824076d13SRobert Watson "Requests"); 1159909ed16cSRobert Watson for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) { 1160909ed16cSRobert Watson mtip = (struct malloc_type_internal *)mtp->ks_handle; 1161909ed16cSRobert Watson allocs = 0; 1162909ed16cSRobert Watson frees = 0; 116324076d13SRobert Watson alloced = 0; 116424076d13SRobert Watson freed = 0; 1165909ed16cSRobert Watson for (i = 0; i < MAXCPU; i++) { 1166909ed16cSRobert Watson allocs += mtip->mti_stats[i].mts_numallocs; 1167909ed16cSRobert Watson frees += mtip->mti_stats[i].mts_numfrees; 116824076d13SRobert Watson alloced += mtip->mti_stats[i].mts_memalloced; 116924076d13SRobert Watson freed += mtip->mti_stats[i].mts_memfreed; 1170909ed16cSRobert Watson } 117124076d13SRobert Watson db_printf("%18s %12ju %12juK %12ju\n", 117224076d13SRobert Watson mtp->ks_shortdesc, allocs - frees, 117324076d13SRobert Watson (alloced - freed + 1023) / 1024, allocs); 1174687c94aaSJohn Baldwin if (db_pager_quit) 1175687c94aaSJohn Baldwin break; 1176909ed16cSRobert Watson } 1177909ed16cSRobert Watson } 1178d7854da1SMatthew D Fleming 1179d7854da1SMatthew D Fleming #if MALLOC_DEBUG_MAXZONES > 1 1180d7854da1SMatthew D Fleming DB_SHOW_COMMAND(multizone_matches, db_show_multizone_matches) 1181d7854da1SMatthew D Fleming { 1182d7854da1SMatthew D Fleming struct malloc_type_internal *mtip; 1183d7854da1SMatthew D Fleming struct malloc_type *mtp; 1184d7854da1SMatthew D Fleming u_int subzone; 1185d7854da1SMatthew D Fleming 1186d7854da1SMatthew D Fleming if (!have_addr) { 1187d7854da1SMatthew D Fleming db_printf("Usage: show multizone_matches <malloc type/addr>\n"); 1188d7854da1SMatthew D Fleming return; 1189d7854da1SMatthew D Fleming } 1190d7854da1SMatthew D Fleming mtp = (void *)addr; 1191d7854da1SMatthew D Fleming if (mtp->ks_magic != M_MAGIC) { 1192d7854da1SMatthew D Fleming db_printf("Magic %lx does not match expected %x\n", 1193d7854da1SMatthew D Fleming mtp->ks_magic, M_MAGIC); 1194d7854da1SMatthew D Fleming return; 1195d7854da1SMatthew D Fleming } 1196d7854da1SMatthew D Fleming 1197d7854da1SMatthew D Fleming mtip = mtp->ks_handle; 1198d7854da1SMatthew D Fleming subzone = mtip->mti_zone; 1199d7854da1SMatthew D Fleming 1200d7854da1SMatthew D Fleming for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) { 1201d7854da1SMatthew D Fleming mtip = mtp->ks_handle; 1202d7854da1SMatthew D Fleming if (mtip->mti_zone != subzone) 1203d7854da1SMatthew D Fleming continue; 1204d7854da1SMatthew D Fleming db_printf("%s\n", mtp->ks_shortdesc); 1205687c94aaSJohn Baldwin if (db_pager_quit) 1206687c94aaSJohn Baldwin break; 1207d7854da1SMatthew D Fleming } 1208d7854da1SMatthew D Fleming } 1209d7854da1SMatthew D Fleming #endif /* MALLOC_DEBUG_MAXZONES > 1 */ 1210d7854da1SMatthew D Fleming #endif /* DDB */ 1211909ed16cSRobert Watson 12125e914b96SJeff Roberson #ifdef MALLOC_PROFILE 12135e914b96SJeff Roberson 12145e914b96SJeff Roberson static int 12155e914b96SJeff Roberson sysctl_kern_mprof(SYSCTL_HANDLER_ARGS) 12165e914b96SJeff Roberson { 121763a7e0a3SRobert Watson struct sbuf sbuf; 12185e914b96SJeff Roberson uint64_t count; 12195e914b96SJeff Roberson uint64_t waste; 12205e914b96SJeff Roberson uint64_t mem; 12215e914b96SJeff Roberson int error; 12225e914b96SJeff Roberson int rsize; 12235e914b96SJeff Roberson int size; 12245e914b96SJeff Roberson int i; 12255e914b96SJeff Roberson 12265e914b96SJeff Roberson waste = 0; 12275e914b96SJeff Roberson mem = 0; 12285e914b96SJeff Roberson 122900f0e671SMatthew D Fleming error = sysctl_wire_old_buffer(req, 0); 123000f0e671SMatthew D Fleming if (error != 0) 123100f0e671SMatthew D Fleming return (error); 12324e657159SMatthew D Fleming sbuf_new_for_sysctl(&sbuf, NULL, 128, req); 123363a7e0a3SRobert Watson sbuf_printf(&sbuf, 12345e914b96SJeff Roberson "\n Size Requests Real Size\n"); 12355e914b96SJeff Roberson for (i = 0; i < KMEM_ZSIZE; i++) { 12365e914b96SJeff Roberson size = i << KMEM_ZSHIFT; 12375e914b96SJeff Roberson rsize = kmemzones[kmemsize[i]].kz_size; 12385e914b96SJeff Roberson count = (long long unsigned)krequests[i]; 12395e914b96SJeff Roberson 124063a7e0a3SRobert Watson sbuf_printf(&sbuf, "%6d%28llu%11d\n", size, 124163a7e0a3SRobert Watson (unsigned long long)count, rsize); 12425e914b96SJeff Roberson 12435e914b96SJeff Roberson if ((rsize * count) > (size * count)) 12445e914b96SJeff Roberson waste += (rsize * count) - (size * count); 12455e914b96SJeff Roberson mem += (rsize * count); 12465e914b96SJeff Roberson } 124763a7e0a3SRobert Watson sbuf_printf(&sbuf, 12485e914b96SJeff Roberson "\nTotal memory used:\t%30llu\nTotal Memory wasted:\t%30llu\n", 12495e914b96SJeff Roberson (unsigned long long)mem, (unsigned long long)waste); 12504e657159SMatthew D Fleming error = sbuf_finish(&sbuf); 125163a7e0a3SRobert Watson sbuf_delete(&sbuf); 12525e914b96SJeff Roberson return (error); 12535e914b96SJeff Roberson } 12545e914b96SJeff Roberson 12555e914b96SJeff Roberson SYSCTL_OID(_kern, OID_AUTO, mprof, CTLTYPE_STRING|CTLFLAG_RD, 12565e914b96SJeff Roberson NULL, 0, sysctl_kern_mprof, "A", "Malloc Profiling"); 12575e914b96SJeff Roberson #endif /* MALLOC_PROFILE */ 1258