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> 636d6a03d7SJeff Roberson #include <sys/queue.h> 6463a7e0a3SRobert Watson #include <sys/sbuf.h> 659afff6b1SMateusz Guzik #include <sys/smp.h> 666f267175SJeff Roberson #include <sys/sysctl.h> 671fb14a47SPoul-Henning Kamp #include <sys/time.h> 685df87b21SJeff Roberson #include <sys/vmem.h> 694b25d1f2SGleb Smirnoff #ifdef EPOCH_TRACE 704b25d1f2SGleb Smirnoff #include <sys/epoch.h> 714b25d1f2SGleb Smirnoff #endif 729a02e8c6SJason Evans 73df8bae1dSRodney W. Grimes #include <vm/vm.h> 7499571dc3SJeff Roberson #include <vm/pmap.h> 759978bd99SMark Johnston #include <vm/vm_domainset.h> 765df87b21SJeff Roberson #include <vm/vm_pageout.h> 77efeaf95aSDavid Greenman #include <vm/vm_param.h> 78df8bae1dSRodney W. Grimes #include <vm/vm_kern.h> 79efeaf95aSDavid Greenman #include <vm/vm_extern.h> 803075778bSJohn Dyson #include <vm/vm_map.h> 8199571dc3SJeff Roberson #include <vm/vm_page.h> 826d6a03d7SJeff Roberson #include <vm/vm_phys.h> 836d6a03d7SJeff Roberson #include <vm/vm_pagequeue.h> 848355f576SJeff Roberson #include <vm/uma.h> 858355f576SJeff Roberson #include <vm/uma_int.h> 868efc4effSJeff Roberson #include <vm/uma_dbg.h> 87df8bae1dSRodney W. Grimes 88e4eb384bSBosko Milekic #ifdef DEBUG_MEMGUARD 89e4eb384bSBosko Milekic #include <vm/memguard.h> 90e4eb384bSBosko Milekic #endif 91847a2a17SPawel Jakub Dawidek #ifdef DEBUG_REDZONE 92847a2a17SPawel Jakub Dawidek #include <vm/redzone.h> 93847a2a17SPawel Jakub Dawidek #endif 94e4eb384bSBosko Milekic 95984982d6SPoul-Henning Kamp #if defined(INVARIANTS) && defined(__i386__) 96984982d6SPoul-Henning Kamp #include <machine/cpu.h> 97984982d6SPoul-Henning Kamp #endif 98984982d6SPoul-Henning Kamp 99909ed16cSRobert Watson #include <ddb/ddb.h> 100909ed16cSRobert Watson 10191dd776cSJohn Birrell #ifdef KDTRACE_HOOKS 10291dd776cSJohn Birrell #include <sys/dtrace_bsd.h> 10391dd776cSJohn Birrell 1047cd79421SMateusz Guzik bool __read_frequently dtrace_malloc_enabled; 1057cd79421SMateusz Guzik dtrace_malloc_probe_func_t __read_mostly dtrace_malloc_probe; 10691dd776cSJohn Birrell #endif 10791dd776cSJohn Birrell 108ab3185d1SJeff Roberson #if defined(INVARIANTS) || defined(MALLOC_MAKE_FAILURES) || \ 109ab3185d1SJeff Roberson defined(DEBUG_MEMGUARD) || defined(DEBUG_REDZONE) 110ab3185d1SJeff Roberson #define MALLOC_DEBUG 1 111ab3185d1SJeff Roberson #endif 112ab3185d1SJeff Roberson 11344a8ff31SArchie Cobbs /* 11444a8ff31SArchie Cobbs * When realloc() is called, if the new size is sufficiently smaller than 11544a8ff31SArchie Cobbs * the old size, realloc() will allocate a new, smaller block to avoid 11644a8ff31SArchie Cobbs * wasting memory. 'Sufficiently smaller' is defined as: newsize <= 11744a8ff31SArchie Cobbs * oldsize / 2^n, where REALLOC_FRACTION defines the value of 'n'. 11844a8ff31SArchie Cobbs */ 11944a8ff31SArchie Cobbs #ifndef REALLOC_FRACTION 12044a8ff31SArchie Cobbs #define REALLOC_FRACTION 1 /* new block if <= half the size */ 12144a8ff31SArchie Cobbs #endif 12244a8ff31SArchie Cobbs 1230ce3f16dSRobert Watson /* 1240ce3f16dSRobert Watson * Centrally define some common malloc types. 1250ce3f16dSRobert Watson */ 1263b6fb885SPoul-Henning Kamp MALLOC_DEFINE(M_CACHE, "cache", "Various Dynamically allocated caches"); 1279ef246c6SBruce Evans MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory"); 1289ef246c6SBruce Evans MALLOC_DEFINE(M_TEMP, "temp", "misc temporary data buffers"); 1299ef246c6SBruce Evans 130db669378SPeter Wemm static struct malloc_type *kmemstatistics; 131cd814b26SRobert Watson static int kmemcount; 1321f6889a1SMatthew Dillon 1338355f576SJeff Roberson #define KMEM_ZSHIFT 4 1348355f576SJeff Roberson #define KMEM_ZBASE 16 1358355f576SJeff Roberson #define KMEM_ZMASK (KMEM_ZBASE - 1) 1368355f576SJeff Roberson 137bda06553SXin LI #define KMEM_ZMAX 65536 1388355f576SJeff Roberson #define KMEM_ZSIZE (KMEM_ZMAX >> KMEM_ZSHIFT) 13960ae52f7SEd Schouten static uint8_t kmemsize[KMEM_ZSIZE + 1]; 1406f267175SJeff Roberson 141d7854da1SMatthew D Fleming #ifndef MALLOC_DEBUG_MAXZONES 142d7854da1SMatthew D Fleming #define MALLOC_DEBUG_MAXZONES 1 143d7854da1SMatthew D Fleming #endif 144d7854da1SMatthew D Fleming static int numzones = MALLOC_DEBUG_MAXZONES; 145d7854da1SMatthew D Fleming 1460ce3f16dSRobert Watson /* 1470ce3f16dSRobert Watson * Small malloc(9) memory allocations are allocated from a set of UMA buckets 1480ce3f16dSRobert Watson * of various sizes. 1490ce3f16dSRobert Watson * 150828afddaSMateusz Guzik * Warning: the layout of the struct is duplicated in libmemstat for KVM support. 151828afddaSMateusz Guzik * 1520ce3f16dSRobert Watson * XXX: The comment here used to read "These won't be powers of two for 1530ce3f16dSRobert Watson * long." It's possible that a significant amount of wasted memory could be 1540ce3f16dSRobert Watson * recovered by tuning the sizes of these buckets. 1550ce3f16dSRobert Watson */ 1568355f576SJeff Roberson struct { 1576f267175SJeff Roberson int kz_size; 158eaa17d42SRyan Libby const char *kz_name; 159d7854da1SMatthew D Fleming uma_zone_t kz_zone[MALLOC_DEBUG_MAXZONES]; 1606f267175SJeff Roberson } kmemzones[] = { 161e1b6a7f8SMateusz Guzik {16, "malloc-16", }, 162e1b6a7f8SMateusz Guzik {32, "malloc-32", }, 163e1b6a7f8SMateusz Guzik {64, "malloc-64", }, 164e1b6a7f8SMateusz Guzik {128, "malloc-128", }, 165e1b6a7f8SMateusz Guzik {256, "malloc-256", }, 166e1b6a7f8SMateusz Guzik {512, "malloc-512", }, 167e1b6a7f8SMateusz Guzik {1024, "malloc-1024", }, 168e1b6a7f8SMateusz Guzik {2048, "malloc-2048", }, 169e1b6a7f8SMateusz Guzik {4096, "malloc-4096", }, 170e1b6a7f8SMateusz Guzik {8192, "malloc-8192", }, 171e1b6a7f8SMateusz Guzik {16384, "malloc-16384", }, 172e1b6a7f8SMateusz Guzik {32768, "malloc-32768", }, 173e1b6a7f8SMateusz Guzik {65536, "malloc-65536", }, 1748355f576SJeff Roberson {0, NULL}, 1758355f576SJeff Roberson }; 1768355f576SJeff Roberson 177b89eaf4eSAlan Cox u_long vm_kmem_size; 178d801e824SAndriy Gapon SYSCTL_ULONG(_vm, OID_AUTO, kmem_size, CTLFLAG_RDTUN, &vm_kmem_size, 0, 17984344f9fSDag-Erling Smørgrav "Size of kernel memory"); 1805a34a9f0SJeff Roberson 1817001d850SXin LI static u_long kmem_zmax = KMEM_ZMAX; 1827001d850SXin LI SYSCTL_ULONG(_vm, OID_AUTO, kmem_zmax, CTLFLAG_RDTUN, &kmem_zmax, 0, 1837001d850SXin LI "Maximum allocation size that malloc(9) would use UMA as backend"); 1847001d850SXin LI 185b89eaf4eSAlan Cox static u_long vm_kmem_size_min; 186d801e824SAndriy Gapon SYSCTL_ULONG(_vm, OID_AUTO, kmem_size_min, CTLFLAG_RDTUN, &vm_kmem_size_min, 0, 1870e5179e4SStephane E. Potvin "Minimum size of kernel memory"); 1880e5179e4SStephane E. Potvin 189b89eaf4eSAlan Cox static u_long vm_kmem_size_max; 190d801e824SAndriy Gapon SYSCTL_ULONG(_vm, OID_AUTO, kmem_size_max, CTLFLAG_RDTUN, &vm_kmem_size_max, 0, 191479439b4SDag-Erling Smørgrav "Maximum size of kernel memory"); 192479439b4SDag-Erling Smørgrav 1934813ad54SHans Petter Selasky static u_int vm_kmem_size_scale; 194d801e824SAndriy Gapon SYSCTL_UINT(_vm, OID_AUTO, kmem_size_scale, CTLFLAG_RDTUN, &vm_kmem_size_scale, 0, 195479439b4SDag-Erling Smørgrav "Scale factor for kernel memory size"); 196479439b4SDag-Erling Smørgrav 1977814c80aSAndriy Gapon static int sysctl_kmem_map_size(SYSCTL_HANDLER_ARGS); 1987814c80aSAndriy Gapon SYSCTL_PROC(_vm, OID_AUTO, kmem_map_size, 1997814c80aSAndriy Gapon CTLFLAG_RD | CTLTYPE_ULONG | CTLFLAG_MPSAFE, NULL, 0, 2005df87b21SJeff Roberson sysctl_kmem_map_size, "LU", "Current kmem allocation size"); 2017814c80aSAndriy Gapon 20295bb9d38SAndriy Gapon static int sysctl_kmem_map_free(SYSCTL_HANDLER_ARGS); 20395bb9d38SAndriy Gapon SYSCTL_PROC(_vm, OID_AUTO, kmem_map_free, 20495bb9d38SAndriy Gapon CTLFLAG_RD | CTLTYPE_ULONG | CTLFLAG_MPSAFE, NULL, 0, 2055df87b21SJeff Roberson sysctl_kmem_map_free, "LU", "Free space in kmem"); 20695bb9d38SAndriy Gapon 207828afddaSMateusz Guzik static SYSCTL_NODE(_vm, OID_AUTO, malloc, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 208828afddaSMateusz Guzik "Malloc information"); 209828afddaSMateusz Guzik 210828afddaSMateusz Guzik static u_int vm_malloc_zone_count = nitems(kmemzones); 211828afddaSMateusz Guzik SYSCTL_UINT(_vm_malloc, OID_AUTO, zone_count, 212828afddaSMateusz Guzik CTLFLAG_RD, &vm_malloc_zone_count, 0, 213828afddaSMateusz Guzik "Number of malloc zones"); 214828afddaSMateusz Guzik 215828afddaSMateusz Guzik static int sysctl_vm_malloc_zone_sizes(SYSCTL_HANDLER_ARGS); 216828afddaSMateusz Guzik SYSCTL_PROC(_vm_malloc, OID_AUTO, zone_sizes, 217828afddaSMateusz Guzik CTLFLAG_RD | CTLTYPE_OPAQUE | CTLFLAG_MPSAFE, NULL, 0, 218828afddaSMateusz Guzik sysctl_vm_malloc_zone_sizes, "S", "Zone sizes used by malloc"); 219828afddaSMateusz Guzik 2205a34a9f0SJeff Roberson /* 22199571dc3SJeff Roberson * The malloc_mtx protects the kmemstatistics linked list. 2225a34a9f0SJeff Roberson */ 2235a34a9f0SJeff Roberson struct mtx malloc_mtx; 22469ef67f9SJason Evans 2255e914b96SJeff Roberson #ifdef MALLOC_PROFILE 2265e914b96SJeff Roberson uint64_t krequests[KMEM_ZSIZE + 1]; 2276f267175SJeff Roberson 2285e914b96SJeff Roberson static int sysctl_kern_mprof(SYSCTL_HANDLER_ARGS); 2295e914b96SJeff Roberson #endif 2305e914b96SJeff Roberson 231cd814b26SRobert Watson static int sysctl_kern_malloc_stats(SYSCTL_HANDLER_ARGS); 232df8bae1dSRodney W. Grimes 2330ce3f16dSRobert Watson /* 2340ce3f16dSRobert Watson * time_uptime of the last malloc(9) failure (induced or real). 2350ce3f16dSRobert Watson */ 2361fb14a47SPoul-Henning Kamp static time_t t_malloc_fail; 2371fb14a47SPoul-Henning Kamp 238d7854da1SMatthew D Fleming #if defined(MALLOC_MAKE_FAILURES) || (MALLOC_DEBUG_MAXZONES > 1) 2397029da5cSPawel Biernacki static SYSCTL_NODE(_debug, OID_AUTO, malloc, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 240d7854da1SMatthew D Fleming "Kernel malloc debugging options"); 241d7854da1SMatthew D Fleming #endif 242d7854da1SMatthew D Fleming 243eae870cdSRobert Watson /* 2440ce3f16dSRobert Watson * malloc(9) fault injection -- cause malloc failures every (n) mallocs when 2450ce3f16dSRobert Watson * the caller specifies M_NOWAIT. If set to 0, no failures are caused. 246eae870cdSRobert Watson */ 2470ce3f16dSRobert Watson #ifdef MALLOC_MAKE_FAILURES 248eae870cdSRobert Watson static int malloc_failure_rate; 249eae870cdSRobert Watson static int malloc_nowait_count; 250eae870cdSRobert Watson static int malloc_failure_count; 251af3b2549SHans Petter Selasky SYSCTL_INT(_debug_malloc, OID_AUTO, failure_rate, CTLFLAG_RWTUN, 252eae870cdSRobert Watson &malloc_failure_rate, 0, "Every (n) mallocs with M_NOWAIT will fail"); 253eae870cdSRobert Watson SYSCTL_INT(_debug_malloc, OID_AUTO, failure_count, CTLFLAG_RD, 254eae870cdSRobert Watson &malloc_failure_count, 0, "Number of imposed M_NOWAIT malloc failures"); 255eae870cdSRobert Watson #endif 256eae870cdSRobert Watson 2577814c80aSAndriy Gapon static int 2587814c80aSAndriy Gapon sysctl_kmem_map_size(SYSCTL_HANDLER_ARGS) 2597814c80aSAndriy Gapon { 2607814c80aSAndriy Gapon u_long size; 2617814c80aSAndriy Gapon 2622e47807cSJeff Roberson size = uma_size(); 2637814c80aSAndriy Gapon return (sysctl_handle_long(oidp, &size, 0, req)); 2647814c80aSAndriy Gapon } 2657814c80aSAndriy Gapon 26695bb9d38SAndriy Gapon static int 26795bb9d38SAndriy Gapon sysctl_kmem_map_free(SYSCTL_HANDLER_ARGS) 26895bb9d38SAndriy Gapon { 2692e47807cSJeff Roberson u_long size, limit; 27095bb9d38SAndriy Gapon 2712e47807cSJeff Roberson /* The sysctl is unsigned, implement as a saturation value. */ 2722e47807cSJeff Roberson size = uma_size(); 2732e47807cSJeff Roberson limit = uma_limit(); 2742e47807cSJeff Roberson if (size > limit) 2752e47807cSJeff Roberson size = 0; 2762e47807cSJeff Roberson else 2772e47807cSJeff Roberson size = limit - size; 27895bb9d38SAndriy Gapon return (sysctl_handle_long(oidp, &size, 0, req)); 27995bb9d38SAndriy Gapon } 28095bb9d38SAndriy Gapon 281828afddaSMateusz Guzik static int 282828afddaSMateusz Guzik sysctl_vm_malloc_zone_sizes(SYSCTL_HANDLER_ARGS) 283828afddaSMateusz Guzik { 284828afddaSMateusz Guzik int sizes[nitems(kmemzones)]; 285828afddaSMateusz Guzik int i; 286828afddaSMateusz Guzik 287828afddaSMateusz Guzik for (i = 0; i < nitems(kmemzones); i++) { 288828afddaSMateusz Guzik sizes[i] = kmemzones[i].kz_size; 289828afddaSMateusz Guzik } 290828afddaSMateusz Guzik 291828afddaSMateusz Guzik return (SYSCTL_OUT(req, &sizes, sizeof(sizes))); 292828afddaSMateusz Guzik } 293828afddaSMateusz Guzik 294d7854da1SMatthew D Fleming /* 295d7854da1SMatthew D Fleming * malloc(9) uma zone separation -- sub-page buffer overruns in one 296d7854da1SMatthew D Fleming * malloc type will affect only a subset of other malloc types. 297d7854da1SMatthew D Fleming */ 298d7854da1SMatthew D Fleming #if MALLOC_DEBUG_MAXZONES > 1 299d7854da1SMatthew D Fleming static void 300d7854da1SMatthew D Fleming tunable_set_numzones(void) 301d7854da1SMatthew D Fleming { 302d7854da1SMatthew D Fleming 303d7854da1SMatthew D Fleming TUNABLE_INT_FETCH("debug.malloc.numzones", 304d7854da1SMatthew D Fleming &numzones); 305d7854da1SMatthew D Fleming 306d7854da1SMatthew D Fleming /* Sanity check the number of malloc uma zones. */ 307d7854da1SMatthew D Fleming if (numzones <= 0) 308d7854da1SMatthew D Fleming numzones = 1; 309d7854da1SMatthew D Fleming if (numzones > MALLOC_DEBUG_MAXZONES) 310d7854da1SMatthew D Fleming numzones = MALLOC_DEBUG_MAXZONES; 311d7854da1SMatthew D Fleming } 312d7854da1SMatthew D Fleming SYSINIT(numzones, SI_SUB_TUNABLES, SI_ORDER_ANY, tunable_set_numzones, NULL); 313af3b2549SHans Petter Selasky SYSCTL_INT(_debug_malloc, OID_AUTO, numzones, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, 314d7854da1SMatthew D Fleming &numzones, 0, "Number of malloc uma subzones"); 315d7854da1SMatthew D Fleming 316d7854da1SMatthew D Fleming /* 317d7854da1SMatthew D Fleming * Any number that changes regularly is an okay choice for the 318d7854da1SMatthew D Fleming * offset. Build numbers are pretty good of you have them. 319d7854da1SMatthew D Fleming */ 320d7854da1SMatthew D Fleming static u_int zone_offset = __FreeBSD_version; 321d7854da1SMatthew D Fleming TUNABLE_INT("debug.malloc.zone_offset", &zone_offset); 322d7854da1SMatthew D Fleming SYSCTL_UINT(_debug_malloc, OID_AUTO, zone_offset, CTLFLAG_RDTUN, 323d7854da1SMatthew D Fleming &zone_offset, 0, "Separate malloc types by examining the " 324d7854da1SMatthew D Fleming "Nth character in the malloc type short description."); 325d7854da1SMatthew D Fleming 326c9e05ccdSMateusz Guzik static void 327c9e05ccdSMateusz Guzik mtp_set_subzone(struct malloc_type *mtp) 328d7854da1SMatthew D Fleming { 329c9e05ccdSMateusz Guzik struct malloc_type_internal *mtip; 330c9e05ccdSMateusz Guzik const char *desc; 331d7854da1SMatthew D Fleming size_t len; 332d7854da1SMatthew D Fleming u_int val; 333d7854da1SMatthew D Fleming 334bdcc2226SMateusz Guzik mtip = &mtp->ks_mti; 335c9e05ccdSMateusz Guzik desc = mtp->ks_shortdesc; 336d7854da1SMatthew D Fleming if (desc == NULL || (len = strlen(desc)) == 0) 337c9e05ccdSMateusz Guzik val = 0; 338c9e05ccdSMateusz Guzik else 339d7854da1SMatthew D Fleming val = desc[zone_offset % len]; 340c9e05ccdSMateusz Guzik mtip->mti_zone = (val % numzones); 341c9e05ccdSMateusz Guzik } 342c9e05ccdSMateusz Guzik 343c9e05ccdSMateusz Guzik static inline u_int 344c9e05ccdSMateusz Guzik mtp_get_subzone(struct malloc_type *mtp) 345c9e05ccdSMateusz Guzik { 346c9e05ccdSMateusz Guzik struct malloc_type_internal *mtip; 347c9e05ccdSMateusz Guzik 348bdcc2226SMateusz Guzik mtip = &mtp->ks_mti; 349c9e05ccdSMateusz Guzik 350c9e05ccdSMateusz Guzik KASSERT(mtip->mti_zone < numzones, 351c9e05ccdSMateusz Guzik ("mti_zone %u out of range %d", 352c9e05ccdSMateusz Guzik mtip->mti_zone, numzones)); 353c9e05ccdSMateusz Guzik return (mtip->mti_zone); 354d7854da1SMatthew D Fleming } 355d7854da1SMatthew D Fleming #elif MALLOC_DEBUG_MAXZONES == 0 356d7854da1SMatthew D Fleming #error "MALLOC_DEBUG_MAXZONES must be positive." 357d7854da1SMatthew D Fleming #else 358c9e05ccdSMateusz Guzik static void 359c9e05ccdSMateusz Guzik mtp_set_subzone(struct malloc_type *mtp) 360c9e05ccdSMateusz Guzik { 361c9e05ccdSMateusz Guzik struct malloc_type_internal *mtip; 362c9e05ccdSMateusz Guzik 363bdcc2226SMateusz Guzik mtip = &mtp->ks_mti; 364c9e05ccdSMateusz Guzik mtip->mti_zone = 0; 365c9e05ccdSMateusz Guzik } 366c9e05ccdSMateusz Guzik 367d7854da1SMatthew D Fleming static inline u_int 368c9e05ccdSMateusz Guzik mtp_get_subzone(struct malloc_type *mtp) 369d7854da1SMatthew D Fleming { 370d7854da1SMatthew D Fleming 371d7854da1SMatthew D Fleming return (0); 372d7854da1SMatthew D Fleming } 373d7854da1SMatthew D Fleming #endif /* MALLOC_DEBUG_MAXZONES > 1 */ 374d7854da1SMatthew D Fleming 3751fb14a47SPoul-Henning Kamp int 3761fb14a47SPoul-Henning Kamp malloc_last_fail(void) 3771fb14a47SPoul-Henning Kamp { 3781fb14a47SPoul-Henning Kamp 3791fb14a47SPoul-Henning Kamp return (time_uptime - t_malloc_fail); 3801fb14a47SPoul-Henning Kamp } 3811fb14a47SPoul-Henning Kamp 382df8bae1dSRodney W. Grimes /* 3830ce3f16dSRobert Watson * An allocation has succeeded -- update malloc type statistics for the 3840ce3f16dSRobert Watson * amount of bucket size. Occurs within a critical section so that the 3850ce3f16dSRobert Watson * thread isn't preempted and doesn't migrate while updating per-PCU 3860ce3f16dSRobert Watson * statistics. 3874362fadaSBrian Feldman */ 3884362fadaSBrian Feldman static void 38963a7e0a3SRobert Watson malloc_type_zone_allocated(struct malloc_type *mtp, unsigned long size, 3904362fadaSBrian Feldman int zindx) 3914362fadaSBrian Feldman { 39263a7e0a3SRobert Watson struct malloc_type_internal *mtip; 39363a7e0a3SRobert Watson struct malloc_type_stats *mtsp; 39463a7e0a3SRobert Watson 39563a7e0a3SRobert Watson critical_enter(); 396bdcc2226SMateusz Guzik mtip = &mtp->ks_mti; 3979afff6b1SMateusz Guzik mtsp = zpcpu_get(mtip->mti_stats); 39873864adbSPawel Jakub Dawidek if (size > 0) { 39963a7e0a3SRobert Watson mtsp->mts_memalloced += size; 40063a7e0a3SRobert Watson mtsp->mts_numallocs++; 40173864adbSPawel Jakub Dawidek } 4024362fadaSBrian Feldman if (zindx != -1) 40363a7e0a3SRobert Watson mtsp->mts_size |= 1 << zindx; 40491dd776cSJohn Birrell 40591dd776cSJohn Birrell #ifdef KDTRACE_HOOKS 4067cd79421SMateusz Guzik if (__predict_false(dtrace_malloc_enabled)) { 40791dd776cSJohn Birrell uint32_t probe_id = mtip->mti_probes[DTMALLOC_PROBE_MALLOC]; 40891dd776cSJohn Birrell if (probe_id != 0) 40991dd776cSJohn Birrell (dtrace_malloc_probe)(probe_id, 41091dd776cSJohn Birrell (uintptr_t) mtp, (uintptr_t) mtip, 41191dd776cSJohn Birrell (uintptr_t) mtsp, size, zindx); 41291dd776cSJohn Birrell } 41391dd776cSJohn Birrell #endif 41491dd776cSJohn Birrell 41563a7e0a3SRobert Watson critical_exit(); 4164362fadaSBrian Feldman } 4174362fadaSBrian Feldman 4184362fadaSBrian Feldman void 41963a7e0a3SRobert Watson malloc_type_allocated(struct malloc_type *mtp, unsigned long size) 4204362fadaSBrian Feldman { 42163a7e0a3SRobert Watson 42273864adbSPawel Jakub Dawidek if (size > 0) 42363a7e0a3SRobert Watson malloc_type_zone_allocated(mtp, size, -1); 4244362fadaSBrian Feldman } 4254362fadaSBrian Feldman 4264362fadaSBrian Feldman /* 4273805385eSRobert Watson * A free operation has occurred -- update malloc type statistics for the 4280ce3f16dSRobert Watson * amount of the bucket size. Occurs within a critical section so that the 4290ce3f16dSRobert Watson * thread isn't preempted and doesn't migrate while updating per-CPU 4300ce3f16dSRobert Watson * statistics. 4314362fadaSBrian Feldman */ 4324362fadaSBrian Feldman void 43363a7e0a3SRobert Watson malloc_type_freed(struct malloc_type *mtp, unsigned long size) 4344362fadaSBrian Feldman { 43563a7e0a3SRobert Watson struct malloc_type_internal *mtip; 43663a7e0a3SRobert Watson struct malloc_type_stats *mtsp; 43763a7e0a3SRobert Watson 43863a7e0a3SRobert Watson critical_enter(); 439bdcc2226SMateusz Guzik mtip = &mtp->ks_mti; 4409afff6b1SMateusz Guzik mtsp = zpcpu_get(mtip->mti_stats); 44163a7e0a3SRobert Watson mtsp->mts_memfreed += size; 44263a7e0a3SRobert Watson mtsp->mts_numfrees++; 44391dd776cSJohn Birrell 44491dd776cSJohn Birrell #ifdef KDTRACE_HOOKS 4457cd79421SMateusz Guzik if (__predict_false(dtrace_malloc_enabled)) { 44691dd776cSJohn Birrell uint32_t probe_id = mtip->mti_probes[DTMALLOC_PROBE_FREE]; 44791dd776cSJohn Birrell if (probe_id != 0) 44891dd776cSJohn Birrell (dtrace_malloc_probe)(probe_id, 44991dd776cSJohn Birrell (uintptr_t) mtp, (uintptr_t) mtip, 45091dd776cSJohn Birrell (uintptr_t) mtsp, size, 0); 45191dd776cSJohn Birrell } 45291dd776cSJohn Birrell #endif 45391dd776cSJohn Birrell 45463a7e0a3SRobert Watson critical_exit(); 4554362fadaSBrian Feldman } 4564362fadaSBrian Feldman 4574362fadaSBrian Feldman /* 458f346986bSAlan Cox * contigmalloc: 459f346986bSAlan Cox * 460f346986bSAlan Cox * Allocate a block of physically contiguous memory. 461f346986bSAlan Cox * 462f346986bSAlan Cox * If M_NOWAIT is set, this routine will not block and return NULL if 463f346986bSAlan Cox * the allocation fails. 464f346986bSAlan Cox */ 465f346986bSAlan Cox void * 466f346986bSAlan Cox contigmalloc(unsigned long size, struct malloc_type *type, int flags, 467f346986bSAlan Cox vm_paddr_t low, vm_paddr_t high, unsigned long alignment, 468831ce4cbSJohn Baldwin vm_paddr_t boundary) 469f346986bSAlan Cox { 470f346986bSAlan Cox void *ret; 471f346986bSAlan Cox 47244d0efb2SAlan Cox ret = (void *)kmem_alloc_contig(size, flags, low, high, alignment, 47344d0efb2SAlan Cox boundary, VM_MEMATTR_DEFAULT); 474f346986bSAlan Cox if (ret != NULL) 475f346986bSAlan Cox malloc_type_allocated(type, round_page(size)); 476f346986bSAlan Cox return (ret); 477f346986bSAlan Cox } 478f346986bSAlan Cox 479ab3185d1SJeff Roberson void * 4809978bd99SMark Johnston contigmalloc_domainset(unsigned long size, struct malloc_type *type, 4819978bd99SMark Johnston struct domainset *ds, int flags, vm_paddr_t low, vm_paddr_t high, 482ab3185d1SJeff Roberson unsigned long alignment, vm_paddr_t boundary) 483ab3185d1SJeff Roberson { 484ab3185d1SJeff Roberson void *ret; 485ab3185d1SJeff Roberson 4869978bd99SMark Johnston ret = (void *)kmem_alloc_contig_domainset(ds, size, flags, low, high, 487ab3185d1SJeff Roberson alignment, boundary, VM_MEMATTR_DEFAULT); 488ab3185d1SJeff Roberson if (ret != NULL) 489ab3185d1SJeff Roberson malloc_type_allocated(type, round_page(size)); 490ab3185d1SJeff Roberson return (ret); 491ab3185d1SJeff Roberson } 492ab3185d1SJeff Roberson 493f346986bSAlan Cox /* 494f346986bSAlan Cox * contigfree: 495f346986bSAlan Cox * 496f346986bSAlan Cox * Free a block of memory allocated by contigmalloc. 497f346986bSAlan Cox * 498f346986bSAlan Cox * This routine may not block. 499f346986bSAlan Cox */ 500f346986bSAlan Cox void 501f346986bSAlan Cox contigfree(void *addr, unsigned long size, struct malloc_type *type) 502f346986bSAlan Cox { 503f346986bSAlan Cox 50449bfa624SAlan Cox kmem_free((vm_offset_t)addr, size); 505f346986bSAlan Cox malloc_type_freed(type, round_page(size)); 506f346986bSAlan Cox } 507f346986bSAlan Cox 508ab3185d1SJeff Roberson #ifdef MALLOC_DEBUG 509ab3185d1SJeff Roberson static int 5105a70796aSLi-Wen Hsu malloc_dbg(caddr_t *vap, size_t *sizep, struct malloc_type *mtp, 511ab3185d1SJeff Roberson int flags) 512df8bae1dSRodney W. Grimes { 513194a0abfSPoul-Henning Kamp #ifdef INVARIANTS 514ab3185d1SJeff Roberson int indx; 515ab3185d1SJeff Roberson 516bdcc2226SMateusz Guzik KASSERT(mtp->ks_version == M_VERSION, ("malloc: bad malloc type version")); 517d3c11994SPoul-Henning Kamp /* 51823198357SRuslan Ermilov * Check that exactly one of M_WAITOK or M_NOWAIT is specified. 519d3c11994SPoul-Henning Kamp */ 52023198357SRuslan Ermilov indx = flags & (M_WAITOK | M_NOWAIT); 521d3c11994SPoul-Henning Kamp if (indx != M_NOWAIT && indx != M_WAITOK) { 522d3c11994SPoul-Henning Kamp static struct timeval lasterr; 523d3c11994SPoul-Henning Kamp static int curerr, once; 524d3c11994SPoul-Henning Kamp if (once == 0 && ppsratecheck(&lasterr, &curerr, 1)) { 525d3c11994SPoul-Henning Kamp printf("Bad malloc flags: %x\n", indx); 5262d50560aSMarcel Moolenaar kdb_backtrace(); 527d3c11994SPoul-Henning Kamp flags |= M_WAITOK; 528d3c11994SPoul-Henning Kamp once++; 529d3c11994SPoul-Henning Kamp } 530d3c11994SPoul-Henning Kamp } 531194a0abfSPoul-Henning Kamp #endif 532eae870cdSRobert Watson #ifdef MALLOC_MAKE_FAILURES 533eae870cdSRobert Watson if ((flags & M_NOWAIT) && (malloc_failure_rate != 0)) { 534eae870cdSRobert Watson atomic_add_int(&malloc_nowait_count, 1); 535eae870cdSRobert Watson if ((malloc_nowait_count % malloc_failure_rate) == 0) { 536eae870cdSRobert Watson atomic_add_int(&malloc_failure_count, 1); 5373f6ee876SPoul-Henning Kamp t_malloc_fail = time_uptime; 538ab3185d1SJeff Roberson *vap = NULL; 539ab3185d1SJeff Roberson return (EJUSTRETURN); 540eae870cdSRobert Watson } 541eae870cdSRobert Watson } 542eae870cdSRobert Watson #endif 54306bf2a6aSMatt Macy if (flags & M_WAITOK) { 544b40ce416SJulian Elischer KASSERT(curthread->td_intr_nesting_level == 0, 545a163d034SWarner Losh ("malloc(M_WAITOK) in interrupt context")); 5465757b59fSGleb Smirnoff if (__predict_false(!THREAD_CAN_SLEEP())) { 547bac06038SGleb Smirnoff #ifdef EPOCH_TRACE 548bac06038SGleb Smirnoff epoch_trace_list(curthread); 549bac06038SGleb Smirnoff #endif 5505757b59fSGleb Smirnoff KASSERT(1, 5515757b59fSGleb Smirnoff ("malloc(M_WAITOK) with sleeping prohibited")); 5525757b59fSGleb Smirnoff } 55306bf2a6aSMatt Macy } 554d9e2e68dSMark Johnston KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 5551067a2baSJonathan T. Looney ("malloc: called with spinlock or critical section held")); 5561067a2baSJonathan T. Looney 557e4eb384bSBosko Milekic #ifdef DEBUG_MEMGUARD 558ab3185d1SJeff Roberson if (memguard_cmp_mtp(mtp, *sizep)) { 559ab3185d1SJeff Roberson *vap = memguard_alloc(*sizep, flags); 560ab3185d1SJeff Roberson if (*vap != NULL) 561ab3185d1SJeff Roberson return (EJUSTRETURN); 562e3813573SMatthew D Fleming /* This is unfortunate but should not be fatal. */ 563e3813573SMatthew D Fleming } 564e4eb384bSBosko Milekic #endif 565e4eb384bSBosko Milekic 566847a2a17SPawel Jakub Dawidek #ifdef DEBUG_REDZONE 567ab3185d1SJeff Roberson *sizep = redzone_size_ntor(*sizep); 568ab3185d1SJeff Roberson #endif 569ab3185d1SJeff Roberson 570ab3185d1SJeff Roberson return (0); 571ab3185d1SJeff Roberson } 572ab3185d1SJeff Roberson #endif 573ab3185d1SJeff Roberson 574ab3185d1SJeff Roberson /* 5756d6a03d7SJeff Roberson * Handle large allocations and frees by using kmem_malloc directly. 5766d6a03d7SJeff Roberson */ 5776d6a03d7SJeff Roberson static inline bool 5786d6a03d7SJeff Roberson malloc_large_slab(uma_slab_t slab) 5796d6a03d7SJeff Roberson { 5806d6a03d7SJeff Roberson uintptr_t va; 5816d6a03d7SJeff Roberson 5826d6a03d7SJeff Roberson va = (uintptr_t)slab; 5836d6a03d7SJeff Roberson return ((va & 1) != 0); 5846d6a03d7SJeff Roberson } 5856d6a03d7SJeff Roberson 5866d6a03d7SJeff Roberson static inline size_t 5876d6a03d7SJeff Roberson malloc_large_size(uma_slab_t slab) 5886d6a03d7SJeff Roberson { 5896d6a03d7SJeff Roberson uintptr_t va; 5906d6a03d7SJeff Roberson 5916d6a03d7SJeff Roberson va = (uintptr_t)slab; 5926d6a03d7SJeff Roberson return (va >> 1); 5936d6a03d7SJeff Roberson } 5946d6a03d7SJeff Roberson 5956d6a03d7SJeff Roberson static caddr_t 5966d6a03d7SJeff Roberson malloc_large(size_t *size, struct domainset *policy, int flags) 5976d6a03d7SJeff Roberson { 5986d6a03d7SJeff Roberson vm_offset_t va; 5996d6a03d7SJeff Roberson size_t sz; 6006d6a03d7SJeff Roberson 6016d6a03d7SJeff Roberson sz = roundup(*size, PAGE_SIZE); 6026d6a03d7SJeff Roberson va = kmem_malloc_domainset(policy, sz, flags); 6036d6a03d7SJeff Roberson if (va != 0) { 6046d6a03d7SJeff Roberson /* The low bit is unused for slab pointers. */ 6056d6a03d7SJeff Roberson vsetzoneslab(va, NULL, (void *)((sz << 1) | 1)); 6066d6a03d7SJeff Roberson uma_total_inc(sz); 6076d6a03d7SJeff Roberson *size = sz; 6086d6a03d7SJeff Roberson } 6096d6a03d7SJeff Roberson return ((caddr_t)va); 6106d6a03d7SJeff Roberson } 6116d6a03d7SJeff Roberson 6126d6a03d7SJeff Roberson static void 6136d6a03d7SJeff Roberson free_large(void *addr, size_t size) 6146d6a03d7SJeff Roberson { 6156d6a03d7SJeff Roberson 6166d6a03d7SJeff Roberson kmem_free((vm_offset_t)addr, size); 6176d6a03d7SJeff Roberson uma_total_dec(size); 6186d6a03d7SJeff Roberson } 6196d6a03d7SJeff Roberson 6206d6a03d7SJeff Roberson /* 621ab3185d1SJeff Roberson * malloc: 622ab3185d1SJeff Roberson * 623ab3185d1SJeff Roberson * Allocate a block of memory. 624ab3185d1SJeff Roberson * 625ab3185d1SJeff Roberson * If M_NOWAIT is set, this routine will not block and return NULL if 626ab3185d1SJeff Roberson * the allocation fails. 627ab3185d1SJeff Roberson */ 628ab3185d1SJeff Roberson void * 62934c538c3SMateusz Guzik (malloc)(size_t size, struct malloc_type *mtp, int flags) 630ab3185d1SJeff Roberson { 631ab3185d1SJeff Roberson int indx; 632ab3185d1SJeff Roberson caddr_t va; 633ab3185d1SJeff Roberson uma_zone_t zone; 634ab3185d1SJeff Roberson #if defined(DEBUG_REDZONE) 635ab3185d1SJeff Roberson unsigned long osize = size; 636ab3185d1SJeff Roberson #endif 637ab3185d1SJeff Roberson 63882c174a3SMateusz Guzik MPASS((flags & M_EXEC) == 0); 639ab3185d1SJeff Roberson #ifdef MALLOC_DEBUG 6405072a5f4SMatt Macy va = NULL; 641ab3185d1SJeff Roberson if (malloc_dbg(&va, &size, mtp, flags) != 0) 642ab3185d1SJeff Roberson return (va); 643847a2a17SPawel Jakub Dawidek #endif 644847a2a17SPawel Jakub Dawidek 64582c174a3SMateusz Guzik if (size <= kmem_zmax) { 6466f267175SJeff Roberson if (size & KMEM_ZMASK) 6476f267175SJeff Roberson size = (size & ~KMEM_ZMASK) + KMEM_ZBASE; 6486f267175SJeff Roberson indx = kmemsize[size >> KMEM_ZSHIFT]; 649c9e05ccdSMateusz Guzik zone = kmemzones[indx].kz_zone[mtp_get_subzone(mtp)]; 6506f267175SJeff Roberson #ifdef MALLOC_PROFILE 6516f267175SJeff Roberson krequests[size >> KMEM_ZSHIFT]++; 6526f267175SJeff Roberson #endif 6538355f576SJeff Roberson va = uma_zalloc(zone, flags); 6544362fadaSBrian Feldman if (va != NULL) 655e20a199fSJeff Roberson size = zone->uz_size; 65663a7e0a3SRobert Watson malloc_type_zone_allocated(mtp, va == NULL ? 0 : size, indx); 6578355f576SJeff Roberson } else { 6586d6a03d7SJeff Roberson va = malloc_large(&size, DOMAINSET_RR(), flags); 65963a7e0a3SRobert Watson malloc_type_allocated(mtp, va == NULL ? 0 : size); 660df8bae1dSRodney W. Grimes } 66182c174a3SMateusz Guzik if (__predict_false(va == NULL)) { 66282c174a3SMateusz Guzik KASSERT((flags & M_WAITOK) == 0, 66382c174a3SMateusz Guzik ("malloc(M_WAITOK) returned NULL")); 6641fb14a47SPoul-Henning Kamp t_malloc_fail = time_uptime; 66582c174a3SMateusz Guzik } 666ab3185d1SJeff Roberson #ifdef DEBUG_REDZONE 667ab3185d1SJeff Roberson if (va != NULL) 668ab3185d1SJeff Roberson va = redzone_setup(va, osize); 6694db4f5c8SPoul-Henning Kamp #endif 670ab3185d1SJeff Roberson return ((void *) va); 671ab3185d1SJeff Roberson } 672ab3185d1SJeff Roberson 6739978bd99SMark Johnston static void * 674dc727127SMark Johnston malloc_domain(size_t *sizep, int *indxp, struct malloc_type *mtp, int domain, 6756d6a03d7SJeff Roberson int flags) 676ab3185d1SJeff Roberson { 677ab3185d1SJeff Roberson uma_zone_t zone; 678dc727127SMark Johnston caddr_t va; 679dc727127SMark Johnston size_t size; 680dc727127SMark Johnston int indx; 681ab3185d1SJeff Roberson 682dc727127SMark Johnston size = *sizep; 6836d6a03d7SJeff Roberson KASSERT(size <= kmem_zmax && (flags & M_EXEC) == 0, 6846d6a03d7SJeff Roberson ("malloc_domain: Called with bad flag / size combination.")); 685ab3185d1SJeff Roberson if (size & KMEM_ZMASK) 686ab3185d1SJeff Roberson size = (size & ~KMEM_ZMASK) + KMEM_ZBASE; 687ab3185d1SJeff Roberson indx = kmemsize[size >> KMEM_ZSHIFT]; 688c9e05ccdSMateusz Guzik zone = kmemzones[indx].kz_zone[mtp_get_subzone(mtp)]; 689ab3185d1SJeff Roberson #ifdef MALLOC_PROFILE 690ab3185d1SJeff Roberson krequests[size >> KMEM_ZSHIFT]++; 691ab3185d1SJeff Roberson #endif 692ab3185d1SJeff Roberson va = uma_zalloc_domain(zone, NULL, domain, flags); 693ab3185d1SJeff Roberson if (va != NULL) 694dc727127SMark Johnston *sizep = zone->uz_size; 6956d6a03d7SJeff Roberson *indxp = indx; 696df8bae1dSRodney W. Grimes return ((void *)va); 697df8bae1dSRodney W. Grimes } 698df8bae1dSRodney W. Grimes 699fd91e076SKristof Provost void * 7009978bd99SMark Johnston malloc_domainset(size_t size, struct malloc_type *mtp, struct domainset *ds, 7019978bd99SMark Johnston int flags) 7029978bd99SMark Johnston { 7039978bd99SMark Johnston struct vm_domainset_iter di; 70482c174a3SMateusz Guzik caddr_t va; 7059978bd99SMark Johnston int domain; 7066d6a03d7SJeff Roberson int indx; 7079978bd99SMark Johnston 7086d6a03d7SJeff Roberson #if defined(DEBUG_REDZONE) 7096d6a03d7SJeff Roberson unsigned long osize = size; 7106d6a03d7SJeff Roberson #endif 71182c174a3SMateusz Guzik MPASS((flags & M_EXEC) == 0); 7126d6a03d7SJeff Roberson #ifdef MALLOC_DEBUG 71382c174a3SMateusz Guzik va = NULL; 71482c174a3SMateusz Guzik if (malloc_dbg(&va, &size, mtp, flags) != 0) 71582c174a3SMateusz Guzik return (va); 7166d6a03d7SJeff Roberson #endif 71782c174a3SMateusz Guzik if (size <= kmem_zmax) { 7189978bd99SMark Johnston vm_domainset_iter_policy_init(&di, ds, &domain, &flags); 7199978bd99SMark Johnston do { 72082c174a3SMateusz Guzik va = malloc_domain(&size, &indx, mtp, domain, flags); 72182c174a3SMateusz Guzik } while (va == NULL && 7226d6a03d7SJeff Roberson vm_domainset_iter_policy(&di, &domain) == 0); 72382c174a3SMateusz Guzik malloc_type_zone_allocated(mtp, va == NULL ? 0 : size, indx); 7246d6a03d7SJeff Roberson } else { 7256d6a03d7SJeff Roberson /* Policy is handled by kmem. */ 72682c174a3SMateusz Guzik va = malloc_large(&size, ds, flags); 72782c174a3SMateusz Guzik malloc_type_allocated(mtp, va == NULL ? 0 : size); 72882c174a3SMateusz Guzik } 72982c174a3SMateusz Guzik if (__predict_false(va == NULL)) { 73082c174a3SMateusz Guzik KASSERT((flags & M_WAITOK) == 0, 73182c174a3SMateusz Guzik ("malloc(M_WAITOK) returned NULL")); 73282c174a3SMateusz Guzik t_malloc_fail = time_uptime; 73382c174a3SMateusz Guzik } 73482c174a3SMateusz Guzik #ifdef DEBUG_REDZONE 73582c174a3SMateusz Guzik if (va != NULL) 73682c174a3SMateusz Guzik va = redzone_setup(va, osize); 73782c174a3SMateusz Guzik #endif 73882c174a3SMateusz Guzik return (va); 7396d6a03d7SJeff Roberson } 7409978bd99SMark Johnston 74182c174a3SMateusz Guzik /* 74282c174a3SMateusz Guzik * Allocate an executable area. 74382c174a3SMateusz Guzik */ 74482c174a3SMateusz Guzik void * 74582c174a3SMateusz Guzik malloc_exec(size_t size, struct malloc_type *mtp, int flags) 74682c174a3SMateusz Guzik { 74782c174a3SMateusz Guzik caddr_t va; 74882c174a3SMateusz Guzik #if defined(DEBUG_REDZONE) 74982c174a3SMateusz Guzik unsigned long osize = size; 7506d6a03d7SJeff Roberson #endif 75182c174a3SMateusz Guzik 75282c174a3SMateusz Guzik flags |= M_EXEC; 75382c174a3SMateusz Guzik #ifdef MALLOC_DEBUG 75482c174a3SMateusz Guzik va = NULL; 75582c174a3SMateusz Guzik if (malloc_dbg(&va, &size, mtp, flags) != 0) 75682c174a3SMateusz Guzik return (va); 75782c174a3SMateusz Guzik #endif 75882c174a3SMateusz Guzik va = malloc_large(&size, DOMAINSET_RR(), flags); 75982c174a3SMateusz Guzik malloc_type_allocated(mtp, va == NULL ? 0 : size); 76082c174a3SMateusz Guzik if (__predict_false(va == NULL)) { 76182c174a3SMateusz Guzik KASSERT((flags & M_WAITOK) == 0, 76282c174a3SMateusz Guzik ("malloc(M_WAITOK) returned NULL")); 76382c174a3SMateusz Guzik t_malloc_fail = time_uptime; 76482c174a3SMateusz Guzik } 76582c174a3SMateusz Guzik #ifdef DEBUG_REDZONE 76682c174a3SMateusz Guzik if (va != NULL) 76782c174a3SMateusz Guzik va = redzone_setup(va, osize); 76882c174a3SMateusz Guzik #endif 76982c174a3SMateusz Guzik return ((void *) va); 77082c174a3SMateusz Guzik } 77182c174a3SMateusz Guzik 77282c174a3SMateusz Guzik void * 77382c174a3SMateusz Guzik malloc_domainset_exec(size_t size, struct malloc_type *mtp, struct domainset *ds, 77482c174a3SMateusz Guzik int flags) 77582c174a3SMateusz Guzik { 77682c174a3SMateusz Guzik caddr_t va; 77782c174a3SMateusz Guzik #if defined(DEBUG_REDZONE) 77882c174a3SMateusz Guzik unsigned long osize = size; 77982c174a3SMateusz Guzik #endif 78082c174a3SMateusz Guzik 78182c174a3SMateusz Guzik flags |= M_EXEC; 78282c174a3SMateusz Guzik #ifdef MALLOC_DEBUG 78382c174a3SMateusz Guzik va = NULL; 78482c174a3SMateusz Guzik if (malloc_dbg(&va, &size, mtp, flags) != 0) 78582c174a3SMateusz Guzik return (va); 78682c174a3SMateusz Guzik #endif 78782c174a3SMateusz Guzik /* Policy is handled by kmem. */ 78882c174a3SMateusz Guzik va = malloc_large(&size, ds, flags); 78982c174a3SMateusz Guzik malloc_type_allocated(mtp, va == NULL ? 0 : size); 79082c174a3SMateusz Guzik if (__predict_false(va == NULL)) { 79182c174a3SMateusz Guzik KASSERT((flags & M_WAITOK) == 0, 79282c174a3SMateusz Guzik ("malloc(M_WAITOK) returned NULL")); 79382c174a3SMateusz Guzik t_malloc_fail = time_uptime; 79482c174a3SMateusz Guzik } 79582c174a3SMateusz Guzik #ifdef DEBUG_REDZONE 79682c174a3SMateusz Guzik if (va != NULL) 79782c174a3SMateusz Guzik va = redzone_setup(va, osize); 79882c174a3SMateusz Guzik #endif 79982c174a3SMateusz Guzik return (va); 8009978bd99SMark Johnston } 8019978bd99SMark Johnston 8029978bd99SMark Johnston void * 803fd91e076SKristof Provost mallocarray(size_t nmemb, size_t size, struct malloc_type *type, int flags) 804fd91e076SKristof Provost { 805fd91e076SKristof Provost 806c02fc960SConrad Meyer if (WOULD_OVERFLOW(nmemb, size)) 807c02fc960SConrad Meyer panic("mallocarray: %zu * %zu overflowed", nmemb, size); 808fd91e076SKristof Provost 809fd91e076SKristof Provost return (malloc(size * nmemb, type, flags)); 810fd91e076SKristof Provost } 811fd91e076SKristof Provost 812ab3185d1SJeff Roberson #ifdef INVARIANTS 813ab3185d1SJeff Roberson static void 814ab3185d1SJeff Roberson free_save_type(void *addr, struct malloc_type *mtp, u_long size) 815ab3185d1SJeff Roberson { 816ab3185d1SJeff Roberson struct malloc_type **mtpp = addr; 817ab3185d1SJeff Roberson 818ab3185d1SJeff Roberson /* 819ab3185d1SJeff Roberson * Cache a pointer to the malloc_type that most recently freed 820ab3185d1SJeff Roberson * this memory here. This way we know who is most likely to 821ab3185d1SJeff Roberson * have stepped on it later. 822ab3185d1SJeff Roberson * 823ab3185d1SJeff Roberson * This code assumes that size is a multiple of 8 bytes for 824ab3185d1SJeff Roberson * 64 bit machines 825ab3185d1SJeff Roberson */ 826ab3185d1SJeff Roberson mtpp = (struct malloc_type **) ((unsigned long)mtpp & ~UMA_ALIGN_PTR); 827ab3185d1SJeff Roberson mtpp += (size - sizeof(struct malloc_type *)) / 828ab3185d1SJeff Roberson sizeof(struct malloc_type *); 829ab3185d1SJeff Roberson *mtpp = mtp; 830ab3185d1SJeff Roberson } 831ab3185d1SJeff Roberson #endif 832ab3185d1SJeff Roberson 833ab3185d1SJeff Roberson #ifdef MALLOC_DEBUG 834ab3185d1SJeff Roberson static int 835ab3185d1SJeff Roberson free_dbg(void **addrp, struct malloc_type *mtp) 836ab3185d1SJeff Roberson { 837ab3185d1SJeff Roberson void *addr; 838ab3185d1SJeff Roberson 839ab3185d1SJeff Roberson addr = *addrp; 840bdcc2226SMateusz Guzik KASSERT(mtp->ks_version == M_VERSION, ("free: bad malloc type version")); 841ab3185d1SJeff Roberson KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 842ab3185d1SJeff Roberson ("free: called with spinlock or critical section held")); 843ab3185d1SJeff Roberson 844ab3185d1SJeff Roberson /* free(NULL, ...) does nothing */ 845ab3185d1SJeff Roberson if (addr == NULL) 846ab3185d1SJeff Roberson return (EJUSTRETURN); 847ab3185d1SJeff Roberson 848ab3185d1SJeff Roberson #ifdef DEBUG_MEMGUARD 849ab3185d1SJeff Roberson if (is_memguard_addr(addr)) { 850ab3185d1SJeff Roberson memguard_free(addr); 851ab3185d1SJeff Roberson return (EJUSTRETURN); 852ab3185d1SJeff Roberson } 853ab3185d1SJeff Roberson #endif 854ab3185d1SJeff Roberson 855ab3185d1SJeff Roberson #ifdef DEBUG_REDZONE 856ab3185d1SJeff Roberson redzone_check(addr); 857ab3185d1SJeff Roberson *addrp = redzone_addr_ntor(addr); 858ab3185d1SJeff Roberson #endif 859ab3185d1SJeff Roberson 860ab3185d1SJeff Roberson return (0); 861ab3185d1SJeff Roberson } 862ab3185d1SJeff Roberson #endif 863ab3185d1SJeff Roberson 864fd91e076SKristof Provost /* 8651c7c3c6aSMatthew Dillon * free: 8661c7c3c6aSMatthew Dillon * 867df8bae1dSRodney W. Grimes * Free a block of memory allocated by malloc. 8681c7c3c6aSMatthew Dillon * 8691c7c3c6aSMatthew Dillon * This routine may not block. 870df8bae1dSRodney W. Grimes */ 871df8bae1dSRodney W. Grimes void 87263a7e0a3SRobert Watson free(void *addr, struct malloc_type *mtp) 873df8bae1dSRodney W. Grimes { 874584061b4SJeff Roberson uma_zone_t zone; 87599571dc3SJeff Roberson uma_slab_t slab; 87699571dc3SJeff Roberson u_long size; 877254c6cb3SPoul-Henning Kamp 878ab3185d1SJeff Roberson #ifdef MALLOC_DEBUG 879ab3185d1SJeff Roberson if (free_dbg(&addr, mtp) != 0) 880ab3185d1SJeff Roberson return; 881ab3185d1SJeff Roberson #endif 88244a8ff31SArchie Cobbs /* free(NULL, ...) does nothing */ 88344a8ff31SArchie Cobbs if (addr == NULL) 88444a8ff31SArchie Cobbs return; 88544a8ff31SArchie Cobbs 886584061b4SJeff Roberson vtozoneslab((vm_offset_t)addr & (~UMA_SLAB_MASK), &zone, &slab); 8878355f576SJeff Roberson if (slab == NULL) 8886f267175SJeff Roberson panic("free: address %p(%p) has not been allocated.\n", 88999571dc3SJeff Roberson addr, (void *)((u_long)addr & (~UMA_SLAB_MASK))); 89099571dc3SJeff Roberson 8916d6a03d7SJeff Roberson if (__predict_true(!malloc_large_slab(slab))) { 892584061b4SJeff Roberson size = zone->uz_size; 8938f70816cSJeff Roberson #ifdef INVARIANTS 894ab3185d1SJeff Roberson free_save_type(addr, mtp, size); 8958f70816cSJeff Roberson #endif 896584061b4SJeff Roberson uma_zfree_arg(zone, addr, slab); 89714bf02f8SJohn Dyson } else { 8986d6a03d7SJeff Roberson size = malloc_large_size(slab); 8996d6a03d7SJeff Roberson free_large(addr, size); 90014bf02f8SJohn Dyson } 90163a7e0a3SRobert Watson malloc_type_freed(mtp, size); 902df8bae1dSRodney W. Grimes } 903df8bae1dSRodney W. Grimes 90445035becSMatt Macy /* 90545035becSMatt Macy * zfree: 90645035becSMatt Macy * 90745035becSMatt Macy * Zero then free a block of memory allocated by malloc. 90845035becSMatt Macy * 90945035becSMatt Macy * This routine may not block. 91045035becSMatt Macy */ 91145035becSMatt Macy void 91245035becSMatt Macy zfree(void *addr, struct malloc_type *mtp) 91345035becSMatt Macy { 91445035becSMatt Macy uma_zone_t zone; 91545035becSMatt Macy uma_slab_t slab; 91645035becSMatt Macy u_long size; 91745035becSMatt Macy 91845035becSMatt Macy #ifdef MALLOC_DEBUG 91945035becSMatt Macy if (free_dbg(&addr, mtp) != 0) 92045035becSMatt Macy return; 92145035becSMatt Macy #endif 92245035becSMatt Macy /* free(NULL, ...) does nothing */ 92345035becSMatt Macy if (addr == NULL) 92445035becSMatt Macy return; 92545035becSMatt Macy 92645035becSMatt Macy vtozoneslab((vm_offset_t)addr & (~UMA_SLAB_MASK), &zone, &slab); 92745035becSMatt Macy if (slab == NULL) 92845035becSMatt Macy panic("free: address %p(%p) has not been allocated.\n", 92945035becSMatt Macy addr, (void *)((u_long)addr & (~UMA_SLAB_MASK))); 93045035becSMatt Macy 93145035becSMatt Macy if (__predict_true(!malloc_large_slab(slab))) { 93245035becSMatt Macy size = zone->uz_size; 93345035becSMatt Macy #ifdef INVARIANTS 93445035becSMatt Macy free_save_type(addr, mtp, size); 93545035becSMatt Macy #endif 93645035becSMatt Macy explicit_bzero(addr, size); 93745035becSMatt Macy uma_zfree_arg(zone, addr, slab); 93845035becSMatt Macy } else { 93945035becSMatt Macy size = malloc_large_size(slab); 94045035becSMatt Macy explicit_bzero(addr, size); 94145035becSMatt Macy free_large(addr, size); 94245035becSMatt Macy } 94345035becSMatt Macy malloc_type_freed(mtp, size); 94445035becSMatt Macy } 94545035becSMatt Macy 946df8bae1dSRodney W. Grimes /* 94744a8ff31SArchie Cobbs * realloc: change the size of a memory block 94844a8ff31SArchie Cobbs */ 94944a8ff31SArchie Cobbs void * 950bd555da9SConrad Meyer realloc(void *addr, size_t size, struct malloc_type *mtp, int flags) 95144a8ff31SArchie Cobbs { 952584061b4SJeff Roberson uma_zone_t zone; 9538355f576SJeff Roberson uma_slab_t slab; 95444a8ff31SArchie Cobbs unsigned long alloc; 95544a8ff31SArchie Cobbs void *newaddr; 95644a8ff31SArchie Cobbs 957bdcc2226SMateusz Guzik KASSERT(mtp->ks_version == M_VERSION, 958bdcc2226SMateusz Guzik ("realloc: bad malloc type version")); 959d9e2e68dSMark Johnston KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 9601067a2baSJonathan T. Looney ("realloc: called with spinlock or critical section held")); 9611067a2baSJonathan T. Looney 96244a8ff31SArchie Cobbs /* realloc(NULL, ...) is equivalent to malloc(...) */ 96344a8ff31SArchie Cobbs if (addr == NULL) 96463a7e0a3SRobert Watson return (malloc(size, mtp, flags)); 96563a7e0a3SRobert Watson 96663a7e0a3SRobert Watson /* 96763a7e0a3SRobert Watson * XXX: Should report free of old memory and alloc of new memory to 96863a7e0a3SRobert Watson * per-CPU stats. 96963a7e0a3SRobert Watson */ 97044a8ff31SArchie Cobbs 971e4eb384bSBosko Milekic #ifdef DEBUG_MEMGUARD 9726d3ed393SMatthew D Fleming if (is_memguard_addr(addr)) 9736d3ed393SMatthew D Fleming return (memguard_realloc(addr, size, mtp, flags)); 974e4eb384bSBosko Milekic #endif 975e4eb384bSBosko Milekic 976847a2a17SPawel Jakub Dawidek #ifdef DEBUG_REDZONE 977847a2a17SPawel Jakub Dawidek slab = NULL; 978b476ae7fSJeff Roberson zone = NULL; 979847a2a17SPawel Jakub Dawidek alloc = redzone_get_size(addr); 980847a2a17SPawel Jakub Dawidek #else 981584061b4SJeff Roberson vtozoneslab((vm_offset_t)addr & (~UMA_SLAB_MASK), &zone, &slab); 9828355f576SJeff Roberson 98344a8ff31SArchie Cobbs /* Sanity check */ 9848355f576SJeff Roberson KASSERT(slab != NULL, 98544a8ff31SArchie Cobbs ("realloc: address %p out of range", (void *)addr)); 98644a8ff31SArchie Cobbs 98744a8ff31SArchie Cobbs /* Get the size of the original block */ 9886d6a03d7SJeff Roberson if (!malloc_large_slab(slab)) 989584061b4SJeff Roberson alloc = zone->uz_size; 9908355f576SJeff Roberson else 9916d6a03d7SJeff Roberson alloc = malloc_large_size(slab); 99244a8ff31SArchie Cobbs 99344a8ff31SArchie Cobbs /* Reuse the original block if appropriate */ 99444a8ff31SArchie Cobbs if (size <= alloc 99544a8ff31SArchie Cobbs && (size > (alloc >> REALLOC_FRACTION) || alloc == MINALLOCSIZE)) 99644a8ff31SArchie Cobbs return (addr); 997847a2a17SPawel Jakub Dawidek #endif /* !DEBUG_REDZONE */ 99844a8ff31SArchie Cobbs 99944a8ff31SArchie Cobbs /* Allocate a new, bigger (or smaller) block */ 100063a7e0a3SRobert Watson if ((newaddr = malloc(size, mtp, flags)) == NULL) 100144a8ff31SArchie Cobbs return (NULL); 100244a8ff31SArchie Cobbs 100344a8ff31SArchie Cobbs /* Copy over original contents */ 100444a8ff31SArchie Cobbs bcopy(addr, newaddr, min(size, alloc)); 100563a7e0a3SRobert Watson free(addr, mtp); 100644a8ff31SArchie Cobbs return (newaddr); 100744a8ff31SArchie Cobbs } 100844a8ff31SArchie Cobbs 100944a8ff31SArchie Cobbs /* 101044a8ff31SArchie Cobbs * reallocf: same as realloc() but free memory on failure. 101144a8ff31SArchie Cobbs */ 101244a8ff31SArchie Cobbs void * 1013bd555da9SConrad Meyer reallocf(void *addr, size_t size, struct malloc_type *mtp, int flags) 101444a8ff31SArchie Cobbs { 101544a8ff31SArchie Cobbs void *mem; 101644a8ff31SArchie Cobbs 101763a7e0a3SRobert Watson if ((mem = realloc(addr, size, mtp, flags)) == NULL) 101863a7e0a3SRobert Watson free(addr, mtp); 101944a8ff31SArchie Cobbs return (mem); 102044a8ff31SArchie Cobbs } 102144a8ff31SArchie Cobbs 10225d4bf057SVladimir Kondratyev /* 102316b971edSMateusz Guzik * malloc_size: returns the number of bytes allocated for a request of the 102416b971edSMateusz Guzik * specified size 102516b971edSMateusz Guzik */ 102616b971edSMateusz Guzik size_t 102716b971edSMateusz Guzik malloc_size(size_t size) 102816b971edSMateusz Guzik { 102916b971edSMateusz Guzik int indx; 103016b971edSMateusz Guzik 103116b971edSMateusz Guzik if (size > kmem_zmax) 103216b971edSMateusz Guzik return (0); 103316b971edSMateusz Guzik if (size & KMEM_ZMASK) 103416b971edSMateusz Guzik size = (size & ~KMEM_ZMASK) + KMEM_ZBASE; 103516b971edSMateusz Guzik indx = kmemsize[size >> KMEM_ZSHIFT]; 103616b971edSMateusz Guzik return (kmemzones[indx].kz_size); 103716b971edSMateusz Guzik } 103816b971edSMateusz Guzik 103916b971edSMateusz Guzik /* 10405d4bf057SVladimir Kondratyev * malloc_usable_size: returns the usable size of the allocation. 10415d4bf057SVladimir Kondratyev */ 10425d4bf057SVladimir Kondratyev size_t 10435d4bf057SVladimir Kondratyev malloc_usable_size(const void *addr) 10445d4bf057SVladimir Kondratyev { 10455d4bf057SVladimir Kondratyev #ifndef DEBUG_REDZONE 10465d4bf057SVladimir Kondratyev uma_zone_t zone; 10475d4bf057SVladimir Kondratyev uma_slab_t slab; 10485d4bf057SVladimir Kondratyev #endif 10495d4bf057SVladimir Kondratyev u_long size; 10505d4bf057SVladimir Kondratyev 10515d4bf057SVladimir Kondratyev if (addr == NULL) 10525d4bf057SVladimir Kondratyev return (0); 10535d4bf057SVladimir Kondratyev 10545d4bf057SVladimir Kondratyev #ifdef DEBUG_MEMGUARD 10555d4bf057SVladimir Kondratyev if (is_memguard_addr(__DECONST(void *, addr))) 10565d4bf057SVladimir Kondratyev return (memguard_get_req_size(addr)); 10575d4bf057SVladimir Kondratyev #endif 10585d4bf057SVladimir Kondratyev 10595d4bf057SVladimir Kondratyev #ifdef DEBUG_REDZONE 10605d4bf057SVladimir Kondratyev size = redzone_get_size(__DECONST(void *, addr)); 10615d4bf057SVladimir Kondratyev #else 10625d4bf057SVladimir Kondratyev vtozoneslab((vm_offset_t)addr & (~UMA_SLAB_MASK), &zone, &slab); 10635d4bf057SVladimir Kondratyev if (slab == NULL) 10645d4bf057SVladimir Kondratyev panic("malloc_usable_size: address %p(%p) is not allocated.\n", 10655d4bf057SVladimir Kondratyev addr, (void *)((u_long)addr & (~UMA_SLAB_MASK))); 10665d4bf057SVladimir Kondratyev 10675d4bf057SVladimir Kondratyev if (!malloc_large_slab(slab)) 10685d4bf057SVladimir Kondratyev size = zone->uz_size; 10695d4bf057SVladimir Kondratyev else 10705d4bf057SVladimir Kondratyev size = malloc_large_size(slab); 10715d4bf057SVladimir Kondratyev #endif 10725d4bf057SVladimir Kondratyev return (size); 10735d4bf057SVladimir Kondratyev } 10745d4bf057SVladimir Kondratyev 1075c70af487SAlan Cox CTASSERT(VM_KMEM_SIZE_SCALE >= 1); 1076c70af487SAlan Cox 10775df87b21SJeff Roberson /* 1078c70af487SAlan Cox * Initialize the kernel memory (kmem) arena. 10795df87b21SJeff Roberson */ 10805df87b21SJeff Roberson void 10815df87b21SJeff Roberson kmeminit(void) 10825df87b21SJeff Roberson { 1083af3b2549SHans Petter Selasky u_long mem_size; 1084af3b2549SHans Petter Selasky u_long tmp; 108569ef67f9SJason Evans 1086af3b2549SHans Petter Selasky #ifdef VM_KMEM_SIZE 1087af3b2549SHans Petter Selasky if (vm_kmem_size == 0) 1088af3b2549SHans Petter Selasky vm_kmem_size = VM_KMEM_SIZE; 1089af3b2549SHans Petter Selasky #endif 1090af3b2549SHans Petter Selasky #ifdef VM_KMEM_SIZE_MIN 1091af3b2549SHans Petter Selasky if (vm_kmem_size_min == 0) 1092af3b2549SHans Petter Selasky vm_kmem_size_min = VM_KMEM_SIZE_MIN; 1093af3b2549SHans Petter Selasky #endif 1094af3b2549SHans Petter Selasky #ifdef VM_KMEM_SIZE_MAX 1095af3b2549SHans Petter Selasky if (vm_kmem_size_max == 0) 1096af3b2549SHans Petter Selasky vm_kmem_size_max = VM_KMEM_SIZE_MAX; 1097af3b2549SHans Petter Selasky #endif 10988a58a9f6SJohn Dyson /* 1099c70af487SAlan Cox * Calculate the amount of kernel virtual address (KVA) space that is 1100c70af487SAlan Cox * preallocated to the kmem arena. In order to support a wide range 1101c70af487SAlan Cox * of machines, it is a function of the physical memory size, 1102c70af487SAlan Cox * specifically, 11038a58a9f6SJohn Dyson * 1104c70af487SAlan Cox * min(max(physical memory size / VM_KMEM_SIZE_SCALE, 1105c70af487SAlan Cox * VM_KMEM_SIZE_MIN), VM_KMEM_SIZE_MAX) 1106c70af487SAlan Cox * 1107c70af487SAlan Cox * Every architecture must define an integral value for 1108c70af487SAlan Cox * VM_KMEM_SIZE_SCALE. However, the definitions of VM_KMEM_SIZE_MIN 1109c70af487SAlan Cox * and VM_KMEM_SIZE_MAX, which represent respectively the floor and 1110c70af487SAlan Cox * ceiling on this preallocation, are optional. Typically, 1111c70af487SAlan Cox * VM_KMEM_SIZE_MAX is itself a function of the available KVA space on 1112c70af487SAlan Cox * a given architecture. 11138a58a9f6SJohn Dyson */ 111444f1c916SBryan Drewery mem_size = vm_cnt.v_page_count; 11157c51714eSSean Bruno if (mem_size <= 32768) /* delphij XXX 128MB */ 11167c51714eSSean Bruno kmem_zmax = PAGE_SIZE; 11178a58a9f6SJohn Dyson 1118c70af487SAlan Cox if (vm_kmem_size_scale < 1) 1119c70af487SAlan Cox vm_kmem_size_scale = VM_KMEM_SIZE_SCALE; 1120c70af487SAlan Cox 1121af3b2549SHans Petter Selasky /* 1122af3b2549SHans Petter Selasky * Check if we should use defaults for the "vm_kmem_size" 1123af3b2549SHans Petter Selasky * variable: 1124af3b2549SHans Petter Selasky */ 1125af3b2549SHans Petter Selasky if (vm_kmem_size == 0) { 112628b740daSKonstantin Belousov vm_kmem_size = mem_size / vm_kmem_size_scale; 112728b740daSKonstantin Belousov vm_kmem_size = vm_kmem_size * PAGE_SIZE < vm_kmem_size ? 112828b740daSKonstantin Belousov vm_kmem_size_max : vm_kmem_size * PAGE_SIZE; 1129c70af487SAlan Cox if (vm_kmem_size_min > 0 && vm_kmem_size < vm_kmem_size_min) 11300e5179e4SStephane E. Potvin vm_kmem_size = vm_kmem_size_min; 1131479439b4SDag-Erling Smørgrav if (vm_kmem_size_max > 0 && vm_kmem_size >= vm_kmem_size_max) 1132479439b4SDag-Erling Smørgrav vm_kmem_size = vm_kmem_size_max; 1133af3b2549SHans Petter Selasky } 113428b740daSKonstantin Belousov if (vm_kmem_size == 0) 113528b740daSKonstantin Belousov panic("Tune VM_KMEM_SIZE_* for the platform"); 11368a58a9f6SJohn Dyson 113727b8623fSDavid Greenman /* 1138af3b2549SHans Petter Selasky * The amount of KVA space that is preallocated to the 1139c70af487SAlan Cox * kmem arena can be set statically at compile-time or manually 1140c70af487SAlan Cox * through the kernel environment. However, it is still limited to 1141c70af487SAlan Cox * twice the physical memory size, which has been sufficient to handle 1142c70af487SAlan Cox * the most severe cases of external fragmentation in the kmem arena. 114327b8623fSDavid Greenman */ 1144c749c003SAlan Cox if (vm_kmem_size / 2 / PAGE_SIZE > mem_size) 1145c749c003SAlan Cox vm_kmem_size = 2 * mem_size * PAGE_SIZE; 11468a58a9f6SJohn Dyson 1147e137643eSOlivier Houchard vm_kmem_size = round_page(vm_kmem_size); 1148e3813573SMatthew D Fleming #ifdef DEBUG_MEMGUARD 1149f806cdcfSMatthew D Fleming tmp = memguard_fudge(vm_kmem_size, kernel_map); 1150e3813573SMatthew D Fleming #else 1151e3813573SMatthew D Fleming tmp = vm_kmem_size; 1152e3813573SMatthew D Fleming #endif 11532e47807cSJeff Roberson uma_set_limit(tmp); 11548355f576SJeff Roberson 1155e4eb384bSBosko Milekic #ifdef DEBUG_MEMGUARD 1156e4eb384bSBosko Milekic /* 1157e4eb384bSBosko Milekic * Initialize MemGuard if support compiled in. MemGuard is a 1158e4eb384bSBosko Milekic * replacement allocator used for detecting tamper-after-free 1159e4eb384bSBosko Milekic * scenarios as they occur. It is only used for debugging. 1160e4eb384bSBosko Milekic */ 11612e47807cSJeff Roberson memguard_init(kernel_arena); 1162e4eb384bSBosko Milekic #endif 11635df87b21SJeff Roberson } 11645df87b21SJeff Roberson 11655df87b21SJeff Roberson /* 11665df87b21SJeff Roberson * Initialize the kernel memory allocator 11675df87b21SJeff Roberson */ 11685df87b21SJeff Roberson /* ARGSUSED*/ 11695df87b21SJeff Roberson static void 11705df87b21SJeff Roberson mallocinit(void *dummy) 11715df87b21SJeff Roberson { 11725df87b21SJeff Roberson int i; 11735df87b21SJeff Roberson uint8_t indx; 11745df87b21SJeff Roberson 11755df87b21SJeff Roberson mtx_init(&malloc_mtx, "malloc", NULL, MTX_DEF); 11765df87b21SJeff Roberson 11775df87b21SJeff Roberson kmeminit(); 1178e4eb384bSBosko Milekic 11797001d850SXin LI if (kmem_zmax < PAGE_SIZE || kmem_zmax > KMEM_ZMAX) 11807001d850SXin LI kmem_zmax = KMEM_ZMAX; 11817001d850SXin LI 11826f267175SJeff Roberson for (i = 0, indx = 0; kmemzones[indx].kz_size != 0; indx++) { 11836f267175SJeff Roberson int size = kmemzones[indx].kz_size; 1184eaa17d42SRyan Libby const char *name = kmemzones[indx].kz_name; 1185d7854da1SMatthew D Fleming int subzone; 11868355f576SJeff Roberson 1187d7854da1SMatthew D Fleming for (subzone = 0; subzone < numzones; subzone++) { 1188d7854da1SMatthew D Fleming kmemzones[indx].kz_zone[subzone] = 1189d7854da1SMatthew D Fleming uma_zcreate(name, size, 11908efc4effSJeff Roberson #ifdef INVARIANTS 11918f70816cSJeff Roberson mtrash_ctor, mtrash_dtor, mtrash_init, mtrash_fini, 11928efc4effSJeff Roberson #else 11938efc4effSJeff Roberson NULL, NULL, NULL, NULL, 11948efc4effSJeff Roberson #endif 11958efc4effSJeff Roberson UMA_ALIGN_PTR, UMA_ZONE_MALLOC); 1196d7854da1SMatthew D Fleming } 11978355f576SJeff Roberson for (;i <= size; i+= KMEM_ZBASE) 11986f267175SJeff Roberson kmemsize[i >> KMEM_ZSHIFT] = indx; 1199df8bae1dSRodney W. Grimes } 1200254c6cb3SPoul-Henning Kamp } 1201af3b2549SHans Petter Selasky SYSINIT(kmem, SI_SUB_KMEM, SI_ORDER_SECOND, mallocinit, NULL); 1202254c6cb3SPoul-Henning Kamp 1203db669378SPeter Wemm void 120487efd4d5SRobert Watson malloc_init(void *data) 1205254c6cb3SPoul-Henning Kamp { 120663a7e0a3SRobert Watson struct malloc_type_internal *mtip; 120763a7e0a3SRobert Watson struct malloc_type *mtp; 120863a7e0a3SRobert Watson 120944f1c916SBryan Drewery KASSERT(vm_cnt.v_page_count != 0, ("malloc_register before vm_init")); 121063a7e0a3SRobert Watson 121163a7e0a3SRobert Watson mtp = data; 1212bdcc2226SMateusz Guzik if (mtp->ks_version != M_VERSION) 1213e25d8b67SMateusz Guzik panic("malloc_init: type %s with unsupported version %lu", 1214e25d8b67SMateusz Guzik mtp->ks_shortdesc, mtp->ks_version); 1215bb1c7df8SRobert Watson 1216bdcc2226SMateusz Guzik mtip = &mtp->ks_mti; 1217*8e6526e9SMateusz Guzik mtip->mti_stats = uma_zalloc_pcpu(pcpu_zone_64, M_WAITOK | M_ZERO); 1218c9e05ccdSMateusz Guzik mtp_set_subzone(mtp); 1219254c6cb3SPoul-Henning Kamp 12206f267175SJeff Roberson mtx_lock(&malloc_mtx); 122163a7e0a3SRobert Watson mtp->ks_next = kmemstatistics; 122263a7e0a3SRobert Watson kmemstatistics = mtp; 1223cd814b26SRobert Watson kmemcount++; 12246f267175SJeff Roberson mtx_unlock(&malloc_mtx); 1225df8bae1dSRodney W. Grimes } 1226db669378SPeter Wemm 1227db669378SPeter Wemm void 122887efd4d5SRobert Watson malloc_uninit(void *data) 1229db669378SPeter Wemm { 123063a7e0a3SRobert Watson struct malloc_type_internal *mtip; 12312a143d5bSPawel Jakub Dawidek struct malloc_type_stats *mtsp; 123263a7e0a3SRobert Watson struct malloc_type *mtp, *temp; 12332a143d5bSPawel Jakub Dawidek long temp_allocs, temp_bytes; 12342a143d5bSPawel Jakub Dawidek int i; 1235db669378SPeter Wemm 123663a7e0a3SRobert Watson mtp = data; 1237bdcc2226SMateusz Guzik KASSERT(mtp->ks_version == M_VERSION, 1238bdcc2226SMateusz Guzik ("malloc_uninit: bad malloc type version")); 1239bb1c7df8SRobert Watson 12406f267175SJeff Roberson mtx_lock(&malloc_mtx); 1241bdcc2226SMateusz Guzik mtip = &mtp->ks_mti; 124263a7e0a3SRobert Watson if (mtp != kmemstatistics) { 124363a7e0a3SRobert Watson for (temp = kmemstatistics; temp != NULL; 124463a7e0a3SRobert Watson temp = temp->ks_next) { 1245f121baaaSBrian Somers if (temp->ks_next == mtp) { 124663a7e0a3SRobert Watson temp->ks_next = mtp->ks_next; 1247f121baaaSBrian Somers break; 1248db669378SPeter Wemm } 1249f121baaaSBrian Somers } 1250f121baaaSBrian Somers KASSERT(temp, 1251f121baaaSBrian Somers ("malloc_uninit: type '%s' not found", mtp->ks_shortdesc)); 125263a7e0a3SRobert Watson } else 125363a7e0a3SRobert Watson kmemstatistics = mtp->ks_next; 1254cd814b26SRobert Watson kmemcount--; 12556f267175SJeff Roberson mtx_unlock(&malloc_mtx); 12562a143d5bSPawel Jakub Dawidek 12572a143d5bSPawel Jakub Dawidek /* 12582a143d5bSPawel Jakub Dawidek * Look for memory leaks. 12592a143d5bSPawel Jakub Dawidek */ 12602a143d5bSPawel Jakub Dawidek temp_allocs = temp_bytes = 0; 12619afff6b1SMateusz Guzik for (i = 0; i <= mp_maxid; i++) { 12629afff6b1SMateusz Guzik mtsp = zpcpu_get_cpu(mtip->mti_stats, i); 12632a143d5bSPawel Jakub Dawidek temp_allocs += mtsp->mts_numallocs; 12642a143d5bSPawel Jakub Dawidek temp_allocs -= mtsp->mts_numfrees; 12652a143d5bSPawel Jakub Dawidek temp_bytes += mtsp->mts_memalloced; 12662a143d5bSPawel Jakub Dawidek temp_bytes -= mtsp->mts_memfreed; 12672a143d5bSPawel Jakub Dawidek } 12682a143d5bSPawel Jakub Dawidek if (temp_allocs > 0 || temp_bytes > 0) { 12692a143d5bSPawel Jakub Dawidek printf("Warning: memory type %s leaked memory on destroy " 12702a143d5bSPawel Jakub Dawidek "(%ld allocations, %ld bytes leaked).\n", mtp->ks_shortdesc, 12712a143d5bSPawel Jakub Dawidek temp_allocs, temp_bytes); 12722a143d5bSPawel Jakub Dawidek } 12732a143d5bSPawel Jakub Dawidek 1274*8e6526e9SMateusz Guzik uma_zfree_pcpu(pcpu_zone_64, mtip->mti_stats); 1275db669378SPeter Wemm } 12766f267175SJeff Roberson 1277d362c40dSPawel Jakub Dawidek struct malloc_type * 1278d362c40dSPawel Jakub Dawidek malloc_desc2type(const char *desc) 1279d362c40dSPawel Jakub Dawidek { 1280d362c40dSPawel Jakub Dawidek struct malloc_type *mtp; 1281d362c40dSPawel Jakub Dawidek 1282d362c40dSPawel Jakub Dawidek mtx_assert(&malloc_mtx, MA_OWNED); 1283d362c40dSPawel Jakub Dawidek for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) { 1284d362c40dSPawel Jakub Dawidek if (strcmp(mtp->ks_shortdesc, desc) == 0) 1285d362c40dSPawel Jakub Dawidek return (mtp); 1286d362c40dSPawel Jakub Dawidek } 1287d362c40dSPawel Jakub Dawidek return (NULL); 1288d362c40dSPawel Jakub Dawidek } 1289d362c40dSPawel Jakub Dawidek 12906f267175SJeff Roberson static int 1291cd814b26SRobert Watson sysctl_kern_malloc_stats(SYSCTL_HANDLER_ARGS) 1292cd814b26SRobert Watson { 1293cd814b26SRobert Watson struct malloc_type_stream_header mtsh; 1294cd814b26SRobert Watson struct malloc_type_internal *mtip; 12959afff6b1SMateusz Guzik struct malloc_type_stats *mtsp, zeromts; 1296cd814b26SRobert Watson struct malloc_type_header mth; 1297cd814b26SRobert Watson struct malloc_type *mtp; 12984e657159SMatthew D Fleming int error, i; 1299cd814b26SRobert Watson struct sbuf sbuf; 1300cd814b26SRobert Watson 130100f0e671SMatthew D Fleming error = sysctl_wire_old_buffer(req, 0); 130200f0e671SMatthew D Fleming if (error != 0) 130300f0e671SMatthew D Fleming return (error); 13044e657159SMatthew D Fleming sbuf_new_for_sysctl(&sbuf, NULL, 128, req); 13051eafc078SIan Lepore sbuf_clear_flags(&sbuf, SBUF_INCLUDENUL); 1306cd814b26SRobert Watson mtx_lock(&malloc_mtx); 1307cd814b26SRobert Watson 13089afff6b1SMateusz Guzik bzero(&zeromts, sizeof(zeromts)); 13099afff6b1SMateusz Guzik 1310cd814b26SRobert Watson /* 1311cd814b26SRobert Watson * Insert stream header. 1312cd814b26SRobert Watson */ 1313cd814b26SRobert Watson bzero(&mtsh, sizeof(mtsh)); 1314cd814b26SRobert Watson mtsh.mtsh_version = MALLOC_TYPE_STREAM_VERSION; 1315cd814b26SRobert Watson mtsh.mtsh_maxcpus = MAXCPU; 1316cd814b26SRobert Watson mtsh.mtsh_count = kmemcount; 13174e657159SMatthew D Fleming (void)sbuf_bcat(&sbuf, &mtsh, sizeof(mtsh)); 1318cd814b26SRobert Watson 1319cd814b26SRobert Watson /* 1320cd814b26SRobert Watson * Insert alternating sequence of type headers and type statistics. 1321cd814b26SRobert Watson */ 1322cd814b26SRobert Watson for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) { 1323bdcc2226SMateusz Guzik mtip = &mtp->ks_mti; 1324cd814b26SRobert Watson 1325cd814b26SRobert Watson /* 1326cd814b26SRobert Watson * Insert type header. 1327cd814b26SRobert Watson */ 1328cd814b26SRobert Watson bzero(&mth, sizeof(mth)); 1329cd814b26SRobert Watson strlcpy(mth.mth_name, mtp->ks_shortdesc, MALLOC_MAX_NAME); 13304e657159SMatthew D Fleming (void)sbuf_bcat(&sbuf, &mth, sizeof(mth)); 1331cd814b26SRobert Watson 1332cd814b26SRobert Watson /* 1333cd814b26SRobert Watson * Insert type statistics for each CPU. 1334cd814b26SRobert Watson */ 13359afff6b1SMateusz Guzik for (i = 0; i <= mp_maxid; i++) { 13369afff6b1SMateusz Guzik mtsp = zpcpu_get_cpu(mtip->mti_stats, i); 13379afff6b1SMateusz Guzik (void)sbuf_bcat(&sbuf, mtsp, sizeof(*mtsp)); 1338cd814b26SRobert Watson } 13399afff6b1SMateusz Guzik /* 13409afff6b1SMateusz Guzik * Fill in the missing CPUs. 13419afff6b1SMateusz Guzik */ 13429afff6b1SMateusz Guzik for (; i < MAXCPU; i++) { 13439afff6b1SMateusz Guzik (void)sbuf_bcat(&sbuf, &zeromts, sizeof(zeromts)); 13449afff6b1SMateusz Guzik } 1345cd814b26SRobert Watson } 1346cd814b26SRobert Watson mtx_unlock(&malloc_mtx); 13474e657159SMatthew D Fleming error = sbuf_finish(&sbuf); 1348cd814b26SRobert Watson sbuf_delete(&sbuf); 1349cd814b26SRobert Watson return (error); 1350cd814b26SRobert Watson } 1351cd814b26SRobert Watson 13527029da5cSPawel Biernacki SYSCTL_PROC(_kern, OID_AUTO, malloc_stats, 13537029da5cSPawel Biernacki CTLFLAG_RD | CTLTYPE_STRUCT | CTLFLAG_MPSAFE, 0, 0, 13547029da5cSPawel Biernacki sysctl_kern_malloc_stats, "s,malloc_type_ustats", 1355cd814b26SRobert Watson "Return malloc types"); 1356cd814b26SRobert Watson 1357cd814b26SRobert Watson SYSCTL_INT(_kern, OID_AUTO, malloc_count, CTLFLAG_RD, &kmemcount, 0, 1358cd814b26SRobert Watson "Count of kernel malloc types"); 1359cd814b26SRobert Watson 136091dd776cSJohn Birrell void 136191dd776cSJohn Birrell malloc_type_list(malloc_type_list_func_t *func, void *arg) 136291dd776cSJohn Birrell { 136391dd776cSJohn Birrell struct malloc_type *mtp, **bufmtp; 136491dd776cSJohn Birrell int count, i; 136591dd776cSJohn Birrell size_t buflen; 136691dd776cSJohn Birrell 136791dd776cSJohn Birrell mtx_lock(&malloc_mtx); 136891dd776cSJohn Birrell restart: 136991dd776cSJohn Birrell mtx_assert(&malloc_mtx, MA_OWNED); 137091dd776cSJohn Birrell count = kmemcount; 137191dd776cSJohn Birrell mtx_unlock(&malloc_mtx); 137291dd776cSJohn Birrell 137391dd776cSJohn Birrell buflen = sizeof(struct malloc_type *) * count; 137491dd776cSJohn Birrell bufmtp = malloc(buflen, M_TEMP, M_WAITOK); 137591dd776cSJohn Birrell 137691dd776cSJohn Birrell mtx_lock(&malloc_mtx); 137791dd776cSJohn Birrell 137891dd776cSJohn Birrell if (count < kmemcount) { 137991dd776cSJohn Birrell free(bufmtp, M_TEMP); 138091dd776cSJohn Birrell goto restart; 138191dd776cSJohn Birrell } 138291dd776cSJohn Birrell 138391dd776cSJohn Birrell for (mtp = kmemstatistics, i = 0; mtp != NULL; mtp = mtp->ks_next, i++) 138491dd776cSJohn Birrell bufmtp[i] = mtp; 138591dd776cSJohn Birrell 138691dd776cSJohn Birrell mtx_unlock(&malloc_mtx); 138791dd776cSJohn Birrell 138891dd776cSJohn Birrell for (i = 0; i < count; i++) 138991dd776cSJohn Birrell (func)(bufmtp[i], arg); 139091dd776cSJohn Birrell 139191dd776cSJohn Birrell free(bufmtp, M_TEMP); 139291dd776cSJohn Birrell } 139391dd776cSJohn Birrell 1394909ed16cSRobert Watson #ifdef DDB 139546d70077SConrad Meyer static int64_t 139646d70077SConrad Meyer get_malloc_stats(const struct malloc_type_internal *mtip, uint64_t *allocs, 139746d70077SConrad Meyer uint64_t *inuse) 1398909ed16cSRobert Watson { 139946d70077SConrad Meyer const struct malloc_type_stats *mtsp; 140046d70077SConrad Meyer uint64_t frees, alloced, freed; 1401909ed16cSRobert Watson int i; 1402909ed16cSRobert Watson 140346d70077SConrad Meyer *allocs = 0; 1404909ed16cSRobert Watson frees = 0; 140524076d13SRobert Watson alloced = 0; 140624076d13SRobert Watson freed = 0; 14079afff6b1SMateusz Guzik for (i = 0; i <= mp_maxid; i++) { 14089afff6b1SMateusz Guzik mtsp = zpcpu_get_cpu(mtip->mti_stats, i); 140946d70077SConrad Meyer 141046d70077SConrad Meyer *allocs += mtsp->mts_numallocs; 141126e9d9b0SMark Johnston frees += mtsp->mts_numfrees; 141226e9d9b0SMark Johnston alloced += mtsp->mts_memalloced; 141326e9d9b0SMark Johnston freed += mtsp->mts_memfreed; 1414909ed16cSRobert Watson } 141546d70077SConrad Meyer *inuse = *allocs - frees; 141646d70077SConrad Meyer return (alloced - freed); 141746d70077SConrad Meyer } 141846d70077SConrad Meyer 141946d70077SConrad Meyer DB_SHOW_COMMAND(malloc, db_show_malloc) 142046d70077SConrad Meyer { 142146d70077SConrad Meyer const char *fmt_hdr, *fmt_entry; 142246d70077SConrad Meyer struct malloc_type *mtp; 142346d70077SConrad Meyer uint64_t allocs, inuse; 142446d70077SConrad Meyer int64_t size; 142546d70077SConrad Meyer /* variables for sorting */ 142646d70077SConrad Meyer struct malloc_type *last_mtype, *cur_mtype; 142746d70077SConrad Meyer int64_t cur_size, last_size; 142846d70077SConrad Meyer int ties; 142946d70077SConrad Meyer 143046d70077SConrad Meyer if (modif[0] == 'i') { 143146d70077SConrad Meyer fmt_hdr = "%s,%s,%s,%s\n"; 143246d70077SConrad Meyer fmt_entry = "\"%s\",%ju,%jdK,%ju\n"; 143346d70077SConrad Meyer } else { 143446d70077SConrad Meyer fmt_hdr = "%18s %12s %12s %12s\n"; 143546d70077SConrad Meyer fmt_entry = "%18s %12ju %12jdK %12ju\n"; 143646d70077SConrad Meyer } 143746d70077SConrad Meyer 143846d70077SConrad Meyer db_printf(fmt_hdr, "Type", "InUse", "MemUse", "Requests"); 143946d70077SConrad Meyer 144046d70077SConrad Meyer /* Select sort, largest size first. */ 144146d70077SConrad Meyer last_mtype = NULL; 144246d70077SConrad Meyer last_size = INT64_MAX; 144346d70077SConrad Meyer for (;;) { 144446d70077SConrad Meyer cur_mtype = NULL; 144546d70077SConrad Meyer cur_size = -1; 144646d70077SConrad Meyer ties = 0; 144746d70077SConrad Meyer 144846d70077SConrad Meyer for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) { 144946d70077SConrad Meyer /* 145046d70077SConrad Meyer * In the case of size ties, print out mtypes 145146d70077SConrad Meyer * in the order they are encountered. That is, 145246d70077SConrad Meyer * when we encounter the most recently output 145346d70077SConrad Meyer * mtype, we have already printed all preceding 145446d70077SConrad Meyer * ties, and we must print all following ties. 145546d70077SConrad Meyer */ 145646d70077SConrad Meyer if (mtp == last_mtype) { 145746d70077SConrad Meyer ties = 1; 145846d70077SConrad Meyer continue; 145946d70077SConrad Meyer } 1460bdcc2226SMateusz Guzik size = get_malloc_stats(&mtp->ks_mti, &allocs, 146146d70077SConrad Meyer &inuse); 146246d70077SConrad Meyer if (size > cur_size && size < last_size + ties) { 146346d70077SConrad Meyer cur_size = size; 146446d70077SConrad Meyer cur_mtype = mtp; 146546d70077SConrad Meyer } 146646d70077SConrad Meyer } 146746d70077SConrad Meyer if (cur_mtype == NULL) 146846d70077SConrad Meyer break; 146946d70077SConrad Meyer 1470bdcc2226SMateusz Guzik size = get_malloc_stats(&cur_mtype->ks_mti, &allocs, &inuse); 147146d70077SConrad Meyer db_printf(fmt_entry, cur_mtype->ks_shortdesc, inuse, 147246d70077SConrad Meyer howmany(size, 1024), allocs); 147346d70077SConrad Meyer 1474687c94aaSJohn Baldwin if (db_pager_quit) 1475687c94aaSJohn Baldwin break; 147646d70077SConrad Meyer 147746d70077SConrad Meyer last_mtype = cur_mtype; 147846d70077SConrad Meyer last_size = cur_size; 1479909ed16cSRobert Watson } 1480909ed16cSRobert Watson } 1481d7854da1SMatthew D Fleming 1482d7854da1SMatthew D Fleming #if MALLOC_DEBUG_MAXZONES > 1 1483d7854da1SMatthew D Fleming DB_SHOW_COMMAND(multizone_matches, db_show_multizone_matches) 1484d7854da1SMatthew D Fleming { 1485d7854da1SMatthew D Fleming struct malloc_type_internal *mtip; 1486d7854da1SMatthew D Fleming struct malloc_type *mtp; 1487d7854da1SMatthew D Fleming u_int subzone; 1488d7854da1SMatthew D Fleming 1489d7854da1SMatthew D Fleming if (!have_addr) { 1490d7854da1SMatthew D Fleming db_printf("Usage: show multizone_matches <malloc type/addr>\n"); 1491d7854da1SMatthew D Fleming return; 1492d7854da1SMatthew D Fleming } 1493d7854da1SMatthew D Fleming mtp = (void *)addr; 1494bdcc2226SMateusz Guzik if (mtp->ks_version != M_VERSION) { 1495bdcc2226SMateusz Guzik db_printf("Version %lx does not match expected %x\n", 1496bdcc2226SMateusz Guzik mtp->ks_version, M_VERSION); 1497d7854da1SMatthew D Fleming return; 1498d7854da1SMatthew D Fleming } 1499d7854da1SMatthew D Fleming 1500bdcc2226SMateusz Guzik mtip = &mtp->ks_mti; 1501d7854da1SMatthew D Fleming subzone = mtip->mti_zone; 1502d7854da1SMatthew D Fleming 1503d7854da1SMatthew D Fleming for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) { 1504bdcc2226SMateusz Guzik mtip = &mtp->ks_mti; 1505d7854da1SMatthew D Fleming if (mtip->mti_zone != subzone) 1506d7854da1SMatthew D Fleming continue; 1507d7854da1SMatthew D Fleming db_printf("%s\n", mtp->ks_shortdesc); 1508687c94aaSJohn Baldwin if (db_pager_quit) 1509687c94aaSJohn Baldwin break; 1510d7854da1SMatthew D Fleming } 1511d7854da1SMatthew D Fleming } 1512d7854da1SMatthew D Fleming #endif /* MALLOC_DEBUG_MAXZONES > 1 */ 1513d7854da1SMatthew D Fleming #endif /* DDB */ 1514909ed16cSRobert Watson 15155e914b96SJeff Roberson #ifdef MALLOC_PROFILE 15165e914b96SJeff Roberson 15175e914b96SJeff Roberson static int 15185e914b96SJeff Roberson sysctl_kern_mprof(SYSCTL_HANDLER_ARGS) 15195e914b96SJeff Roberson { 152063a7e0a3SRobert Watson struct sbuf sbuf; 15215e914b96SJeff Roberson uint64_t count; 15225e914b96SJeff Roberson uint64_t waste; 15235e914b96SJeff Roberson uint64_t mem; 15245e914b96SJeff Roberson int error; 15255e914b96SJeff Roberson int rsize; 15265e914b96SJeff Roberson int size; 15275e914b96SJeff Roberson int i; 15285e914b96SJeff Roberson 15295e914b96SJeff Roberson waste = 0; 15305e914b96SJeff Roberson mem = 0; 15315e914b96SJeff Roberson 153200f0e671SMatthew D Fleming error = sysctl_wire_old_buffer(req, 0); 153300f0e671SMatthew D Fleming if (error != 0) 153400f0e671SMatthew D Fleming return (error); 15354e657159SMatthew D Fleming sbuf_new_for_sysctl(&sbuf, NULL, 128, req); 153663a7e0a3SRobert Watson sbuf_printf(&sbuf, 15375e914b96SJeff Roberson "\n Size Requests Real Size\n"); 15385e914b96SJeff Roberson for (i = 0; i < KMEM_ZSIZE; i++) { 15395e914b96SJeff Roberson size = i << KMEM_ZSHIFT; 15405e914b96SJeff Roberson rsize = kmemzones[kmemsize[i]].kz_size; 15415e914b96SJeff Roberson count = (long long unsigned)krequests[i]; 15425e914b96SJeff Roberson 154363a7e0a3SRobert Watson sbuf_printf(&sbuf, "%6d%28llu%11d\n", size, 154463a7e0a3SRobert Watson (unsigned long long)count, rsize); 15455e914b96SJeff Roberson 15465e914b96SJeff Roberson if ((rsize * count) > (size * count)) 15475e914b96SJeff Roberson waste += (rsize * count) - (size * count); 15485e914b96SJeff Roberson mem += (rsize * count); 15495e914b96SJeff Roberson } 155063a7e0a3SRobert Watson sbuf_printf(&sbuf, 15515e914b96SJeff Roberson "\nTotal memory used:\t%30llu\nTotal Memory wasted:\t%30llu\n", 15525e914b96SJeff Roberson (unsigned long long)mem, (unsigned long long)waste); 15534e657159SMatthew D Fleming error = sbuf_finish(&sbuf); 155463a7e0a3SRobert Watson sbuf_delete(&sbuf); 15555e914b96SJeff Roberson return (error); 15565e914b96SJeff Roberson } 15575e914b96SJeff Roberson 15587029da5cSPawel Biernacki SYSCTL_OID(_kern, OID_AUTO, mprof, 15597029da5cSPawel Biernacki CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT, NULL, 0, 15607029da5cSPawel Biernacki sysctl_kern_mprof, "A", 15617029da5cSPawel Biernacki "Malloc Profiling"); 15625e914b96SJeff Roberson #endif /* MALLOC_PROFILE */ 1563