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> 64*9afff6b1SMateusz Guzik #include <sys/smp.h> 656f267175SJeff Roberson #include <sys/sysctl.h> 661fb14a47SPoul-Henning Kamp #include <sys/time.h> 675df87b21SJeff Roberson #include <sys/vmem.h> 689a02e8c6SJason Evans 69df8bae1dSRodney W. Grimes #include <vm/vm.h> 7099571dc3SJeff Roberson #include <vm/pmap.h> 715df87b21SJeff Roberson #include <vm/vm_pageout.h> 72efeaf95aSDavid Greenman #include <vm/vm_param.h> 73df8bae1dSRodney W. Grimes #include <vm/vm_kern.h> 74efeaf95aSDavid Greenman #include <vm/vm_extern.h> 753075778bSJohn Dyson #include <vm/vm_map.h> 7699571dc3SJeff Roberson #include <vm/vm_page.h> 778355f576SJeff Roberson #include <vm/uma.h> 788355f576SJeff Roberson #include <vm/uma_int.h> 798efc4effSJeff Roberson #include <vm/uma_dbg.h> 80df8bae1dSRodney W. Grimes 81e4eb384bSBosko Milekic #ifdef DEBUG_MEMGUARD 82e4eb384bSBosko Milekic #include <vm/memguard.h> 83e4eb384bSBosko Milekic #endif 84847a2a17SPawel Jakub Dawidek #ifdef DEBUG_REDZONE 85847a2a17SPawel Jakub Dawidek #include <vm/redzone.h> 86847a2a17SPawel Jakub Dawidek #endif 87e4eb384bSBosko Milekic 88984982d6SPoul-Henning Kamp #if defined(INVARIANTS) && defined(__i386__) 89984982d6SPoul-Henning Kamp #include <machine/cpu.h> 90984982d6SPoul-Henning Kamp #endif 91984982d6SPoul-Henning Kamp 92909ed16cSRobert Watson #include <ddb/ddb.h> 93909ed16cSRobert Watson 9491dd776cSJohn Birrell #ifdef KDTRACE_HOOKS 9591dd776cSJohn Birrell #include <sys/dtrace_bsd.h> 9691dd776cSJohn Birrell 977cd79421SMateusz Guzik bool __read_frequently dtrace_malloc_enabled; 987cd79421SMateusz Guzik dtrace_malloc_probe_func_t __read_mostly dtrace_malloc_probe; 9991dd776cSJohn Birrell #endif 10091dd776cSJohn Birrell 101ab3185d1SJeff Roberson #if defined(INVARIANTS) || defined(MALLOC_MAKE_FAILURES) || \ 102ab3185d1SJeff Roberson defined(DEBUG_MEMGUARD) || defined(DEBUG_REDZONE) 103ab3185d1SJeff Roberson #define MALLOC_DEBUG 1 104ab3185d1SJeff Roberson #endif 105ab3185d1SJeff Roberson 10644a8ff31SArchie Cobbs /* 10744a8ff31SArchie Cobbs * When realloc() is called, if the new size is sufficiently smaller than 10844a8ff31SArchie Cobbs * the old size, realloc() will allocate a new, smaller block to avoid 10944a8ff31SArchie Cobbs * wasting memory. 'Sufficiently smaller' is defined as: newsize <= 11044a8ff31SArchie Cobbs * oldsize / 2^n, where REALLOC_FRACTION defines the value of 'n'. 11144a8ff31SArchie Cobbs */ 11244a8ff31SArchie Cobbs #ifndef REALLOC_FRACTION 11344a8ff31SArchie Cobbs #define REALLOC_FRACTION 1 /* new block if <= half the size */ 11444a8ff31SArchie Cobbs #endif 11544a8ff31SArchie Cobbs 1160ce3f16dSRobert Watson /* 1170ce3f16dSRobert Watson * Centrally define some common malloc types. 1180ce3f16dSRobert Watson */ 1193b6fb885SPoul-Henning Kamp MALLOC_DEFINE(M_CACHE, "cache", "Various Dynamically allocated caches"); 1209ef246c6SBruce Evans MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory"); 1219ef246c6SBruce Evans MALLOC_DEFINE(M_TEMP, "temp", "misc temporary data buffers"); 1229ef246c6SBruce Evans 123db669378SPeter Wemm static struct malloc_type *kmemstatistics; 124cd814b26SRobert Watson static int kmemcount; 1251f6889a1SMatthew Dillon 1268355f576SJeff Roberson #define KMEM_ZSHIFT 4 1278355f576SJeff Roberson #define KMEM_ZBASE 16 1288355f576SJeff Roberson #define KMEM_ZMASK (KMEM_ZBASE - 1) 1298355f576SJeff Roberson 130bda06553SXin LI #define KMEM_ZMAX 65536 1318355f576SJeff Roberson #define KMEM_ZSIZE (KMEM_ZMAX >> KMEM_ZSHIFT) 13260ae52f7SEd Schouten static uint8_t kmemsize[KMEM_ZSIZE + 1]; 1336f267175SJeff Roberson 134d7854da1SMatthew D Fleming #ifndef MALLOC_DEBUG_MAXZONES 135d7854da1SMatthew D Fleming #define MALLOC_DEBUG_MAXZONES 1 136d7854da1SMatthew D Fleming #endif 137d7854da1SMatthew D Fleming static int numzones = MALLOC_DEBUG_MAXZONES; 138d7854da1SMatthew D Fleming 1390ce3f16dSRobert Watson /* 1400ce3f16dSRobert Watson * Small malloc(9) memory allocations are allocated from a set of UMA buckets 1410ce3f16dSRobert Watson * of various sizes. 1420ce3f16dSRobert Watson * 1430ce3f16dSRobert Watson * XXX: The comment here used to read "These won't be powers of two for 1440ce3f16dSRobert Watson * long." It's possible that a significant amount of wasted memory could be 1450ce3f16dSRobert Watson * recovered by tuning the sizes of these buckets. 1460ce3f16dSRobert Watson */ 1478355f576SJeff Roberson struct { 1486f267175SJeff Roberson int kz_size; 1496f267175SJeff Roberson char *kz_name; 150d7854da1SMatthew D Fleming uma_zone_t kz_zone[MALLOC_DEBUG_MAXZONES]; 1516f267175SJeff Roberson } kmemzones[] = { 152d7854da1SMatthew D Fleming {16, "16", }, 153d7854da1SMatthew D Fleming {32, "32", }, 154d7854da1SMatthew D Fleming {64, "64", }, 155d7854da1SMatthew D Fleming {128, "128", }, 156d7854da1SMatthew D Fleming {256, "256", }, 157d7854da1SMatthew D Fleming {512, "512", }, 158d7854da1SMatthew D Fleming {1024, "1024", }, 159d7854da1SMatthew D Fleming {2048, "2048", }, 160d7854da1SMatthew D Fleming {4096, "4096", }, 161d7854da1SMatthew D Fleming {8192, "8192", }, 162d7854da1SMatthew D Fleming {16384, "16384", }, 163d7854da1SMatthew D Fleming {32768, "32768", }, 164d7854da1SMatthew D Fleming {65536, "65536", }, 1658355f576SJeff Roberson {0, NULL}, 1668355f576SJeff Roberson }; 1678355f576SJeff Roberson 1680ce3f16dSRobert Watson /* 1690ce3f16dSRobert Watson * Zone to allocate malloc type descriptions from. For ABI reasons, memory 1700ce3f16dSRobert Watson * types are described by a data structure passed by the declaring code, but 1710ce3f16dSRobert Watson * the malloc(9) implementation has its own data structure describing the 1720ce3f16dSRobert Watson * type and statistics. This permits the malloc(9)-internal data structures 1730ce3f16dSRobert Watson * to be modified without breaking binary-compiled kernel modules that 1740ce3f16dSRobert Watson * declare malloc types. 1750ce3f16dSRobert Watson */ 17663a7e0a3SRobert Watson static uma_zone_t mt_zone; 177*9afff6b1SMateusz Guzik static uma_zone_t mt_stats_zone; 17863a7e0a3SRobert Watson 179b89eaf4eSAlan Cox u_long vm_kmem_size; 180d801e824SAndriy Gapon SYSCTL_ULONG(_vm, OID_AUTO, kmem_size, CTLFLAG_RDTUN, &vm_kmem_size, 0, 18184344f9fSDag-Erling Smørgrav "Size of kernel memory"); 1825a34a9f0SJeff Roberson 1837001d850SXin LI static u_long kmem_zmax = KMEM_ZMAX; 1847001d850SXin LI SYSCTL_ULONG(_vm, OID_AUTO, kmem_zmax, CTLFLAG_RDTUN, &kmem_zmax, 0, 1857001d850SXin LI "Maximum allocation size that malloc(9) would use UMA as backend"); 1867001d850SXin LI 187b89eaf4eSAlan Cox static u_long vm_kmem_size_min; 188d801e824SAndriy Gapon SYSCTL_ULONG(_vm, OID_AUTO, kmem_size_min, CTLFLAG_RDTUN, &vm_kmem_size_min, 0, 1890e5179e4SStephane E. Potvin "Minimum size of kernel memory"); 1900e5179e4SStephane E. Potvin 191b89eaf4eSAlan Cox static u_long vm_kmem_size_max; 192d801e824SAndriy Gapon SYSCTL_ULONG(_vm, OID_AUTO, kmem_size_max, CTLFLAG_RDTUN, &vm_kmem_size_max, 0, 193479439b4SDag-Erling Smørgrav "Maximum size of kernel memory"); 194479439b4SDag-Erling Smørgrav 1954813ad54SHans Petter Selasky static u_int vm_kmem_size_scale; 196d801e824SAndriy Gapon SYSCTL_UINT(_vm, OID_AUTO, kmem_size_scale, CTLFLAG_RDTUN, &vm_kmem_size_scale, 0, 197479439b4SDag-Erling Smørgrav "Scale factor for kernel memory size"); 198479439b4SDag-Erling Smørgrav 1997814c80aSAndriy Gapon static int sysctl_kmem_map_size(SYSCTL_HANDLER_ARGS); 2007814c80aSAndriy Gapon SYSCTL_PROC(_vm, OID_AUTO, kmem_map_size, 2017814c80aSAndriy Gapon CTLFLAG_RD | CTLTYPE_ULONG | CTLFLAG_MPSAFE, NULL, 0, 2025df87b21SJeff Roberson sysctl_kmem_map_size, "LU", "Current kmem allocation size"); 2037814c80aSAndriy Gapon 20495bb9d38SAndriy Gapon static int sysctl_kmem_map_free(SYSCTL_HANDLER_ARGS); 20595bb9d38SAndriy Gapon SYSCTL_PROC(_vm, OID_AUTO, kmem_map_free, 20695bb9d38SAndriy Gapon CTLFLAG_RD | CTLTYPE_ULONG | CTLFLAG_MPSAFE, NULL, 0, 2075df87b21SJeff Roberson sysctl_kmem_map_free, "LU", "Free space in kmem"); 20895bb9d38SAndriy Gapon 2095a34a9f0SJeff Roberson /* 21099571dc3SJeff Roberson * The malloc_mtx protects the kmemstatistics linked list. 2115a34a9f0SJeff Roberson */ 2125a34a9f0SJeff Roberson struct mtx malloc_mtx; 21369ef67f9SJason Evans 2145e914b96SJeff Roberson #ifdef MALLOC_PROFILE 2155e914b96SJeff Roberson uint64_t krequests[KMEM_ZSIZE + 1]; 2166f267175SJeff Roberson 2175e914b96SJeff Roberson static int sysctl_kern_mprof(SYSCTL_HANDLER_ARGS); 2185e914b96SJeff Roberson #endif 2195e914b96SJeff Roberson 220cd814b26SRobert Watson static int sysctl_kern_malloc_stats(SYSCTL_HANDLER_ARGS); 221df8bae1dSRodney W. Grimes 2220ce3f16dSRobert Watson /* 2230ce3f16dSRobert Watson * time_uptime of the last malloc(9) failure (induced or real). 2240ce3f16dSRobert Watson */ 2251fb14a47SPoul-Henning Kamp static time_t t_malloc_fail; 2261fb14a47SPoul-Henning Kamp 227d7854da1SMatthew D Fleming #if defined(MALLOC_MAKE_FAILURES) || (MALLOC_DEBUG_MAXZONES > 1) 2286472ac3dSEd Schouten static SYSCTL_NODE(_debug, OID_AUTO, malloc, CTLFLAG_RD, 0, 229d7854da1SMatthew D Fleming "Kernel malloc debugging options"); 230d7854da1SMatthew D Fleming #endif 231d7854da1SMatthew D Fleming 232eae870cdSRobert Watson /* 2330ce3f16dSRobert Watson * malloc(9) fault injection -- cause malloc failures every (n) mallocs when 2340ce3f16dSRobert Watson * the caller specifies M_NOWAIT. If set to 0, no failures are caused. 235eae870cdSRobert Watson */ 2360ce3f16dSRobert Watson #ifdef MALLOC_MAKE_FAILURES 237eae870cdSRobert Watson static int malloc_failure_rate; 238eae870cdSRobert Watson static int malloc_nowait_count; 239eae870cdSRobert Watson static int malloc_failure_count; 240af3b2549SHans Petter Selasky SYSCTL_INT(_debug_malloc, OID_AUTO, failure_rate, CTLFLAG_RWTUN, 241eae870cdSRobert Watson &malloc_failure_rate, 0, "Every (n) mallocs with M_NOWAIT will fail"); 242eae870cdSRobert Watson SYSCTL_INT(_debug_malloc, OID_AUTO, failure_count, CTLFLAG_RD, 243eae870cdSRobert Watson &malloc_failure_count, 0, "Number of imposed M_NOWAIT malloc failures"); 244eae870cdSRobert Watson #endif 245eae870cdSRobert Watson 2467814c80aSAndriy Gapon static int 2477814c80aSAndriy Gapon sysctl_kmem_map_size(SYSCTL_HANDLER_ARGS) 2487814c80aSAndriy Gapon { 2497814c80aSAndriy Gapon u_long size; 2507814c80aSAndriy Gapon 2512e47807cSJeff Roberson size = uma_size(); 2527814c80aSAndriy Gapon return (sysctl_handle_long(oidp, &size, 0, req)); 2537814c80aSAndriy Gapon } 2547814c80aSAndriy Gapon 25595bb9d38SAndriy Gapon static int 25695bb9d38SAndriy Gapon sysctl_kmem_map_free(SYSCTL_HANDLER_ARGS) 25795bb9d38SAndriy Gapon { 2582e47807cSJeff Roberson u_long size, limit; 25995bb9d38SAndriy Gapon 2602e47807cSJeff Roberson /* The sysctl is unsigned, implement as a saturation value. */ 2612e47807cSJeff Roberson size = uma_size(); 2622e47807cSJeff Roberson limit = uma_limit(); 2632e47807cSJeff Roberson if (size > limit) 2642e47807cSJeff Roberson size = 0; 2652e47807cSJeff Roberson else 2662e47807cSJeff Roberson size = limit - size; 26795bb9d38SAndriy Gapon return (sysctl_handle_long(oidp, &size, 0, req)); 26895bb9d38SAndriy Gapon } 26995bb9d38SAndriy Gapon 270d7854da1SMatthew D Fleming /* 271d7854da1SMatthew D Fleming * malloc(9) uma zone separation -- sub-page buffer overruns in one 272d7854da1SMatthew D Fleming * malloc type will affect only a subset of other malloc types. 273d7854da1SMatthew D Fleming */ 274d7854da1SMatthew D Fleming #if MALLOC_DEBUG_MAXZONES > 1 275d7854da1SMatthew D Fleming static void 276d7854da1SMatthew D Fleming tunable_set_numzones(void) 277d7854da1SMatthew D Fleming { 278d7854da1SMatthew D Fleming 279d7854da1SMatthew D Fleming TUNABLE_INT_FETCH("debug.malloc.numzones", 280d7854da1SMatthew D Fleming &numzones); 281d7854da1SMatthew D Fleming 282d7854da1SMatthew D Fleming /* Sanity check the number of malloc uma zones. */ 283d7854da1SMatthew D Fleming if (numzones <= 0) 284d7854da1SMatthew D Fleming numzones = 1; 285d7854da1SMatthew D Fleming if (numzones > MALLOC_DEBUG_MAXZONES) 286d7854da1SMatthew D Fleming numzones = MALLOC_DEBUG_MAXZONES; 287d7854da1SMatthew D Fleming } 288d7854da1SMatthew D Fleming SYSINIT(numzones, SI_SUB_TUNABLES, SI_ORDER_ANY, tunable_set_numzones, NULL); 289af3b2549SHans Petter Selasky SYSCTL_INT(_debug_malloc, OID_AUTO, numzones, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, 290d7854da1SMatthew D Fleming &numzones, 0, "Number of malloc uma subzones"); 291d7854da1SMatthew D Fleming 292d7854da1SMatthew D Fleming /* 293d7854da1SMatthew D Fleming * Any number that changes regularly is an okay choice for the 294d7854da1SMatthew D Fleming * offset. Build numbers are pretty good of you have them. 295d7854da1SMatthew D Fleming */ 296d7854da1SMatthew D Fleming static u_int zone_offset = __FreeBSD_version; 297d7854da1SMatthew D Fleming TUNABLE_INT("debug.malloc.zone_offset", &zone_offset); 298d7854da1SMatthew D Fleming SYSCTL_UINT(_debug_malloc, OID_AUTO, zone_offset, CTLFLAG_RDTUN, 299d7854da1SMatthew D Fleming &zone_offset, 0, "Separate malloc types by examining the " 300d7854da1SMatthew D Fleming "Nth character in the malloc type short description."); 301d7854da1SMatthew D Fleming 302c9e05ccdSMateusz Guzik static void 303c9e05ccdSMateusz Guzik mtp_set_subzone(struct malloc_type *mtp) 304d7854da1SMatthew D Fleming { 305c9e05ccdSMateusz Guzik struct malloc_type_internal *mtip; 306c9e05ccdSMateusz Guzik const char *desc; 307d7854da1SMatthew D Fleming size_t len; 308d7854da1SMatthew D Fleming u_int val; 309d7854da1SMatthew D Fleming 310c9e05ccdSMateusz Guzik mtip = mtp->ks_handle; 311c9e05ccdSMateusz Guzik desc = mtp->ks_shortdesc; 312d7854da1SMatthew D Fleming if (desc == NULL || (len = strlen(desc)) == 0) 313c9e05ccdSMateusz Guzik val = 0; 314c9e05ccdSMateusz Guzik else 315d7854da1SMatthew D Fleming val = desc[zone_offset % len]; 316c9e05ccdSMateusz Guzik mtip->mti_zone = (val % numzones); 317c9e05ccdSMateusz Guzik } 318c9e05ccdSMateusz Guzik 319c9e05ccdSMateusz Guzik static inline u_int 320c9e05ccdSMateusz Guzik mtp_get_subzone(struct malloc_type *mtp) 321c9e05ccdSMateusz Guzik { 322c9e05ccdSMateusz Guzik struct malloc_type_internal *mtip; 323c9e05ccdSMateusz Guzik 324c9e05ccdSMateusz Guzik mtip = mtp->ks_handle; 325c9e05ccdSMateusz Guzik 326c9e05ccdSMateusz Guzik KASSERT(mtip->mti_zone < numzones, 327c9e05ccdSMateusz Guzik ("mti_zone %u out of range %d", 328c9e05ccdSMateusz Guzik mtip->mti_zone, numzones)); 329c9e05ccdSMateusz Guzik return (mtip->mti_zone); 330d7854da1SMatthew D Fleming } 331d7854da1SMatthew D Fleming #elif MALLOC_DEBUG_MAXZONES == 0 332d7854da1SMatthew D Fleming #error "MALLOC_DEBUG_MAXZONES must be positive." 333d7854da1SMatthew D Fleming #else 334c9e05ccdSMateusz Guzik static void 335c9e05ccdSMateusz Guzik mtp_set_subzone(struct malloc_type *mtp) 336c9e05ccdSMateusz Guzik { 337c9e05ccdSMateusz Guzik struct malloc_type_internal *mtip; 338c9e05ccdSMateusz Guzik 339c9e05ccdSMateusz Guzik mtip = mtp->ks_handle; 340c9e05ccdSMateusz Guzik mtip->mti_zone = 0; 341c9e05ccdSMateusz Guzik } 342c9e05ccdSMateusz Guzik 343d7854da1SMatthew D Fleming static inline u_int 344c9e05ccdSMateusz Guzik mtp_get_subzone(struct malloc_type *mtp) 345d7854da1SMatthew D Fleming { 346d7854da1SMatthew D Fleming 347d7854da1SMatthew D Fleming return (0); 348d7854da1SMatthew D Fleming } 349d7854da1SMatthew D Fleming #endif /* MALLOC_DEBUG_MAXZONES > 1 */ 350d7854da1SMatthew D Fleming 3511fb14a47SPoul-Henning Kamp int 3521fb14a47SPoul-Henning Kamp malloc_last_fail(void) 3531fb14a47SPoul-Henning Kamp { 3541fb14a47SPoul-Henning Kamp 3551fb14a47SPoul-Henning Kamp return (time_uptime - t_malloc_fail); 3561fb14a47SPoul-Henning Kamp } 3571fb14a47SPoul-Henning Kamp 358df8bae1dSRodney W. Grimes /* 3590ce3f16dSRobert Watson * An allocation has succeeded -- update malloc type statistics for the 3600ce3f16dSRobert Watson * amount of bucket size. Occurs within a critical section so that the 3610ce3f16dSRobert Watson * thread isn't preempted and doesn't migrate while updating per-PCU 3620ce3f16dSRobert Watson * statistics. 3634362fadaSBrian Feldman */ 3644362fadaSBrian Feldman static void 36563a7e0a3SRobert Watson malloc_type_zone_allocated(struct malloc_type *mtp, unsigned long size, 3664362fadaSBrian Feldman int zindx) 3674362fadaSBrian Feldman { 36863a7e0a3SRobert Watson struct malloc_type_internal *mtip; 36963a7e0a3SRobert Watson struct malloc_type_stats *mtsp; 37063a7e0a3SRobert Watson 37163a7e0a3SRobert Watson critical_enter(); 37263a7e0a3SRobert Watson mtip = mtp->ks_handle; 373*9afff6b1SMateusz Guzik mtsp = zpcpu_get(mtip->mti_stats); 37473864adbSPawel Jakub Dawidek if (size > 0) { 37563a7e0a3SRobert Watson mtsp->mts_memalloced += size; 37663a7e0a3SRobert Watson mtsp->mts_numallocs++; 37773864adbSPawel Jakub Dawidek } 3784362fadaSBrian Feldman if (zindx != -1) 37963a7e0a3SRobert Watson mtsp->mts_size |= 1 << zindx; 38091dd776cSJohn Birrell 38191dd776cSJohn Birrell #ifdef KDTRACE_HOOKS 3827cd79421SMateusz Guzik if (__predict_false(dtrace_malloc_enabled)) { 38391dd776cSJohn Birrell uint32_t probe_id = mtip->mti_probes[DTMALLOC_PROBE_MALLOC]; 38491dd776cSJohn Birrell if (probe_id != 0) 38591dd776cSJohn Birrell (dtrace_malloc_probe)(probe_id, 38691dd776cSJohn Birrell (uintptr_t) mtp, (uintptr_t) mtip, 38791dd776cSJohn Birrell (uintptr_t) mtsp, size, zindx); 38891dd776cSJohn Birrell } 38991dd776cSJohn Birrell #endif 39091dd776cSJohn Birrell 39163a7e0a3SRobert Watson critical_exit(); 3924362fadaSBrian Feldman } 3934362fadaSBrian Feldman 3944362fadaSBrian Feldman void 39563a7e0a3SRobert Watson malloc_type_allocated(struct malloc_type *mtp, unsigned long size) 3964362fadaSBrian Feldman { 39763a7e0a3SRobert Watson 39873864adbSPawel Jakub Dawidek if (size > 0) 39963a7e0a3SRobert Watson malloc_type_zone_allocated(mtp, size, -1); 4004362fadaSBrian Feldman } 4014362fadaSBrian Feldman 4024362fadaSBrian Feldman /* 4033805385eSRobert Watson * A free operation has occurred -- update malloc type statistics for the 4040ce3f16dSRobert Watson * amount of the bucket size. Occurs within a critical section so that the 4050ce3f16dSRobert Watson * thread isn't preempted and doesn't migrate while updating per-CPU 4060ce3f16dSRobert Watson * statistics. 4074362fadaSBrian Feldman */ 4084362fadaSBrian Feldman void 40963a7e0a3SRobert Watson malloc_type_freed(struct malloc_type *mtp, unsigned long size) 4104362fadaSBrian Feldman { 41163a7e0a3SRobert Watson struct malloc_type_internal *mtip; 41263a7e0a3SRobert Watson struct malloc_type_stats *mtsp; 41363a7e0a3SRobert Watson 41463a7e0a3SRobert Watson critical_enter(); 41563a7e0a3SRobert Watson mtip = mtp->ks_handle; 416*9afff6b1SMateusz Guzik mtsp = zpcpu_get(mtip->mti_stats); 41763a7e0a3SRobert Watson mtsp->mts_memfreed += size; 41863a7e0a3SRobert Watson mtsp->mts_numfrees++; 41991dd776cSJohn Birrell 42091dd776cSJohn Birrell #ifdef KDTRACE_HOOKS 4217cd79421SMateusz Guzik if (__predict_false(dtrace_malloc_enabled)) { 42291dd776cSJohn Birrell uint32_t probe_id = mtip->mti_probes[DTMALLOC_PROBE_FREE]; 42391dd776cSJohn Birrell if (probe_id != 0) 42491dd776cSJohn Birrell (dtrace_malloc_probe)(probe_id, 42591dd776cSJohn Birrell (uintptr_t) mtp, (uintptr_t) mtip, 42691dd776cSJohn Birrell (uintptr_t) mtsp, size, 0); 42791dd776cSJohn Birrell } 42891dd776cSJohn Birrell #endif 42991dd776cSJohn Birrell 43063a7e0a3SRobert Watson critical_exit(); 4314362fadaSBrian Feldman } 4324362fadaSBrian Feldman 4334362fadaSBrian Feldman /* 434f346986bSAlan Cox * contigmalloc: 435f346986bSAlan Cox * 436f346986bSAlan Cox * Allocate a block of physically contiguous memory. 437f346986bSAlan Cox * 438f346986bSAlan Cox * If M_NOWAIT is set, this routine will not block and return NULL if 439f346986bSAlan Cox * the allocation fails. 440f346986bSAlan Cox */ 441f346986bSAlan Cox void * 442f346986bSAlan Cox contigmalloc(unsigned long size, struct malloc_type *type, int flags, 443f346986bSAlan Cox vm_paddr_t low, vm_paddr_t high, unsigned long alignment, 444831ce4cbSJohn Baldwin vm_paddr_t boundary) 445f346986bSAlan Cox { 446f346986bSAlan Cox void *ret; 447f346986bSAlan Cox 44844d0efb2SAlan Cox ret = (void *)kmem_alloc_contig(size, flags, low, high, alignment, 44944d0efb2SAlan Cox boundary, VM_MEMATTR_DEFAULT); 450f346986bSAlan Cox if (ret != NULL) 451f346986bSAlan Cox malloc_type_allocated(type, round_page(size)); 452f346986bSAlan Cox return (ret); 453f346986bSAlan Cox } 454f346986bSAlan Cox 455ab3185d1SJeff Roberson void * 456ab3185d1SJeff Roberson contigmalloc_domain(unsigned long size, struct malloc_type *type, 457ab3185d1SJeff Roberson int domain, int flags, vm_paddr_t low, vm_paddr_t high, 458ab3185d1SJeff Roberson unsigned long alignment, vm_paddr_t boundary) 459ab3185d1SJeff Roberson { 460ab3185d1SJeff Roberson void *ret; 461ab3185d1SJeff Roberson 462ab3185d1SJeff Roberson ret = (void *)kmem_alloc_contig_domain(domain, size, flags, low, high, 463ab3185d1SJeff Roberson alignment, boundary, VM_MEMATTR_DEFAULT); 464ab3185d1SJeff Roberson if (ret != NULL) 465ab3185d1SJeff Roberson malloc_type_allocated(type, round_page(size)); 466ab3185d1SJeff Roberson return (ret); 467ab3185d1SJeff Roberson } 468ab3185d1SJeff Roberson 469f346986bSAlan Cox /* 470f346986bSAlan Cox * contigfree: 471f346986bSAlan Cox * 472f346986bSAlan Cox * Free a block of memory allocated by contigmalloc. 473f346986bSAlan Cox * 474f346986bSAlan Cox * This routine may not block. 475f346986bSAlan Cox */ 476f346986bSAlan Cox void 477f346986bSAlan Cox contigfree(void *addr, unsigned long size, struct malloc_type *type) 478f346986bSAlan Cox { 479f346986bSAlan Cox 48049bfa624SAlan Cox kmem_free((vm_offset_t)addr, size); 481f346986bSAlan Cox malloc_type_freed(type, round_page(size)); 482f346986bSAlan Cox } 483f346986bSAlan Cox 484ab3185d1SJeff Roberson #ifdef MALLOC_DEBUG 485ab3185d1SJeff Roberson static int 4865a70796aSLi-Wen Hsu malloc_dbg(caddr_t *vap, size_t *sizep, struct malloc_type *mtp, 487ab3185d1SJeff Roberson int flags) 488df8bae1dSRodney W. Grimes { 489194a0abfSPoul-Henning Kamp #ifdef INVARIANTS 490ab3185d1SJeff Roberson int indx; 491ab3185d1SJeff Roberson 492bb1c7df8SRobert Watson KASSERT(mtp->ks_magic == M_MAGIC, ("malloc: bad malloc type magic")); 493d3c11994SPoul-Henning Kamp /* 49423198357SRuslan Ermilov * Check that exactly one of M_WAITOK or M_NOWAIT is specified. 495d3c11994SPoul-Henning Kamp */ 49623198357SRuslan Ermilov indx = flags & (M_WAITOK | M_NOWAIT); 497d3c11994SPoul-Henning Kamp if (indx != M_NOWAIT && indx != M_WAITOK) { 498d3c11994SPoul-Henning Kamp static struct timeval lasterr; 499d3c11994SPoul-Henning Kamp static int curerr, once; 500d3c11994SPoul-Henning Kamp if (once == 0 && ppsratecheck(&lasterr, &curerr, 1)) { 501d3c11994SPoul-Henning Kamp printf("Bad malloc flags: %x\n", indx); 5022d50560aSMarcel Moolenaar kdb_backtrace(); 503d3c11994SPoul-Henning Kamp flags |= M_WAITOK; 504d3c11994SPoul-Henning Kamp once++; 505d3c11994SPoul-Henning Kamp } 506d3c11994SPoul-Henning Kamp } 507194a0abfSPoul-Henning Kamp #endif 508eae870cdSRobert Watson #ifdef MALLOC_MAKE_FAILURES 509eae870cdSRobert Watson if ((flags & M_NOWAIT) && (malloc_failure_rate != 0)) { 510eae870cdSRobert Watson atomic_add_int(&malloc_nowait_count, 1); 511eae870cdSRobert Watson if ((malloc_nowait_count % malloc_failure_rate) == 0) { 512eae870cdSRobert Watson atomic_add_int(&malloc_failure_count, 1); 5133f6ee876SPoul-Henning Kamp t_malloc_fail = time_uptime; 514ab3185d1SJeff Roberson *vap = NULL; 515ab3185d1SJeff Roberson return (EJUSTRETURN); 516eae870cdSRobert Watson } 517eae870cdSRobert Watson } 518eae870cdSRobert Watson #endif 51906bf2a6aSMatt Macy if (flags & M_WAITOK) { 520b40ce416SJulian Elischer KASSERT(curthread->td_intr_nesting_level == 0, 521a163d034SWarner Losh ("malloc(M_WAITOK) in interrupt context")); 52206bf2a6aSMatt Macy KASSERT(curthread->td_epochnest == 0, 52306bf2a6aSMatt Macy ("malloc(M_WAITOK) in epoch context")); 52406bf2a6aSMatt Macy } 525d9e2e68dSMark Johnston KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 5261067a2baSJonathan T. Looney ("malloc: called with spinlock or critical section held")); 5271067a2baSJonathan T. Looney 528e4eb384bSBosko Milekic #ifdef DEBUG_MEMGUARD 529ab3185d1SJeff Roberson if (memguard_cmp_mtp(mtp, *sizep)) { 530ab3185d1SJeff Roberson *vap = memguard_alloc(*sizep, flags); 531ab3185d1SJeff Roberson if (*vap != NULL) 532ab3185d1SJeff Roberson return (EJUSTRETURN); 533e3813573SMatthew D Fleming /* This is unfortunate but should not be fatal. */ 534e3813573SMatthew D Fleming } 535e4eb384bSBosko Milekic #endif 536e4eb384bSBosko Milekic 537847a2a17SPawel Jakub Dawidek #ifdef DEBUG_REDZONE 538ab3185d1SJeff Roberson *sizep = redzone_size_ntor(*sizep); 539ab3185d1SJeff Roberson #endif 540ab3185d1SJeff Roberson 541ab3185d1SJeff Roberson return (0); 542ab3185d1SJeff Roberson } 543ab3185d1SJeff Roberson #endif 544ab3185d1SJeff Roberson 545ab3185d1SJeff Roberson /* 546ab3185d1SJeff Roberson * malloc: 547ab3185d1SJeff Roberson * 548ab3185d1SJeff Roberson * Allocate a block of memory. 549ab3185d1SJeff Roberson * 550ab3185d1SJeff Roberson * If M_NOWAIT is set, this routine will not block and return NULL if 551ab3185d1SJeff Roberson * the allocation fails. 552ab3185d1SJeff Roberson */ 553ab3185d1SJeff Roberson void * 55434c538c3SMateusz Guzik (malloc)(size_t size, struct malloc_type *mtp, int flags) 555ab3185d1SJeff Roberson { 556ab3185d1SJeff Roberson int indx; 557ab3185d1SJeff Roberson caddr_t va; 558ab3185d1SJeff Roberson uma_zone_t zone; 559ab3185d1SJeff Roberson #if defined(DEBUG_REDZONE) 560ab3185d1SJeff Roberson unsigned long osize = size; 561ab3185d1SJeff Roberson #endif 562ab3185d1SJeff Roberson 563ab3185d1SJeff Roberson #ifdef MALLOC_DEBUG 5645072a5f4SMatt Macy va = NULL; 565ab3185d1SJeff Roberson if (malloc_dbg(&va, &size, mtp, flags) != 0) 566ab3185d1SJeff Roberson return (va); 567847a2a17SPawel Jakub Dawidek #endif 568847a2a17SPawel Jakub Dawidek 5690766f278SJonathan T. Looney if (size <= kmem_zmax && (flags & M_EXEC) == 0) { 5706f267175SJeff Roberson if (size & KMEM_ZMASK) 5716f267175SJeff Roberson size = (size & ~KMEM_ZMASK) + KMEM_ZBASE; 5726f267175SJeff Roberson indx = kmemsize[size >> KMEM_ZSHIFT]; 573c9e05ccdSMateusz Guzik zone = kmemzones[indx].kz_zone[mtp_get_subzone(mtp)]; 5746f267175SJeff Roberson #ifdef MALLOC_PROFILE 5756f267175SJeff Roberson krequests[size >> KMEM_ZSHIFT]++; 5766f267175SJeff Roberson #endif 5778355f576SJeff Roberson va = uma_zalloc(zone, flags); 5784362fadaSBrian Feldman if (va != NULL) 579e20a199fSJeff Roberson size = zone->uz_size; 58063a7e0a3SRobert Watson malloc_type_zone_allocated(mtp, va == NULL ? 0 : size, indx); 5818355f576SJeff Roberson } else { 5826f267175SJeff Roberson size = roundup(size, PAGE_SIZE); 5838355f576SJeff Roberson zone = NULL; 5848355f576SJeff Roberson va = uma_large_malloc(size, flags); 58563a7e0a3SRobert Watson malloc_type_allocated(mtp, va == NULL ? 0 : size); 586df8bae1dSRodney W. Grimes } 5871282e9acSPoul-Henning Kamp if (flags & M_WAITOK) 588a163d034SWarner Losh KASSERT(va != NULL, ("malloc(M_WAITOK) returned NULL")); 5891282e9acSPoul-Henning Kamp else if (va == NULL) 5901fb14a47SPoul-Henning Kamp t_malloc_fail = time_uptime; 591ab3185d1SJeff Roberson #ifdef DEBUG_REDZONE 592ab3185d1SJeff Roberson if (va != NULL) 593ab3185d1SJeff Roberson va = redzone_setup(va, osize); 5944db4f5c8SPoul-Henning Kamp #endif 595ab3185d1SJeff Roberson return ((void *) va); 596ab3185d1SJeff Roberson } 597ab3185d1SJeff Roberson 598ab3185d1SJeff Roberson void * 599bd555da9SConrad Meyer malloc_domain(size_t size, struct malloc_type *mtp, int domain, 600ab3185d1SJeff Roberson int flags) 601ab3185d1SJeff Roberson { 602ab3185d1SJeff Roberson int indx; 603ab3185d1SJeff Roberson caddr_t va; 604ab3185d1SJeff Roberson uma_zone_t zone; 605ab3185d1SJeff Roberson #if defined(DEBUG_REDZONE) 606ab3185d1SJeff Roberson unsigned long osize = size; 607ab3185d1SJeff Roberson #endif 608ab3185d1SJeff Roberson 609ab3185d1SJeff Roberson #ifdef MALLOC_DEBUG 6105072a5f4SMatt Macy va = NULL; 611ab3185d1SJeff Roberson if (malloc_dbg(&va, &size, mtp, flags) != 0) 612ab3185d1SJeff Roberson return (va); 613ab3185d1SJeff Roberson #endif 6140766f278SJonathan T. Looney if (size <= kmem_zmax && (flags & M_EXEC) == 0) { 615ab3185d1SJeff Roberson if (size & KMEM_ZMASK) 616ab3185d1SJeff Roberson size = (size & ~KMEM_ZMASK) + KMEM_ZBASE; 617ab3185d1SJeff Roberson indx = kmemsize[size >> KMEM_ZSHIFT]; 618c9e05ccdSMateusz Guzik zone = kmemzones[indx].kz_zone[mtp_get_subzone(mtp)]; 619ab3185d1SJeff Roberson #ifdef MALLOC_PROFILE 620ab3185d1SJeff Roberson krequests[size >> KMEM_ZSHIFT]++; 621ab3185d1SJeff Roberson #endif 622ab3185d1SJeff Roberson va = uma_zalloc_domain(zone, NULL, domain, flags); 623ab3185d1SJeff Roberson if (va != NULL) 624ab3185d1SJeff Roberson size = zone->uz_size; 625ab3185d1SJeff Roberson malloc_type_zone_allocated(mtp, va == NULL ? 0 : size, indx); 626ab3185d1SJeff Roberson } else { 627ab3185d1SJeff Roberson size = roundup(size, PAGE_SIZE); 628ab3185d1SJeff Roberson zone = NULL; 629ab3185d1SJeff Roberson va = uma_large_malloc_domain(size, domain, flags); 630ab3185d1SJeff Roberson malloc_type_allocated(mtp, va == NULL ? 0 : size); 631ab3185d1SJeff Roberson } 632ab3185d1SJeff Roberson if (flags & M_WAITOK) 633ab3185d1SJeff Roberson KASSERT(va != NULL, ("malloc(M_WAITOK) returned NULL")); 634ab3185d1SJeff Roberson else if (va == NULL) 635ab3185d1SJeff Roberson t_malloc_fail = time_uptime; 636847a2a17SPawel Jakub Dawidek #ifdef DEBUG_REDZONE 637847a2a17SPawel Jakub Dawidek if (va != NULL) 638847a2a17SPawel Jakub Dawidek va = redzone_setup(va, osize); 639847a2a17SPawel Jakub Dawidek #endif 640df8bae1dSRodney W. Grimes return ((void *) va); 641df8bae1dSRodney W. Grimes } 642df8bae1dSRodney W. Grimes 643fd91e076SKristof Provost void * 644fd91e076SKristof Provost mallocarray(size_t nmemb, size_t size, struct malloc_type *type, int flags) 645fd91e076SKristof Provost { 646fd91e076SKristof Provost 647c02fc960SConrad Meyer if (WOULD_OVERFLOW(nmemb, size)) 648c02fc960SConrad Meyer panic("mallocarray: %zu * %zu overflowed", nmemb, size); 649fd91e076SKristof Provost 650fd91e076SKristof Provost return (malloc(size * nmemb, type, flags)); 651fd91e076SKristof Provost } 652fd91e076SKristof Provost 653ab3185d1SJeff Roberson #ifdef INVARIANTS 654ab3185d1SJeff Roberson static void 655ab3185d1SJeff Roberson free_save_type(void *addr, struct malloc_type *mtp, u_long size) 656ab3185d1SJeff Roberson { 657ab3185d1SJeff Roberson struct malloc_type **mtpp = addr; 658ab3185d1SJeff Roberson 659ab3185d1SJeff Roberson /* 660ab3185d1SJeff Roberson * Cache a pointer to the malloc_type that most recently freed 661ab3185d1SJeff Roberson * this memory here. This way we know who is most likely to 662ab3185d1SJeff Roberson * have stepped on it later. 663ab3185d1SJeff Roberson * 664ab3185d1SJeff Roberson * This code assumes that size is a multiple of 8 bytes for 665ab3185d1SJeff Roberson * 64 bit machines 666ab3185d1SJeff Roberson */ 667ab3185d1SJeff Roberson mtpp = (struct malloc_type **) ((unsigned long)mtpp & ~UMA_ALIGN_PTR); 668ab3185d1SJeff Roberson mtpp += (size - sizeof(struct malloc_type *)) / 669ab3185d1SJeff Roberson sizeof(struct malloc_type *); 670ab3185d1SJeff Roberson *mtpp = mtp; 671ab3185d1SJeff Roberson } 672ab3185d1SJeff Roberson #endif 673ab3185d1SJeff Roberson 674ab3185d1SJeff Roberson #ifdef MALLOC_DEBUG 675ab3185d1SJeff Roberson static int 676ab3185d1SJeff Roberson free_dbg(void **addrp, struct malloc_type *mtp) 677ab3185d1SJeff Roberson { 678ab3185d1SJeff Roberson void *addr; 679ab3185d1SJeff Roberson 680ab3185d1SJeff Roberson addr = *addrp; 681ab3185d1SJeff Roberson KASSERT(mtp->ks_magic == M_MAGIC, ("free: bad malloc type magic")); 682ab3185d1SJeff Roberson KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 683ab3185d1SJeff Roberson ("free: called with spinlock or critical section held")); 684ab3185d1SJeff Roberson 685ab3185d1SJeff Roberson /* free(NULL, ...) does nothing */ 686ab3185d1SJeff Roberson if (addr == NULL) 687ab3185d1SJeff Roberson return (EJUSTRETURN); 688ab3185d1SJeff Roberson 689ab3185d1SJeff Roberson #ifdef DEBUG_MEMGUARD 690ab3185d1SJeff Roberson if (is_memguard_addr(addr)) { 691ab3185d1SJeff Roberson memguard_free(addr); 692ab3185d1SJeff Roberson return (EJUSTRETURN); 693ab3185d1SJeff Roberson } 694ab3185d1SJeff Roberson #endif 695ab3185d1SJeff Roberson 696ab3185d1SJeff Roberson #ifdef DEBUG_REDZONE 697ab3185d1SJeff Roberson redzone_check(addr); 698ab3185d1SJeff Roberson *addrp = redzone_addr_ntor(addr); 699ab3185d1SJeff Roberson #endif 700ab3185d1SJeff Roberson 701ab3185d1SJeff Roberson return (0); 702ab3185d1SJeff Roberson } 703ab3185d1SJeff Roberson #endif 704ab3185d1SJeff Roberson 705fd91e076SKristof Provost /* 7061c7c3c6aSMatthew Dillon * free: 7071c7c3c6aSMatthew Dillon * 708df8bae1dSRodney W. Grimes * Free a block of memory allocated by malloc. 7091c7c3c6aSMatthew Dillon * 7101c7c3c6aSMatthew Dillon * This routine may not block. 711df8bae1dSRodney W. Grimes */ 712df8bae1dSRodney W. Grimes void 71363a7e0a3SRobert Watson free(void *addr, struct malloc_type *mtp) 714df8bae1dSRodney W. Grimes { 71599571dc3SJeff Roberson uma_slab_t slab; 71699571dc3SJeff Roberson u_long size; 717254c6cb3SPoul-Henning Kamp 718ab3185d1SJeff Roberson #ifdef MALLOC_DEBUG 719ab3185d1SJeff Roberson if (free_dbg(&addr, mtp) != 0) 720ab3185d1SJeff Roberson return; 721ab3185d1SJeff Roberson #endif 72244a8ff31SArchie Cobbs /* free(NULL, ...) does nothing */ 72344a8ff31SArchie Cobbs if (addr == NULL) 72444a8ff31SArchie Cobbs return; 72544a8ff31SArchie Cobbs 72699571dc3SJeff Roberson slab = vtoslab((vm_offset_t)addr & (~UMA_SLAB_MASK)); 7278355f576SJeff Roberson if (slab == NULL) 7286f267175SJeff Roberson panic("free: address %p(%p) has not been allocated.\n", 72999571dc3SJeff Roberson addr, (void *)((u_long)addr & (~UMA_SLAB_MASK))); 73099571dc3SJeff Roberson 7318355f576SJeff Roberson if (!(slab->us_flags & UMA_SLAB_MALLOC)) { 732099a0e58SBosko Milekic size = slab->us_keg->uk_size; 7338f70816cSJeff Roberson #ifdef INVARIANTS 734ab3185d1SJeff Roberson free_save_type(addr, mtp, size); 7358f70816cSJeff Roberson #endif 736099a0e58SBosko Milekic uma_zfree_arg(LIST_FIRST(&slab->us_keg->uk_zones), addr, slab); 73714bf02f8SJohn Dyson } else { 7388355f576SJeff Roberson size = slab->us_size; 7398355f576SJeff Roberson uma_large_free(slab); 74014bf02f8SJohn Dyson } 74163a7e0a3SRobert Watson malloc_type_freed(mtp, size); 742df8bae1dSRodney W. Grimes } 743df8bae1dSRodney W. Grimes 744ab3185d1SJeff Roberson void 745ab3185d1SJeff Roberson free_domain(void *addr, struct malloc_type *mtp) 746ab3185d1SJeff Roberson { 747ab3185d1SJeff Roberson uma_slab_t slab; 748ab3185d1SJeff Roberson u_long size; 749ab3185d1SJeff Roberson 750ab3185d1SJeff Roberson #ifdef MALLOC_DEBUG 751ab3185d1SJeff Roberson if (free_dbg(&addr, mtp) != 0) 752ab3185d1SJeff Roberson return; 753ab3185d1SJeff Roberson #endif 754ab3185d1SJeff Roberson 755ab3185d1SJeff Roberson /* free(NULL, ...) does nothing */ 756ab3185d1SJeff Roberson if (addr == NULL) 757ab3185d1SJeff Roberson return; 758ab3185d1SJeff Roberson 759ab3185d1SJeff Roberson slab = vtoslab((vm_offset_t)addr & (~UMA_SLAB_MASK)); 760ab3185d1SJeff Roberson if (slab == NULL) 761ab3185d1SJeff Roberson panic("free_domain: address %p(%p) has not been allocated.\n", 762ab3185d1SJeff Roberson addr, (void *)((u_long)addr & (~UMA_SLAB_MASK))); 763ab3185d1SJeff Roberson 764ab3185d1SJeff Roberson if (!(slab->us_flags & UMA_SLAB_MALLOC)) { 765ab3185d1SJeff Roberson size = slab->us_keg->uk_size; 766ab3185d1SJeff Roberson #ifdef INVARIANTS 767ab3185d1SJeff Roberson free_save_type(addr, mtp, size); 768ab3185d1SJeff Roberson #endif 769ab3185d1SJeff Roberson uma_zfree_domain(LIST_FIRST(&slab->us_keg->uk_zones), 770ab3185d1SJeff Roberson addr, slab); 771ab3185d1SJeff Roberson } else { 772ab3185d1SJeff Roberson size = slab->us_size; 773ab3185d1SJeff Roberson uma_large_free(slab); 774ab3185d1SJeff Roberson } 775ab3185d1SJeff Roberson malloc_type_freed(mtp, size); 776ab3185d1SJeff Roberson } 777ab3185d1SJeff Roberson 778df8bae1dSRodney W. Grimes /* 77944a8ff31SArchie Cobbs * realloc: change the size of a memory block 78044a8ff31SArchie Cobbs */ 78144a8ff31SArchie Cobbs void * 782bd555da9SConrad Meyer realloc(void *addr, size_t size, struct malloc_type *mtp, int flags) 78344a8ff31SArchie Cobbs { 7848355f576SJeff Roberson uma_slab_t slab; 78544a8ff31SArchie Cobbs unsigned long alloc; 78644a8ff31SArchie Cobbs void *newaddr; 78744a8ff31SArchie Cobbs 788bb1c7df8SRobert Watson KASSERT(mtp->ks_magic == M_MAGIC, 789bb1c7df8SRobert Watson ("realloc: bad malloc type magic")); 790d9e2e68dSMark Johnston KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 7911067a2baSJonathan T. Looney ("realloc: called with spinlock or critical section held")); 7921067a2baSJonathan T. Looney 79344a8ff31SArchie Cobbs /* realloc(NULL, ...) is equivalent to malloc(...) */ 79444a8ff31SArchie Cobbs if (addr == NULL) 79563a7e0a3SRobert Watson return (malloc(size, mtp, flags)); 79663a7e0a3SRobert Watson 79763a7e0a3SRobert Watson /* 79863a7e0a3SRobert Watson * XXX: Should report free of old memory and alloc of new memory to 79963a7e0a3SRobert Watson * per-CPU stats. 80063a7e0a3SRobert Watson */ 80144a8ff31SArchie Cobbs 802e4eb384bSBosko Milekic #ifdef DEBUG_MEMGUARD 8036d3ed393SMatthew D Fleming if (is_memguard_addr(addr)) 8046d3ed393SMatthew D Fleming return (memguard_realloc(addr, size, mtp, flags)); 805e4eb384bSBosko Milekic #endif 806e4eb384bSBosko Milekic 807847a2a17SPawel Jakub Dawidek #ifdef DEBUG_REDZONE 808847a2a17SPawel Jakub Dawidek slab = NULL; 809847a2a17SPawel Jakub Dawidek alloc = redzone_get_size(addr); 810847a2a17SPawel Jakub Dawidek #else 81199571dc3SJeff Roberson slab = vtoslab((vm_offset_t)addr & ~(UMA_SLAB_MASK)); 8128355f576SJeff Roberson 81344a8ff31SArchie Cobbs /* Sanity check */ 8148355f576SJeff Roberson KASSERT(slab != NULL, 81544a8ff31SArchie Cobbs ("realloc: address %p out of range", (void *)addr)); 81644a8ff31SArchie Cobbs 81744a8ff31SArchie Cobbs /* Get the size of the original block */ 818619f2841SPawel Jakub Dawidek if (!(slab->us_flags & UMA_SLAB_MALLOC)) 819099a0e58SBosko Milekic alloc = slab->us_keg->uk_size; 8208355f576SJeff Roberson else 8218355f576SJeff Roberson alloc = slab->us_size; 82244a8ff31SArchie Cobbs 82344a8ff31SArchie Cobbs /* Reuse the original block if appropriate */ 82444a8ff31SArchie Cobbs if (size <= alloc 82544a8ff31SArchie Cobbs && (size > (alloc >> REALLOC_FRACTION) || alloc == MINALLOCSIZE)) 82644a8ff31SArchie Cobbs return (addr); 827847a2a17SPawel Jakub Dawidek #endif /* !DEBUG_REDZONE */ 82844a8ff31SArchie Cobbs 82944a8ff31SArchie Cobbs /* Allocate a new, bigger (or smaller) block */ 83063a7e0a3SRobert Watson if ((newaddr = malloc(size, mtp, flags)) == NULL) 83144a8ff31SArchie Cobbs return (NULL); 83244a8ff31SArchie Cobbs 83344a8ff31SArchie Cobbs /* Copy over original contents */ 83444a8ff31SArchie Cobbs bcopy(addr, newaddr, min(size, alloc)); 83563a7e0a3SRobert Watson free(addr, mtp); 83644a8ff31SArchie Cobbs return (newaddr); 83744a8ff31SArchie Cobbs } 83844a8ff31SArchie Cobbs 83944a8ff31SArchie Cobbs /* 84044a8ff31SArchie Cobbs * reallocf: same as realloc() but free memory on failure. 84144a8ff31SArchie Cobbs */ 84244a8ff31SArchie Cobbs void * 843bd555da9SConrad Meyer reallocf(void *addr, size_t size, struct malloc_type *mtp, int flags) 84444a8ff31SArchie Cobbs { 84544a8ff31SArchie Cobbs void *mem; 84644a8ff31SArchie Cobbs 84763a7e0a3SRobert Watson if ((mem = realloc(addr, size, mtp, flags)) == NULL) 84863a7e0a3SRobert Watson free(addr, mtp); 84944a8ff31SArchie Cobbs return (mem); 85044a8ff31SArchie Cobbs } 85144a8ff31SArchie Cobbs 852f9d498adSDimitry Andric #ifndef __sparc64__ 853c70af487SAlan Cox CTASSERT(VM_KMEM_SIZE_SCALE >= 1); 854f9d498adSDimitry Andric #endif 855c70af487SAlan Cox 8565df87b21SJeff Roberson /* 857c70af487SAlan Cox * Initialize the kernel memory (kmem) arena. 8585df87b21SJeff Roberson */ 8595df87b21SJeff Roberson void 8605df87b21SJeff Roberson kmeminit(void) 8615df87b21SJeff Roberson { 862af3b2549SHans Petter Selasky u_long mem_size; 863af3b2549SHans Petter Selasky u_long tmp; 86469ef67f9SJason Evans 865af3b2549SHans Petter Selasky #ifdef VM_KMEM_SIZE 866af3b2549SHans Petter Selasky if (vm_kmem_size == 0) 867af3b2549SHans Petter Selasky vm_kmem_size = VM_KMEM_SIZE; 868af3b2549SHans Petter Selasky #endif 869af3b2549SHans Petter Selasky #ifdef VM_KMEM_SIZE_MIN 870af3b2549SHans Petter Selasky if (vm_kmem_size_min == 0) 871af3b2549SHans Petter Selasky vm_kmem_size_min = VM_KMEM_SIZE_MIN; 872af3b2549SHans Petter Selasky #endif 873af3b2549SHans Petter Selasky #ifdef VM_KMEM_SIZE_MAX 874af3b2549SHans Petter Selasky if (vm_kmem_size_max == 0) 875af3b2549SHans Petter Selasky vm_kmem_size_max = VM_KMEM_SIZE_MAX; 876af3b2549SHans Petter Selasky #endif 8778a58a9f6SJohn Dyson /* 878c70af487SAlan Cox * Calculate the amount of kernel virtual address (KVA) space that is 879c70af487SAlan Cox * preallocated to the kmem arena. In order to support a wide range 880c70af487SAlan Cox * of machines, it is a function of the physical memory size, 881c70af487SAlan Cox * specifically, 8828a58a9f6SJohn Dyson * 883c70af487SAlan Cox * min(max(physical memory size / VM_KMEM_SIZE_SCALE, 884c70af487SAlan Cox * VM_KMEM_SIZE_MIN), VM_KMEM_SIZE_MAX) 885c70af487SAlan Cox * 886c70af487SAlan Cox * Every architecture must define an integral value for 887c70af487SAlan Cox * VM_KMEM_SIZE_SCALE. However, the definitions of VM_KMEM_SIZE_MIN 888c70af487SAlan Cox * and VM_KMEM_SIZE_MAX, which represent respectively the floor and 889c70af487SAlan Cox * ceiling on this preallocation, are optional. Typically, 890c70af487SAlan Cox * VM_KMEM_SIZE_MAX is itself a function of the available KVA space on 891c70af487SAlan Cox * a given architecture. 8928a58a9f6SJohn Dyson */ 89344f1c916SBryan Drewery mem_size = vm_cnt.v_page_count; 8947c51714eSSean Bruno if (mem_size <= 32768) /* delphij XXX 128MB */ 8957c51714eSSean Bruno kmem_zmax = PAGE_SIZE; 8968a58a9f6SJohn Dyson 897c70af487SAlan Cox if (vm_kmem_size_scale < 1) 898c70af487SAlan Cox vm_kmem_size_scale = VM_KMEM_SIZE_SCALE; 899c70af487SAlan Cox 900af3b2549SHans Petter Selasky /* 901af3b2549SHans Petter Selasky * Check if we should use defaults for the "vm_kmem_size" 902af3b2549SHans Petter Selasky * variable: 903af3b2549SHans Petter Selasky */ 904af3b2549SHans Petter Selasky if (vm_kmem_size == 0) { 905479439b4SDag-Erling Smørgrav vm_kmem_size = (mem_size / vm_kmem_size_scale) * PAGE_SIZE; 9068a58a9f6SJohn Dyson 907c70af487SAlan Cox if (vm_kmem_size_min > 0 && vm_kmem_size < vm_kmem_size_min) 9080e5179e4SStephane E. Potvin vm_kmem_size = vm_kmem_size_min; 909479439b4SDag-Erling Smørgrav if (vm_kmem_size_max > 0 && vm_kmem_size >= vm_kmem_size_max) 910479439b4SDag-Erling Smørgrav vm_kmem_size = vm_kmem_size_max; 911af3b2549SHans Petter Selasky } 9128a58a9f6SJohn Dyson 91327b8623fSDavid Greenman /* 914af3b2549SHans Petter Selasky * The amount of KVA space that is preallocated to the 915c70af487SAlan Cox * kmem arena can be set statically at compile-time or manually 916c70af487SAlan Cox * through the kernel environment. However, it is still limited to 917c70af487SAlan Cox * twice the physical memory size, which has been sufficient to handle 918c70af487SAlan Cox * the most severe cases of external fragmentation in the kmem arena. 91927b8623fSDavid Greenman */ 920c749c003SAlan Cox if (vm_kmem_size / 2 / PAGE_SIZE > mem_size) 921c749c003SAlan Cox vm_kmem_size = 2 * mem_size * PAGE_SIZE; 9228a58a9f6SJohn Dyson 923e137643eSOlivier Houchard vm_kmem_size = round_page(vm_kmem_size); 924e3813573SMatthew D Fleming #ifdef DEBUG_MEMGUARD 925f806cdcfSMatthew D Fleming tmp = memguard_fudge(vm_kmem_size, kernel_map); 926e3813573SMatthew D Fleming #else 927e3813573SMatthew D Fleming tmp = vm_kmem_size; 928e3813573SMatthew D Fleming #endif 9292e47807cSJeff Roberson uma_set_limit(tmp); 9308355f576SJeff Roberson 931e4eb384bSBosko Milekic #ifdef DEBUG_MEMGUARD 932e4eb384bSBosko Milekic /* 933e4eb384bSBosko Milekic * Initialize MemGuard if support compiled in. MemGuard is a 934e4eb384bSBosko Milekic * replacement allocator used for detecting tamper-after-free 935e4eb384bSBosko Milekic * scenarios as they occur. It is only used for debugging. 936e4eb384bSBosko Milekic */ 9372e47807cSJeff Roberson memguard_init(kernel_arena); 938e4eb384bSBosko Milekic #endif 9395df87b21SJeff Roberson } 9405df87b21SJeff Roberson 9415df87b21SJeff Roberson /* 9425df87b21SJeff Roberson * Initialize the kernel memory allocator 9435df87b21SJeff Roberson */ 9445df87b21SJeff Roberson /* ARGSUSED*/ 9455df87b21SJeff Roberson static void 9465df87b21SJeff Roberson mallocinit(void *dummy) 9475df87b21SJeff Roberson { 9485df87b21SJeff Roberson int i; 9495df87b21SJeff Roberson uint8_t indx; 9505df87b21SJeff Roberson 9515df87b21SJeff Roberson mtx_init(&malloc_mtx, "malloc", NULL, MTX_DEF); 9525df87b21SJeff Roberson 9535df87b21SJeff Roberson kmeminit(); 954e4eb384bSBosko Milekic 9557001d850SXin LI if (kmem_zmax < PAGE_SIZE || kmem_zmax > KMEM_ZMAX) 9567001d850SXin LI kmem_zmax = KMEM_ZMAX; 9577001d850SXin LI 958*9afff6b1SMateusz Guzik mt_stats_zone = uma_zcreate("mt_stats_zone", 959*9afff6b1SMateusz Guzik sizeof(struct malloc_type_stats), NULL, NULL, NULL, NULL, 960*9afff6b1SMateusz Guzik UMA_ALIGN_PTR, UMA_ZONE_PCPU); 96163a7e0a3SRobert Watson mt_zone = uma_zcreate("mt_zone", sizeof(struct malloc_type_internal), 96263a7e0a3SRobert Watson #ifdef INVARIANTS 96363a7e0a3SRobert Watson mtrash_ctor, mtrash_dtor, mtrash_init, mtrash_fini, 96463a7e0a3SRobert Watson #else 96563a7e0a3SRobert Watson NULL, NULL, NULL, NULL, 96663a7e0a3SRobert Watson #endif 96763a7e0a3SRobert Watson UMA_ALIGN_PTR, UMA_ZONE_MALLOC); 9686f267175SJeff Roberson for (i = 0, indx = 0; kmemzones[indx].kz_size != 0; indx++) { 9696f267175SJeff Roberson int size = kmemzones[indx].kz_size; 9706f267175SJeff Roberson char *name = kmemzones[indx].kz_name; 971d7854da1SMatthew D Fleming int subzone; 9728355f576SJeff Roberson 973d7854da1SMatthew D Fleming for (subzone = 0; subzone < numzones; subzone++) { 974d7854da1SMatthew D Fleming kmemzones[indx].kz_zone[subzone] = 975d7854da1SMatthew D Fleming uma_zcreate(name, size, 9768efc4effSJeff Roberson #ifdef INVARIANTS 9778f70816cSJeff Roberson mtrash_ctor, mtrash_dtor, mtrash_init, mtrash_fini, 9788efc4effSJeff Roberson #else 9798efc4effSJeff Roberson NULL, NULL, NULL, NULL, 9808efc4effSJeff Roberson #endif 9818efc4effSJeff Roberson UMA_ALIGN_PTR, UMA_ZONE_MALLOC); 982d7854da1SMatthew D Fleming } 9838355f576SJeff Roberson for (;i <= size; i+= KMEM_ZBASE) 9846f267175SJeff Roberson kmemsize[i >> KMEM_ZSHIFT] = indx; 9858355f576SJeff Roberson 986df8bae1dSRodney W. Grimes } 987254c6cb3SPoul-Henning Kamp } 988af3b2549SHans Petter Selasky SYSINIT(kmem, SI_SUB_KMEM, SI_ORDER_SECOND, mallocinit, NULL); 989254c6cb3SPoul-Henning Kamp 990db669378SPeter Wemm void 99187efd4d5SRobert Watson malloc_init(void *data) 992254c6cb3SPoul-Henning Kamp { 99363a7e0a3SRobert Watson struct malloc_type_internal *mtip; 99463a7e0a3SRobert Watson struct malloc_type *mtp; 99563a7e0a3SRobert Watson 99644f1c916SBryan Drewery KASSERT(vm_cnt.v_page_count != 0, ("malloc_register before vm_init")); 99763a7e0a3SRobert Watson 99863a7e0a3SRobert Watson mtp = data; 999f121baaaSBrian Somers if (mtp->ks_magic != M_MAGIC) 1000f121baaaSBrian Somers panic("malloc_init: bad malloc type magic"); 1001bb1c7df8SRobert Watson 100263a7e0a3SRobert Watson mtip = uma_zalloc(mt_zone, M_WAITOK | M_ZERO); 1003*9afff6b1SMateusz Guzik mtip->mti_stats = uma_zalloc_pcpu(mt_stats_zone, M_WAITOK | M_ZERO); 100463a7e0a3SRobert Watson mtp->ks_handle = mtip; 1005c9e05ccdSMateusz Guzik mtp_set_subzone(mtp); 1006254c6cb3SPoul-Henning Kamp 10076f267175SJeff Roberson mtx_lock(&malloc_mtx); 100863a7e0a3SRobert Watson mtp->ks_next = kmemstatistics; 100963a7e0a3SRobert Watson kmemstatistics = mtp; 1010cd814b26SRobert Watson kmemcount++; 10116f267175SJeff Roberson mtx_unlock(&malloc_mtx); 1012df8bae1dSRodney W. Grimes } 1013db669378SPeter Wemm 1014db669378SPeter Wemm void 101587efd4d5SRobert Watson malloc_uninit(void *data) 1016db669378SPeter Wemm { 101763a7e0a3SRobert Watson struct malloc_type_internal *mtip; 10182a143d5bSPawel Jakub Dawidek struct malloc_type_stats *mtsp; 101963a7e0a3SRobert Watson struct malloc_type *mtp, *temp; 102045d48bdaSPaul Saab uma_slab_t slab; 10212a143d5bSPawel Jakub Dawidek long temp_allocs, temp_bytes; 10222a143d5bSPawel Jakub Dawidek int i; 1023db669378SPeter Wemm 102463a7e0a3SRobert Watson mtp = data; 1025bb1c7df8SRobert Watson KASSERT(mtp->ks_magic == M_MAGIC, 1026bb1c7df8SRobert Watson ("malloc_uninit: bad malloc type magic")); 102763a7e0a3SRobert Watson KASSERT(mtp->ks_handle != NULL, ("malloc_deregister: cookie NULL")); 1028bb1c7df8SRobert Watson 10296f267175SJeff Roberson mtx_lock(&malloc_mtx); 103063a7e0a3SRobert Watson mtip = mtp->ks_handle; 103163a7e0a3SRobert Watson mtp->ks_handle = NULL; 103263a7e0a3SRobert Watson if (mtp != kmemstatistics) { 103363a7e0a3SRobert Watson for (temp = kmemstatistics; temp != NULL; 103463a7e0a3SRobert Watson temp = temp->ks_next) { 1035f121baaaSBrian Somers if (temp->ks_next == mtp) { 103663a7e0a3SRobert Watson temp->ks_next = mtp->ks_next; 1037f121baaaSBrian Somers break; 1038db669378SPeter Wemm } 1039f121baaaSBrian Somers } 1040f121baaaSBrian Somers KASSERT(temp, 1041f121baaaSBrian Somers ("malloc_uninit: type '%s' not found", mtp->ks_shortdesc)); 104263a7e0a3SRobert Watson } else 104363a7e0a3SRobert Watson kmemstatistics = mtp->ks_next; 1044cd814b26SRobert Watson kmemcount--; 10456f267175SJeff Roberson mtx_unlock(&malloc_mtx); 10462a143d5bSPawel Jakub Dawidek 10472a143d5bSPawel Jakub Dawidek /* 10482a143d5bSPawel Jakub Dawidek * Look for memory leaks. 10492a143d5bSPawel Jakub Dawidek */ 10502a143d5bSPawel Jakub Dawidek temp_allocs = temp_bytes = 0; 1051*9afff6b1SMateusz Guzik for (i = 0; i <= mp_maxid; i++) { 1052*9afff6b1SMateusz Guzik mtsp = zpcpu_get_cpu(mtip->mti_stats, i); 10532a143d5bSPawel Jakub Dawidek temp_allocs += mtsp->mts_numallocs; 10542a143d5bSPawel Jakub Dawidek temp_allocs -= mtsp->mts_numfrees; 10552a143d5bSPawel Jakub Dawidek temp_bytes += mtsp->mts_memalloced; 10562a143d5bSPawel Jakub Dawidek temp_bytes -= mtsp->mts_memfreed; 10572a143d5bSPawel Jakub Dawidek } 10582a143d5bSPawel Jakub Dawidek if (temp_allocs > 0 || temp_bytes > 0) { 10592a143d5bSPawel Jakub Dawidek printf("Warning: memory type %s leaked memory on destroy " 10602a143d5bSPawel Jakub Dawidek "(%ld allocations, %ld bytes leaked).\n", mtp->ks_shortdesc, 10612a143d5bSPawel Jakub Dawidek temp_allocs, temp_bytes); 10622a143d5bSPawel Jakub Dawidek } 10632a143d5bSPawel Jakub Dawidek 106445d48bdaSPaul Saab slab = vtoslab((vm_offset_t) mtip & (~UMA_SLAB_MASK)); 1065*9afff6b1SMateusz Guzik uma_zfree_pcpu(mt_stats_zone, mtip->mti_stats); 106645d48bdaSPaul Saab uma_zfree_arg(mt_zone, mtip, slab); 1067db669378SPeter Wemm } 10686f267175SJeff Roberson 1069d362c40dSPawel Jakub Dawidek struct malloc_type * 1070d362c40dSPawel Jakub Dawidek malloc_desc2type(const char *desc) 1071d362c40dSPawel Jakub Dawidek { 1072d362c40dSPawel Jakub Dawidek struct malloc_type *mtp; 1073d362c40dSPawel Jakub Dawidek 1074d362c40dSPawel Jakub Dawidek mtx_assert(&malloc_mtx, MA_OWNED); 1075d362c40dSPawel Jakub Dawidek for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) { 1076d362c40dSPawel Jakub Dawidek if (strcmp(mtp->ks_shortdesc, desc) == 0) 1077d362c40dSPawel Jakub Dawidek return (mtp); 1078d362c40dSPawel Jakub Dawidek } 1079d362c40dSPawel Jakub Dawidek return (NULL); 1080d362c40dSPawel Jakub Dawidek } 1081d362c40dSPawel Jakub Dawidek 10826f267175SJeff Roberson static int 1083cd814b26SRobert Watson sysctl_kern_malloc_stats(SYSCTL_HANDLER_ARGS) 1084cd814b26SRobert Watson { 1085cd814b26SRobert Watson struct malloc_type_stream_header mtsh; 1086cd814b26SRobert Watson struct malloc_type_internal *mtip; 1087*9afff6b1SMateusz Guzik struct malloc_type_stats *mtsp, zeromts; 1088cd814b26SRobert Watson struct malloc_type_header mth; 1089cd814b26SRobert Watson struct malloc_type *mtp; 10904e657159SMatthew D Fleming int error, i; 1091cd814b26SRobert Watson struct sbuf sbuf; 1092cd814b26SRobert Watson 109300f0e671SMatthew D Fleming error = sysctl_wire_old_buffer(req, 0); 109400f0e671SMatthew D Fleming if (error != 0) 109500f0e671SMatthew D Fleming return (error); 10964e657159SMatthew D Fleming sbuf_new_for_sysctl(&sbuf, NULL, 128, req); 10971eafc078SIan Lepore sbuf_clear_flags(&sbuf, SBUF_INCLUDENUL); 1098cd814b26SRobert Watson mtx_lock(&malloc_mtx); 1099cd814b26SRobert Watson 1100*9afff6b1SMateusz Guzik bzero(&zeromts, sizeof(zeromts)); 1101*9afff6b1SMateusz Guzik 1102cd814b26SRobert Watson /* 1103cd814b26SRobert Watson * Insert stream header. 1104cd814b26SRobert Watson */ 1105cd814b26SRobert Watson bzero(&mtsh, sizeof(mtsh)); 1106cd814b26SRobert Watson mtsh.mtsh_version = MALLOC_TYPE_STREAM_VERSION; 1107cd814b26SRobert Watson mtsh.mtsh_maxcpus = MAXCPU; 1108cd814b26SRobert Watson mtsh.mtsh_count = kmemcount; 11094e657159SMatthew D Fleming (void)sbuf_bcat(&sbuf, &mtsh, sizeof(mtsh)); 1110cd814b26SRobert Watson 1111cd814b26SRobert Watson /* 1112cd814b26SRobert Watson * Insert alternating sequence of type headers and type statistics. 1113cd814b26SRobert Watson */ 1114cd814b26SRobert Watson for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) { 1115cd814b26SRobert Watson mtip = (struct malloc_type_internal *)mtp->ks_handle; 1116cd814b26SRobert Watson 1117cd814b26SRobert Watson /* 1118cd814b26SRobert Watson * Insert type header. 1119cd814b26SRobert Watson */ 1120cd814b26SRobert Watson bzero(&mth, sizeof(mth)); 1121cd814b26SRobert Watson strlcpy(mth.mth_name, mtp->ks_shortdesc, MALLOC_MAX_NAME); 11224e657159SMatthew D Fleming (void)sbuf_bcat(&sbuf, &mth, sizeof(mth)); 1123cd814b26SRobert Watson 1124cd814b26SRobert Watson /* 1125cd814b26SRobert Watson * Insert type statistics for each CPU. 1126cd814b26SRobert Watson */ 1127*9afff6b1SMateusz Guzik for (i = 0; i <= mp_maxid; i++) { 1128*9afff6b1SMateusz Guzik mtsp = zpcpu_get_cpu(mtip->mti_stats, i); 1129*9afff6b1SMateusz Guzik (void)sbuf_bcat(&sbuf, mtsp, sizeof(*mtsp)); 1130cd814b26SRobert Watson } 1131*9afff6b1SMateusz Guzik /* 1132*9afff6b1SMateusz Guzik * Fill in the missing CPUs. 1133*9afff6b1SMateusz Guzik */ 1134*9afff6b1SMateusz Guzik for (; i < MAXCPU; i++) { 1135*9afff6b1SMateusz Guzik (void)sbuf_bcat(&sbuf, &zeromts, sizeof(zeromts)); 1136*9afff6b1SMateusz Guzik } 1137*9afff6b1SMateusz Guzik 1138cd814b26SRobert Watson } 1139cd814b26SRobert Watson mtx_unlock(&malloc_mtx); 11404e657159SMatthew D Fleming error = sbuf_finish(&sbuf); 1141cd814b26SRobert Watson sbuf_delete(&sbuf); 1142cd814b26SRobert Watson return (error); 1143cd814b26SRobert Watson } 1144cd814b26SRobert Watson 1145cd814b26SRobert Watson SYSCTL_PROC(_kern, OID_AUTO, malloc_stats, CTLFLAG_RD|CTLTYPE_STRUCT, 1146cd814b26SRobert Watson 0, 0, sysctl_kern_malloc_stats, "s,malloc_type_ustats", 1147cd814b26SRobert Watson "Return malloc types"); 1148cd814b26SRobert Watson 1149cd814b26SRobert Watson SYSCTL_INT(_kern, OID_AUTO, malloc_count, CTLFLAG_RD, &kmemcount, 0, 1150cd814b26SRobert Watson "Count of kernel malloc types"); 1151cd814b26SRobert Watson 115291dd776cSJohn Birrell void 115391dd776cSJohn Birrell malloc_type_list(malloc_type_list_func_t *func, void *arg) 115491dd776cSJohn Birrell { 115591dd776cSJohn Birrell struct malloc_type *mtp, **bufmtp; 115691dd776cSJohn Birrell int count, i; 115791dd776cSJohn Birrell size_t buflen; 115891dd776cSJohn Birrell 115991dd776cSJohn Birrell mtx_lock(&malloc_mtx); 116091dd776cSJohn Birrell restart: 116191dd776cSJohn Birrell mtx_assert(&malloc_mtx, MA_OWNED); 116291dd776cSJohn Birrell count = kmemcount; 116391dd776cSJohn Birrell mtx_unlock(&malloc_mtx); 116491dd776cSJohn Birrell 116591dd776cSJohn Birrell buflen = sizeof(struct malloc_type *) * count; 116691dd776cSJohn Birrell bufmtp = malloc(buflen, M_TEMP, M_WAITOK); 116791dd776cSJohn Birrell 116891dd776cSJohn Birrell mtx_lock(&malloc_mtx); 116991dd776cSJohn Birrell 117091dd776cSJohn Birrell if (count < kmemcount) { 117191dd776cSJohn Birrell free(bufmtp, M_TEMP); 117291dd776cSJohn Birrell goto restart; 117391dd776cSJohn Birrell } 117491dd776cSJohn Birrell 117591dd776cSJohn Birrell for (mtp = kmemstatistics, i = 0; mtp != NULL; mtp = mtp->ks_next, i++) 117691dd776cSJohn Birrell bufmtp[i] = mtp; 117791dd776cSJohn Birrell 117891dd776cSJohn Birrell mtx_unlock(&malloc_mtx); 117991dd776cSJohn Birrell 118091dd776cSJohn Birrell for (i = 0; i < count; i++) 118191dd776cSJohn Birrell (func)(bufmtp[i], arg); 118291dd776cSJohn Birrell 118391dd776cSJohn Birrell free(bufmtp, M_TEMP); 118491dd776cSJohn Birrell } 118591dd776cSJohn Birrell 1186909ed16cSRobert Watson #ifdef DDB 1187909ed16cSRobert Watson DB_SHOW_COMMAND(malloc, db_show_malloc) 1188909ed16cSRobert Watson { 1189909ed16cSRobert Watson struct malloc_type_internal *mtip; 1190*9afff6b1SMateusz Guzik struct malloc_type_internal *mtsp; 1191909ed16cSRobert Watson struct malloc_type *mtp; 119260ae52f7SEd Schouten uint64_t allocs, frees; 119360ae52f7SEd Schouten uint64_t alloced, freed; 1194909ed16cSRobert Watson int i; 1195909ed16cSRobert Watson 119624076d13SRobert Watson db_printf("%18s %12s %12s %12s\n", "Type", "InUse", "MemUse", 119724076d13SRobert Watson "Requests"); 1198909ed16cSRobert Watson for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) { 1199909ed16cSRobert Watson mtip = (struct malloc_type_internal *)mtp->ks_handle; 1200909ed16cSRobert Watson allocs = 0; 1201909ed16cSRobert Watson frees = 0; 120224076d13SRobert Watson alloced = 0; 120324076d13SRobert Watson freed = 0; 1204*9afff6b1SMateusz Guzik for (i = 0; i <= mp_maxid; i++) { 1205*9afff6b1SMateusz Guzik mtsp = zpcpu_get_cpu(mtip->mti_stats, i); 1206909ed16cSRobert Watson allocs += mtip->mti_stats[i].mts_numallocs; 1207909ed16cSRobert Watson frees += mtip->mti_stats[i].mts_numfrees; 120824076d13SRobert Watson alloced += mtip->mti_stats[i].mts_memalloced; 120924076d13SRobert Watson freed += mtip->mti_stats[i].mts_memfreed; 1210909ed16cSRobert Watson } 121124076d13SRobert Watson db_printf("%18s %12ju %12juK %12ju\n", 121224076d13SRobert Watson mtp->ks_shortdesc, allocs - frees, 121324076d13SRobert Watson (alloced - freed + 1023) / 1024, allocs); 1214687c94aaSJohn Baldwin if (db_pager_quit) 1215687c94aaSJohn Baldwin break; 1216909ed16cSRobert Watson } 1217909ed16cSRobert Watson } 1218d7854da1SMatthew D Fleming 1219d7854da1SMatthew D Fleming #if MALLOC_DEBUG_MAXZONES > 1 1220d7854da1SMatthew D Fleming DB_SHOW_COMMAND(multizone_matches, db_show_multizone_matches) 1221d7854da1SMatthew D Fleming { 1222d7854da1SMatthew D Fleming struct malloc_type_internal *mtip; 1223d7854da1SMatthew D Fleming struct malloc_type *mtp; 1224d7854da1SMatthew D Fleming u_int subzone; 1225d7854da1SMatthew D Fleming 1226d7854da1SMatthew D Fleming if (!have_addr) { 1227d7854da1SMatthew D Fleming db_printf("Usage: show multizone_matches <malloc type/addr>\n"); 1228d7854da1SMatthew D Fleming return; 1229d7854da1SMatthew D Fleming } 1230d7854da1SMatthew D Fleming mtp = (void *)addr; 1231d7854da1SMatthew D Fleming if (mtp->ks_magic != M_MAGIC) { 1232d7854da1SMatthew D Fleming db_printf("Magic %lx does not match expected %x\n", 1233d7854da1SMatthew D Fleming mtp->ks_magic, M_MAGIC); 1234d7854da1SMatthew D Fleming return; 1235d7854da1SMatthew D Fleming } 1236d7854da1SMatthew D Fleming 1237d7854da1SMatthew D Fleming mtip = mtp->ks_handle; 1238d7854da1SMatthew D Fleming subzone = mtip->mti_zone; 1239d7854da1SMatthew D Fleming 1240d7854da1SMatthew D Fleming for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) { 1241d7854da1SMatthew D Fleming mtip = mtp->ks_handle; 1242d7854da1SMatthew D Fleming if (mtip->mti_zone != subzone) 1243d7854da1SMatthew D Fleming continue; 1244d7854da1SMatthew D Fleming db_printf("%s\n", mtp->ks_shortdesc); 1245687c94aaSJohn Baldwin if (db_pager_quit) 1246687c94aaSJohn Baldwin break; 1247d7854da1SMatthew D Fleming } 1248d7854da1SMatthew D Fleming } 1249d7854da1SMatthew D Fleming #endif /* MALLOC_DEBUG_MAXZONES > 1 */ 1250d7854da1SMatthew D Fleming #endif /* DDB */ 1251909ed16cSRobert Watson 12525e914b96SJeff Roberson #ifdef MALLOC_PROFILE 12535e914b96SJeff Roberson 12545e914b96SJeff Roberson static int 12555e914b96SJeff Roberson sysctl_kern_mprof(SYSCTL_HANDLER_ARGS) 12565e914b96SJeff Roberson { 125763a7e0a3SRobert Watson struct sbuf sbuf; 12585e914b96SJeff Roberson uint64_t count; 12595e914b96SJeff Roberson uint64_t waste; 12605e914b96SJeff Roberson uint64_t mem; 12615e914b96SJeff Roberson int error; 12625e914b96SJeff Roberson int rsize; 12635e914b96SJeff Roberson int size; 12645e914b96SJeff Roberson int i; 12655e914b96SJeff Roberson 12665e914b96SJeff Roberson waste = 0; 12675e914b96SJeff Roberson mem = 0; 12685e914b96SJeff Roberson 126900f0e671SMatthew D Fleming error = sysctl_wire_old_buffer(req, 0); 127000f0e671SMatthew D Fleming if (error != 0) 127100f0e671SMatthew D Fleming return (error); 12724e657159SMatthew D Fleming sbuf_new_for_sysctl(&sbuf, NULL, 128, req); 127363a7e0a3SRobert Watson sbuf_printf(&sbuf, 12745e914b96SJeff Roberson "\n Size Requests Real Size\n"); 12755e914b96SJeff Roberson for (i = 0; i < KMEM_ZSIZE; i++) { 12765e914b96SJeff Roberson size = i << KMEM_ZSHIFT; 12775e914b96SJeff Roberson rsize = kmemzones[kmemsize[i]].kz_size; 12785e914b96SJeff Roberson count = (long long unsigned)krequests[i]; 12795e914b96SJeff Roberson 128063a7e0a3SRobert Watson sbuf_printf(&sbuf, "%6d%28llu%11d\n", size, 128163a7e0a3SRobert Watson (unsigned long long)count, rsize); 12825e914b96SJeff Roberson 12835e914b96SJeff Roberson if ((rsize * count) > (size * count)) 12845e914b96SJeff Roberson waste += (rsize * count) - (size * count); 12855e914b96SJeff Roberson mem += (rsize * count); 12865e914b96SJeff Roberson } 128763a7e0a3SRobert Watson sbuf_printf(&sbuf, 12885e914b96SJeff Roberson "\nTotal memory used:\t%30llu\nTotal Memory wasted:\t%30llu\n", 12895e914b96SJeff Roberson (unsigned long long)mem, (unsigned long long)waste); 12904e657159SMatthew D Fleming error = sbuf_finish(&sbuf); 129163a7e0a3SRobert Watson sbuf_delete(&sbuf); 12925e914b96SJeff Roberson return (error); 12935e914b96SJeff Roberson } 12945e914b96SJeff Roberson 12955e914b96SJeff Roberson SYSCTL_OID(_kern, OID_AUTO, mprof, CTLTYPE_STRING|CTLFLAG_RD, 12965e914b96SJeff Roberson NULL, 0, sysctl_kern_mprof, "A", "Malloc Profiling"); 12975e914b96SJeff Roberson #endif /* MALLOC_PROFILE */ 1298