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 1770ce3f16dSRobert Watson /* 178bdcc2226SMateusz Guzik * Zone to allocate per-CPU storage for statistics. 1790ce3f16dSRobert Watson */ 1809afff6b1SMateusz Guzik static uma_zone_t mt_stats_zone; 18163a7e0a3SRobert Watson 182b89eaf4eSAlan Cox u_long vm_kmem_size; 183d801e824SAndriy Gapon SYSCTL_ULONG(_vm, OID_AUTO, kmem_size, CTLFLAG_RDTUN, &vm_kmem_size, 0, 18484344f9fSDag-Erling Smørgrav "Size of kernel memory"); 1855a34a9f0SJeff Roberson 1867001d850SXin LI static u_long kmem_zmax = KMEM_ZMAX; 1877001d850SXin LI SYSCTL_ULONG(_vm, OID_AUTO, kmem_zmax, CTLFLAG_RDTUN, &kmem_zmax, 0, 1887001d850SXin LI "Maximum allocation size that malloc(9) would use UMA as backend"); 1897001d850SXin LI 190b89eaf4eSAlan Cox static u_long vm_kmem_size_min; 191d801e824SAndriy Gapon SYSCTL_ULONG(_vm, OID_AUTO, kmem_size_min, CTLFLAG_RDTUN, &vm_kmem_size_min, 0, 1920e5179e4SStephane E. Potvin "Minimum size of kernel memory"); 1930e5179e4SStephane E. Potvin 194b89eaf4eSAlan Cox static u_long vm_kmem_size_max; 195d801e824SAndriy Gapon SYSCTL_ULONG(_vm, OID_AUTO, kmem_size_max, CTLFLAG_RDTUN, &vm_kmem_size_max, 0, 196479439b4SDag-Erling Smørgrav "Maximum size of kernel memory"); 197479439b4SDag-Erling Smørgrav 1984813ad54SHans Petter Selasky static u_int vm_kmem_size_scale; 199d801e824SAndriy Gapon SYSCTL_UINT(_vm, OID_AUTO, kmem_size_scale, CTLFLAG_RDTUN, &vm_kmem_size_scale, 0, 200479439b4SDag-Erling Smørgrav "Scale factor for kernel memory size"); 201479439b4SDag-Erling Smørgrav 2027814c80aSAndriy Gapon static int sysctl_kmem_map_size(SYSCTL_HANDLER_ARGS); 2037814c80aSAndriy Gapon SYSCTL_PROC(_vm, OID_AUTO, kmem_map_size, 2047814c80aSAndriy Gapon CTLFLAG_RD | CTLTYPE_ULONG | CTLFLAG_MPSAFE, NULL, 0, 2055df87b21SJeff Roberson sysctl_kmem_map_size, "LU", "Current kmem allocation size"); 2067814c80aSAndriy Gapon 20795bb9d38SAndriy Gapon static int sysctl_kmem_map_free(SYSCTL_HANDLER_ARGS); 20895bb9d38SAndriy Gapon SYSCTL_PROC(_vm, OID_AUTO, kmem_map_free, 20995bb9d38SAndriy Gapon CTLFLAG_RD | CTLTYPE_ULONG | CTLFLAG_MPSAFE, NULL, 0, 2105df87b21SJeff Roberson sysctl_kmem_map_free, "LU", "Free space in kmem"); 21195bb9d38SAndriy Gapon 212828afddaSMateusz Guzik static SYSCTL_NODE(_vm, OID_AUTO, malloc, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 213828afddaSMateusz Guzik "Malloc information"); 214828afddaSMateusz Guzik 215828afddaSMateusz Guzik static u_int vm_malloc_zone_count = nitems(kmemzones); 216828afddaSMateusz Guzik SYSCTL_UINT(_vm_malloc, OID_AUTO, zone_count, 217828afddaSMateusz Guzik CTLFLAG_RD, &vm_malloc_zone_count, 0, 218828afddaSMateusz Guzik "Number of malloc zones"); 219828afddaSMateusz Guzik 220828afddaSMateusz Guzik static int sysctl_vm_malloc_zone_sizes(SYSCTL_HANDLER_ARGS); 221828afddaSMateusz Guzik SYSCTL_PROC(_vm_malloc, OID_AUTO, zone_sizes, 222828afddaSMateusz Guzik CTLFLAG_RD | CTLTYPE_OPAQUE | CTLFLAG_MPSAFE, NULL, 0, 223828afddaSMateusz Guzik sysctl_vm_malloc_zone_sizes, "S", "Zone sizes used by malloc"); 224828afddaSMateusz Guzik 2255a34a9f0SJeff Roberson /* 22699571dc3SJeff Roberson * The malloc_mtx protects the kmemstatistics linked list. 2275a34a9f0SJeff Roberson */ 2285a34a9f0SJeff Roberson struct mtx malloc_mtx; 22969ef67f9SJason Evans 2305e914b96SJeff Roberson #ifdef MALLOC_PROFILE 2315e914b96SJeff Roberson uint64_t krequests[KMEM_ZSIZE + 1]; 2326f267175SJeff Roberson 2335e914b96SJeff Roberson static int sysctl_kern_mprof(SYSCTL_HANDLER_ARGS); 2345e914b96SJeff Roberson #endif 2355e914b96SJeff Roberson 236cd814b26SRobert Watson static int sysctl_kern_malloc_stats(SYSCTL_HANDLER_ARGS); 237df8bae1dSRodney W. Grimes 2380ce3f16dSRobert Watson /* 2390ce3f16dSRobert Watson * time_uptime of the last malloc(9) failure (induced or real). 2400ce3f16dSRobert Watson */ 2411fb14a47SPoul-Henning Kamp static time_t t_malloc_fail; 2421fb14a47SPoul-Henning Kamp 243d7854da1SMatthew D Fleming #if defined(MALLOC_MAKE_FAILURES) || (MALLOC_DEBUG_MAXZONES > 1) 2447029da5cSPawel Biernacki static SYSCTL_NODE(_debug, OID_AUTO, malloc, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 245d7854da1SMatthew D Fleming "Kernel malloc debugging options"); 246d7854da1SMatthew D Fleming #endif 247d7854da1SMatthew D Fleming 248eae870cdSRobert Watson /* 2490ce3f16dSRobert Watson * malloc(9) fault injection -- cause malloc failures every (n) mallocs when 2500ce3f16dSRobert Watson * the caller specifies M_NOWAIT. If set to 0, no failures are caused. 251eae870cdSRobert Watson */ 2520ce3f16dSRobert Watson #ifdef MALLOC_MAKE_FAILURES 253eae870cdSRobert Watson static int malloc_failure_rate; 254eae870cdSRobert Watson static int malloc_nowait_count; 255eae870cdSRobert Watson static int malloc_failure_count; 256af3b2549SHans Petter Selasky SYSCTL_INT(_debug_malloc, OID_AUTO, failure_rate, CTLFLAG_RWTUN, 257eae870cdSRobert Watson &malloc_failure_rate, 0, "Every (n) mallocs with M_NOWAIT will fail"); 258eae870cdSRobert Watson SYSCTL_INT(_debug_malloc, OID_AUTO, failure_count, CTLFLAG_RD, 259eae870cdSRobert Watson &malloc_failure_count, 0, "Number of imposed M_NOWAIT malloc failures"); 260eae870cdSRobert Watson #endif 261eae870cdSRobert Watson 2627814c80aSAndriy Gapon static int 2637814c80aSAndriy Gapon sysctl_kmem_map_size(SYSCTL_HANDLER_ARGS) 2647814c80aSAndriy Gapon { 2657814c80aSAndriy Gapon u_long size; 2667814c80aSAndriy Gapon 2672e47807cSJeff Roberson size = uma_size(); 2687814c80aSAndriy Gapon return (sysctl_handle_long(oidp, &size, 0, req)); 2697814c80aSAndriy Gapon } 2707814c80aSAndriy Gapon 27195bb9d38SAndriy Gapon static int 27295bb9d38SAndriy Gapon sysctl_kmem_map_free(SYSCTL_HANDLER_ARGS) 27395bb9d38SAndriy Gapon { 2742e47807cSJeff Roberson u_long size, limit; 27595bb9d38SAndriy Gapon 2762e47807cSJeff Roberson /* The sysctl is unsigned, implement as a saturation value. */ 2772e47807cSJeff Roberson size = uma_size(); 2782e47807cSJeff Roberson limit = uma_limit(); 2792e47807cSJeff Roberson if (size > limit) 2802e47807cSJeff Roberson size = 0; 2812e47807cSJeff Roberson else 2822e47807cSJeff Roberson size = limit - size; 28395bb9d38SAndriy Gapon return (sysctl_handle_long(oidp, &size, 0, req)); 28495bb9d38SAndriy Gapon } 28595bb9d38SAndriy Gapon 286828afddaSMateusz Guzik static int 287828afddaSMateusz Guzik sysctl_vm_malloc_zone_sizes(SYSCTL_HANDLER_ARGS) 288828afddaSMateusz Guzik { 289828afddaSMateusz Guzik int sizes[nitems(kmemzones)]; 290828afddaSMateusz Guzik int i; 291828afddaSMateusz Guzik 292828afddaSMateusz Guzik for (i = 0; i < nitems(kmemzones); i++) { 293828afddaSMateusz Guzik sizes[i] = kmemzones[i].kz_size; 294828afddaSMateusz Guzik } 295828afddaSMateusz Guzik 296828afddaSMateusz Guzik return (SYSCTL_OUT(req, &sizes, sizeof(sizes))); 297828afddaSMateusz Guzik } 298828afddaSMateusz Guzik 299d7854da1SMatthew D Fleming /* 300d7854da1SMatthew D Fleming * malloc(9) uma zone separation -- sub-page buffer overruns in one 301d7854da1SMatthew D Fleming * malloc type will affect only a subset of other malloc types. 302d7854da1SMatthew D Fleming */ 303d7854da1SMatthew D Fleming #if MALLOC_DEBUG_MAXZONES > 1 304d7854da1SMatthew D Fleming static void 305d7854da1SMatthew D Fleming tunable_set_numzones(void) 306d7854da1SMatthew D Fleming { 307d7854da1SMatthew D Fleming 308d7854da1SMatthew D Fleming TUNABLE_INT_FETCH("debug.malloc.numzones", 309d7854da1SMatthew D Fleming &numzones); 310d7854da1SMatthew D Fleming 311d7854da1SMatthew D Fleming /* Sanity check the number of malloc uma zones. */ 312d7854da1SMatthew D Fleming if (numzones <= 0) 313d7854da1SMatthew D Fleming numzones = 1; 314d7854da1SMatthew D Fleming if (numzones > MALLOC_DEBUG_MAXZONES) 315d7854da1SMatthew D Fleming numzones = MALLOC_DEBUG_MAXZONES; 316d7854da1SMatthew D Fleming } 317d7854da1SMatthew D Fleming SYSINIT(numzones, SI_SUB_TUNABLES, SI_ORDER_ANY, tunable_set_numzones, NULL); 318af3b2549SHans Petter Selasky SYSCTL_INT(_debug_malloc, OID_AUTO, numzones, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, 319d7854da1SMatthew D Fleming &numzones, 0, "Number of malloc uma subzones"); 320d7854da1SMatthew D Fleming 321d7854da1SMatthew D Fleming /* 322d7854da1SMatthew D Fleming * Any number that changes regularly is an okay choice for the 323d7854da1SMatthew D Fleming * offset. Build numbers are pretty good of you have them. 324d7854da1SMatthew D Fleming */ 325d7854da1SMatthew D Fleming static u_int zone_offset = __FreeBSD_version; 326d7854da1SMatthew D Fleming TUNABLE_INT("debug.malloc.zone_offset", &zone_offset); 327d7854da1SMatthew D Fleming SYSCTL_UINT(_debug_malloc, OID_AUTO, zone_offset, CTLFLAG_RDTUN, 328d7854da1SMatthew D Fleming &zone_offset, 0, "Separate malloc types by examining the " 329d7854da1SMatthew D Fleming "Nth character in the malloc type short description."); 330d7854da1SMatthew D Fleming 331c9e05ccdSMateusz Guzik static void 332c9e05ccdSMateusz Guzik mtp_set_subzone(struct malloc_type *mtp) 333d7854da1SMatthew D Fleming { 334c9e05ccdSMateusz Guzik struct malloc_type_internal *mtip; 335c9e05ccdSMateusz Guzik const char *desc; 336d7854da1SMatthew D Fleming size_t len; 337d7854da1SMatthew D Fleming u_int val; 338d7854da1SMatthew D Fleming 339bdcc2226SMateusz Guzik mtip = &mtp->ks_mti; 340c9e05ccdSMateusz Guzik desc = mtp->ks_shortdesc; 341d7854da1SMatthew D Fleming if (desc == NULL || (len = strlen(desc)) == 0) 342c9e05ccdSMateusz Guzik val = 0; 343c9e05ccdSMateusz Guzik else 344d7854da1SMatthew D Fleming val = desc[zone_offset % len]; 345c9e05ccdSMateusz Guzik mtip->mti_zone = (val % numzones); 346c9e05ccdSMateusz Guzik } 347c9e05ccdSMateusz Guzik 348c9e05ccdSMateusz Guzik static inline u_int 349c9e05ccdSMateusz Guzik mtp_get_subzone(struct malloc_type *mtp) 350c9e05ccdSMateusz Guzik { 351c9e05ccdSMateusz Guzik struct malloc_type_internal *mtip; 352c9e05ccdSMateusz Guzik 353bdcc2226SMateusz Guzik mtip = &mtp->ks_mti; 354c9e05ccdSMateusz Guzik 355c9e05ccdSMateusz Guzik KASSERT(mtip->mti_zone < numzones, 356c9e05ccdSMateusz Guzik ("mti_zone %u out of range %d", 357c9e05ccdSMateusz Guzik mtip->mti_zone, numzones)); 358c9e05ccdSMateusz Guzik return (mtip->mti_zone); 359d7854da1SMatthew D Fleming } 360d7854da1SMatthew D Fleming #elif MALLOC_DEBUG_MAXZONES == 0 361d7854da1SMatthew D Fleming #error "MALLOC_DEBUG_MAXZONES must be positive." 362d7854da1SMatthew D Fleming #else 363c9e05ccdSMateusz Guzik static void 364c9e05ccdSMateusz Guzik mtp_set_subzone(struct malloc_type *mtp) 365c9e05ccdSMateusz Guzik { 366c9e05ccdSMateusz Guzik struct malloc_type_internal *mtip; 367c9e05ccdSMateusz Guzik 368bdcc2226SMateusz Guzik mtip = &mtp->ks_mti; 369c9e05ccdSMateusz Guzik mtip->mti_zone = 0; 370c9e05ccdSMateusz Guzik } 371c9e05ccdSMateusz Guzik 372d7854da1SMatthew D Fleming static inline u_int 373c9e05ccdSMateusz Guzik mtp_get_subzone(struct malloc_type *mtp) 374d7854da1SMatthew D Fleming { 375d7854da1SMatthew D Fleming 376d7854da1SMatthew D Fleming return (0); 377d7854da1SMatthew D Fleming } 378d7854da1SMatthew D Fleming #endif /* MALLOC_DEBUG_MAXZONES > 1 */ 379d7854da1SMatthew D Fleming 3801fb14a47SPoul-Henning Kamp int 3811fb14a47SPoul-Henning Kamp malloc_last_fail(void) 3821fb14a47SPoul-Henning Kamp { 3831fb14a47SPoul-Henning Kamp 3841fb14a47SPoul-Henning Kamp return (time_uptime - t_malloc_fail); 3851fb14a47SPoul-Henning Kamp } 3861fb14a47SPoul-Henning Kamp 387df8bae1dSRodney W. Grimes /* 3880ce3f16dSRobert Watson * An allocation has succeeded -- update malloc type statistics for the 3890ce3f16dSRobert Watson * amount of bucket size. Occurs within a critical section so that the 3900ce3f16dSRobert Watson * thread isn't preempted and doesn't migrate while updating per-PCU 3910ce3f16dSRobert Watson * statistics. 3924362fadaSBrian Feldman */ 3934362fadaSBrian Feldman static void 39463a7e0a3SRobert Watson malloc_type_zone_allocated(struct malloc_type *mtp, unsigned long size, 3954362fadaSBrian Feldman int zindx) 3964362fadaSBrian Feldman { 39763a7e0a3SRobert Watson struct malloc_type_internal *mtip; 39863a7e0a3SRobert Watson struct malloc_type_stats *mtsp; 39963a7e0a3SRobert Watson 40063a7e0a3SRobert Watson critical_enter(); 401bdcc2226SMateusz Guzik mtip = &mtp->ks_mti; 4029afff6b1SMateusz Guzik mtsp = zpcpu_get(mtip->mti_stats); 40373864adbSPawel Jakub Dawidek if (size > 0) { 40463a7e0a3SRobert Watson mtsp->mts_memalloced += size; 40563a7e0a3SRobert Watson mtsp->mts_numallocs++; 40673864adbSPawel Jakub Dawidek } 4074362fadaSBrian Feldman if (zindx != -1) 40863a7e0a3SRobert Watson mtsp->mts_size |= 1 << zindx; 40991dd776cSJohn Birrell 41091dd776cSJohn Birrell #ifdef KDTRACE_HOOKS 4117cd79421SMateusz Guzik if (__predict_false(dtrace_malloc_enabled)) { 41291dd776cSJohn Birrell uint32_t probe_id = mtip->mti_probes[DTMALLOC_PROBE_MALLOC]; 41391dd776cSJohn Birrell if (probe_id != 0) 41491dd776cSJohn Birrell (dtrace_malloc_probe)(probe_id, 41591dd776cSJohn Birrell (uintptr_t) mtp, (uintptr_t) mtip, 41691dd776cSJohn Birrell (uintptr_t) mtsp, size, zindx); 41791dd776cSJohn Birrell } 41891dd776cSJohn Birrell #endif 41991dd776cSJohn Birrell 42063a7e0a3SRobert Watson critical_exit(); 4214362fadaSBrian Feldman } 4224362fadaSBrian Feldman 4234362fadaSBrian Feldman void 42463a7e0a3SRobert Watson malloc_type_allocated(struct malloc_type *mtp, unsigned long size) 4254362fadaSBrian Feldman { 42663a7e0a3SRobert Watson 42773864adbSPawel Jakub Dawidek if (size > 0) 42863a7e0a3SRobert Watson malloc_type_zone_allocated(mtp, size, -1); 4294362fadaSBrian Feldman } 4304362fadaSBrian Feldman 4314362fadaSBrian Feldman /* 4323805385eSRobert Watson * A free operation has occurred -- update malloc type statistics for the 4330ce3f16dSRobert Watson * amount of the bucket size. Occurs within a critical section so that the 4340ce3f16dSRobert Watson * thread isn't preempted and doesn't migrate while updating per-CPU 4350ce3f16dSRobert Watson * statistics. 4364362fadaSBrian Feldman */ 4374362fadaSBrian Feldman void 43863a7e0a3SRobert Watson malloc_type_freed(struct malloc_type *mtp, unsigned long size) 4394362fadaSBrian Feldman { 44063a7e0a3SRobert Watson struct malloc_type_internal *mtip; 44163a7e0a3SRobert Watson struct malloc_type_stats *mtsp; 44263a7e0a3SRobert Watson 44363a7e0a3SRobert Watson critical_enter(); 444bdcc2226SMateusz Guzik mtip = &mtp->ks_mti; 4459afff6b1SMateusz Guzik mtsp = zpcpu_get(mtip->mti_stats); 44663a7e0a3SRobert Watson mtsp->mts_memfreed += size; 44763a7e0a3SRobert Watson mtsp->mts_numfrees++; 44891dd776cSJohn Birrell 44991dd776cSJohn Birrell #ifdef KDTRACE_HOOKS 4507cd79421SMateusz Guzik if (__predict_false(dtrace_malloc_enabled)) { 45191dd776cSJohn Birrell uint32_t probe_id = mtip->mti_probes[DTMALLOC_PROBE_FREE]; 45291dd776cSJohn Birrell if (probe_id != 0) 45391dd776cSJohn Birrell (dtrace_malloc_probe)(probe_id, 45491dd776cSJohn Birrell (uintptr_t) mtp, (uintptr_t) mtip, 45591dd776cSJohn Birrell (uintptr_t) mtsp, size, 0); 45691dd776cSJohn Birrell } 45791dd776cSJohn Birrell #endif 45891dd776cSJohn Birrell 45963a7e0a3SRobert Watson critical_exit(); 4604362fadaSBrian Feldman } 4614362fadaSBrian Feldman 4624362fadaSBrian Feldman /* 463f346986bSAlan Cox * contigmalloc: 464f346986bSAlan Cox * 465f346986bSAlan Cox * Allocate a block of physically contiguous memory. 466f346986bSAlan Cox * 467f346986bSAlan Cox * If M_NOWAIT is set, this routine will not block and return NULL if 468f346986bSAlan Cox * the allocation fails. 469f346986bSAlan Cox */ 470f346986bSAlan Cox void * 471f346986bSAlan Cox contigmalloc(unsigned long size, struct malloc_type *type, int flags, 472f346986bSAlan Cox vm_paddr_t low, vm_paddr_t high, unsigned long alignment, 473831ce4cbSJohn Baldwin vm_paddr_t boundary) 474f346986bSAlan Cox { 475f346986bSAlan Cox void *ret; 476f346986bSAlan Cox 47744d0efb2SAlan Cox ret = (void *)kmem_alloc_contig(size, flags, low, high, alignment, 47844d0efb2SAlan Cox boundary, VM_MEMATTR_DEFAULT); 479f346986bSAlan Cox if (ret != NULL) 480f346986bSAlan Cox malloc_type_allocated(type, round_page(size)); 481f346986bSAlan Cox return (ret); 482f346986bSAlan Cox } 483f346986bSAlan Cox 484ab3185d1SJeff Roberson void * 4859978bd99SMark Johnston contigmalloc_domainset(unsigned long size, struct malloc_type *type, 4869978bd99SMark Johnston struct domainset *ds, int flags, vm_paddr_t low, vm_paddr_t high, 487ab3185d1SJeff Roberson unsigned long alignment, vm_paddr_t boundary) 488ab3185d1SJeff Roberson { 489ab3185d1SJeff Roberson void *ret; 490ab3185d1SJeff Roberson 4919978bd99SMark Johnston ret = (void *)kmem_alloc_contig_domainset(ds, size, flags, low, high, 492ab3185d1SJeff Roberson alignment, boundary, VM_MEMATTR_DEFAULT); 493ab3185d1SJeff Roberson if (ret != NULL) 494ab3185d1SJeff Roberson malloc_type_allocated(type, round_page(size)); 495ab3185d1SJeff Roberson return (ret); 496ab3185d1SJeff Roberson } 497ab3185d1SJeff Roberson 498f346986bSAlan Cox /* 499f346986bSAlan Cox * contigfree: 500f346986bSAlan Cox * 501f346986bSAlan Cox * Free a block of memory allocated by contigmalloc. 502f346986bSAlan Cox * 503f346986bSAlan Cox * This routine may not block. 504f346986bSAlan Cox */ 505f346986bSAlan Cox void 506f346986bSAlan Cox contigfree(void *addr, unsigned long size, struct malloc_type *type) 507f346986bSAlan Cox { 508f346986bSAlan Cox 50949bfa624SAlan Cox kmem_free((vm_offset_t)addr, size); 510f346986bSAlan Cox malloc_type_freed(type, round_page(size)); 511f346986bSAlan Cox } 512f346986bSAlan Cox 513ab3185d1SJeff Roberson #ifdef MALLOC_DEBUG 514ab3185d1SJeff Roberson static int 5155a70796aSLi-Wen Hsu malloc_dbg(caddr_t *vap, size_t *sizep, struct malloc_type *mtp, 516ab3185d1SJeff Roberson int flags) 517df8bae1dSRodney W. Grimes { 518194a0abfSPoul-Henning Kamp #ifdef INVARIANTS 519ab3185d1SJeff Roberson int indx; 520ab3185d1SJeff Roberson 521bdcc2226SMateusz Guzik KASSERT(mtp->ks_version == M_VERSION, ("malloc: bad malloc type version")); 522d3c11994SPoul-Henning Kamp /* 52323198357SRuslan Ermilov * Check that exactly one of M_WAITOK or M_NOWAIT is specified. 524d3c11994SPoul-Henning Kamp */ 52523198357SRuslan Ermilov indx = flags & (M_WAITOK | M_NOWAIT); 526d3c11994SPoul-Henning Kamp if (indx != M_NOWAIT && indx != M_WAITOK) { 527d3c11994SPoul-Henning Kamp static struct timeval lasterr; 528d3c11994SPoul-Henning Kamp static int curerr, once; 529d3c11994SPoul-Henning Kamp if (once == 0 && ppsratecheck(&lasterr, &curerr, 1)) { 530d3c11994SPoul-Henning Kamp printf("Bad malloc flags: %x\n", indx); 5312d50560aSMarcel Moolenaar kdb_backtrace(); 532d3c11994SPoul-Henning Kamp flags |= M_WAITOK; 533d3c11994SPoul-Henning Kamp once++; 534d3c11994SPoul-Henning Kamp } 535d3c11994SPoul-Henning Kamp } 536194a0abfSPoul-Henning Kamp #endif 537eae870cdSRobert Watson #ifdef MALLOC_MAKE_FAILURES 538eae870cdSRobert Watson if ((flags & M_NOWAIT) && (malloc_failure_rate != 0)) { 539eae870cdSRobert Watson atomic_add_int(&malloc_nowait_count, 1); 540eae870cdSRobert Watson if ((malloc_nowait_count % malloc_failure_rate) == 0) { 541eae870cdSRobert Watson atomic_add_int(&malloc_failure_count, 1); 5423f6ee876SPoul-Henning Kamp t_malloc_fail = time_uptime; 543ab3185d1SJeff Roberson *vap = NULL; 544ab3185d1SJeff Roberson return (EJUSTRETURN); 545eae870cdSRobert Watson } 546eae870cdSRobert Watson } 547eae870cdSRobert Watson #endif 54806bf2a6aSMatt Macy if (flags & M_WAITOK) { 549b40ce416SJulian Elischer KASSERT(curthread->td_intr_nesting_level == 0, 550a163d034SWarner Losh ("malloc(M_WAITOK) in interrupt context")); 5515757b59fSGleb Smirnoff if (__predict_false(!THREAD_CAN_SLEEP())) { 552bac06038SGleb Smirnoff #ifdef EPOCH_TRACE 553bac06038SGleb Smirnoff epoch_trace_list(curthread); 554bac06038SGleb Smirnoff #endif 5555757b59fSGleb Smirnoff KASSERT(1, 5565757b59fSGleb Smirnoff ("malloc(M_WAITOK) with sleeping prohibited")); 5575757b59fSGleb Smirnoff } 55806bf2a6aSMatt Macy } 559d9e2e68dSMark Johnston KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 5601067a2baSJonathan T. Looney ("malloc: called with spinlock or critical section held")); 5611067a2baSJonathan T. Looney 562e4eb384bSBosko Milekic #ifdef DEBUG_MEMGUARD 563ab3185d1SJeff Roberson if (memguard_cmp_mtp(mtp, *sizep)) { 564ab3185d1SJeff Roberson *vap = memguard_alloc(*sizep, flags); 565ab3185d1SJeff Roberson if (*vap != NULL) 566ab3185d1SJeff Roberson return (EJUSTRETURN); 567e3813573SMatthew D Fleming /* This is unfortunate but should not be fatal. */ 568e3813573SMatthew D Fleming } 569e4eb384bSBosko Milekic #endif 570e4eb384bSBosko Milekic 571847a2a17SPawel Jakub Dawidek #ifdef DEBUG_REDZONE 572ab3185d1SJeff Roberson *sizep = redzone_size_ntor(*sizep); 573ab3185d1SJeff Roberson #endif 574ab3185d1SJeff Roberson 575ab3185d1SJeff Roberson return (0); 576ab3185d1SJeff Roberson } 577ab3185d1SJeff Roberson #endif 578ab3185d1SJeff Roberson 579ab3185d1SJeff Roberson /* 5806d6a03d7SJeff Roberson * Handle large allocations and frees by using kmem_malloc directly. 5816d6a03d7SJeff Roberson */ 5826d6a03d7SJeff Roberson static inline bool 5836d6a03d7SJeff Roberson malloc_large_slab(uma_slab_t slab) 5846d6a03d7SJeff Roberson { 5856d6a03d7SJeff Roberson uintptr_t va; 5866d6a03d7SJeff Roberson 5876d6a03d7SJeff Roberson va = (uintptr_t)slab; 5886d6a03d7SJeff Roberson return ((va & 1) != 0); 5896d6a03d7SJeff Roberson } 5906d6a03d7SJeff Roberson 5916d6a03d7SJeff Roberson static inline size_t 5926d6a03d7SJeff Roberson malloc_large_size(uma_slab_t slab) 5936d6a03d7SJeff Roberson { 5946d6a03d7SJeff Roberson uintptr_t va; 5956d6a03d7SJeff Roberson 5966d6a03d7SJeff Roberson va = (uintptr_t)slab; 5976d6a03d7SJeff Roberson return (va >> 1); 5986d6a03d7SJeff Roberson } 5996d6a03d7SJeff Roberson 6006d6a03d7SJeff Roberson static caddr_t 6016d6a03d7SJeff Roberson malloc_large(size_t *size, struct domainset *policy, int flags) 6026d6a03d7SJeff Roberson { 6036d6a03d7SJeff Roberson vm_offset_t va; 6046d6a03d7SJeff Roberson size_t sz; 6056d6a03d7SJeff Roberson 6066d6a03d7SJeff Roberson sz = roundup(*size, PAGE_SIZE); 6076d6a03d7SJeff Roberson va = kmem_malloc_domainset(policy, sz, flags); 6086d6a03d7SJeff Roberson if (va != 0) { 6096d6a03d7SJeff Roberson /* The low bit is unused for slab pointers. */ 6106d6a03d7SJeff Roberson vsetzoneslab(va, NULL, (void *)((sz << 1) | 1)); 6116d6a03d7SJeff Roberson uma_total_inc(sz); 6126d6a03d7SJeff Roberson *size = sz; 6136d6a03d7SJeff Roberson } 6146d6a03d7SJeff Roberson return ((caddr_t)va); 6156d6a03d7SJeff Roberson } 6166d6a03d7SJeff Roberson 6176d6a03d7SJeff Roberson static void 6186d6a03d7SJeff Roberson free_large(void *addr, size_t size) 6196d6a03d7SJeff Roberson { 6206d6a03d7SJeff Roberson 6216d6a03d7SJeff Roberson kmem_free((vm_offset_t)addr, size); 6226d6a03d7SJeff Roberson uma_total_dec(size); 6236d6a03d7SJeff Roberson } 6246d6a03d7SJeff Roberson 6256d6a03d7SJeff Roberson /* 626ab3185d1SJeff Roberson * malloc: 627ab3185d1SJeff Roberson * 628ab3185d1SJeff Roberson * Allocate a block of memory. 629ab3185d1SJeff Roberson * 630ab3185d1SJeff Roberson * If M_NOWAIT is set, this routine will not block and return NULL if 631ab3185d1SJeff Roberson * the allocation fails. 632ab3185d1SJeff Roberson */ 633ab3185d1SJeff Roberson void * 63434c538c3SMateusz Guzik (malloc)(size_t size, struct malloc_type *mtp, int flags) 635ab3185d1SJeff Roberson { 636ab3185d1SJeff Roberson int indx; 637ab3185d1SJeff Roberson caddr_t va; 638ab3185d1SJeff Roberson uma_zone_t zone; 639ab3185d1SJeff Roberson #if defined(DEBUG_REDZONE) 640ab3185d1SJeff Roberson unsigned long osize = size; 641ab3185d1SJeff Roberson #endif 642ab3185d1SJeff Roberson 64382c174a3SMateusz Guzik MPASS((flags & M_EXEC) == 0); 644ab3185d1SJeff Roberson #ifdef MALLOC_DEBUG 6455072a5f4SMatt Macy va = NULL; 646ab3185d1SJeff Roberson if (malloc_dbg(&va, &size, mtp, flags) != 0) 647ab3185d1SJeff Roberson return (va); 648847a2a17SPawel Jakub Dawidek #endif 649847a2a17SPawel Jakub Dawidek 65082c174a3SMateusz Guzik if (size <= kmem_zmax) { 6516f267175SJeff Roberson if (size & KMEM_ZMASK) 6526f267175SJeff Roberson size = (size & ~KMEM_ZMASK) + KMEM_ZBASE; 6536f267175SJeff Roberson indx = kmemsize[size >> KMEM_ZSHIFT]; 654c9e05ccdSMateusz Guzik zone = kmemzones[indx].kz_zone[mtp_get_subzone(mtp)]; 6556f267175SJeff Roberson #ifdef MALLOC_PROFILE 6566f267175SJeff Roberson krequests[size >> KMEM_ZSHIFT]++; 6576f267175SJeff Roberson #endif 6588355f576SJeff Roberson va = uma_zalloc(zone, flags); 6594362fadaSBrian Feldman if (va != NULL) 660e20a199fSJeff Roberson size = zone->uz_size; 66163a7e0a3SRobert Watson malloc_type_zone_allocated(mtp, va == NULL ? 0 : size, indx); 6628355f576SJeff Roberson } else { 6636d6a03d7SJeff Roberson va = malloc_large(&size, DOMAINSET_RR(), flags); 66463a7e0a3SRobert Watson malloc_type_allocated(mtp, va == NULL ? 0 : size); 665df8bae1dSRodney W. Grimes } 66682c174a3SMateusz Guzik if (__predict_false(va == NULL)) { 66782c174a3SMateusz Guzik KASSERT((flags & M_WAITOK) == 0, 66882c174a3SMateusz Guzik ("malloc(M_WAITOK) returned NULL")); 6691fb14a47SPoul-Henning Kamp t_malloc_fail = time_uptime; 67082c174a3SMateusz Guzik } 671ab3185d1SJeff Roberson #ifdef DEBUG_REDZONE 672ab3185d1SJeff Roberson if (va != NULL) 673ab3185d1SJeff Roberson va = redzone_setup(va, osize); 6744db4f5c8SPoul-Henning Kamp #endif 675ab3185d1SJeff Roberson return ((void *) va); 676ab3185d1SJeff Roberson } 677ab3185d1SJeff Roberson 6789978bd99SMark Johnston static void * 679dc727127SMark Johnston malloc_domain(size_t *sizep, int *indxp, struct malloc_type *mtp, int domain, 6806d6a03d7SJeff Roberson int flags) 681ab3185d1SJeff Roberson { 682ab3185d1SJeff Roberson uma_zone_t zone; 683dc727127SMark Johnston caddr_t va; 684dc727127SMark Johnston size_t size; 685dc727127SMark Johnston int indx; 686ab3185d1SJeff Roberson 687dc727127SMark Johnston size = *sizep; 6886d6a03d7SJeff Roberson KASSERT(size <= kmem_zmax && (flags & M_EXEC) == 0, 6896d6a03d7SJeff Roberson ("malloc_domain: Called with bad flag / size combination.")); 690ab3185d1SJeff Roberson if (size & KMEM_ZMASK) 691ab3185d1SJeff Roberson size = (size & ~KMEM_ZMASK) + KMEM_ZBASE; 692ab3185d1SJeff Roberson indx = kmemsize[size >> KMEM_ZSHIFT]; 693c9e05ccdSMateusz Guzik zone = kmemzones[indx].kz_zone[mtp_get_subzone(mtp)]; 694ab3185d1SJeff Roberson #ifdef MALLOC_PROFILE 695ab3185d1SJeff Roberson krequests[size >> KMEM_ZSHIFT]++; 696ab3185d1SJeff Roberson #endif 697ab3185d1SJeff Roberson va = uma_zalloc_domain(zone, NULL, domain, flags); 698ab3185d1SJeff Roberson if (va != NULL) 699dc727127SMark Johnston *sizep = zone->uz_size; 7006d6a03d7SJeff Roberson *indxp = indx; 701df8bae1dSRodney W. Grimes return ((void *)va); 702df8bae1dSRodney W. Grimes } 703df8bae1dSRodney W. Grimes 704fd91e076SKristof Provost void * 7059978bd99SMark Johnston malloc_domainset(size_t size, struct malloc_type *mtp, struct domainset *ds, 7069978bd99SMark Johnston int flags) 7079978bd99SMark Johnston { 7089978bd99SMark Johnston struct vm_domainset_iter di; 70982c174a3SMateusz Guzik caddr_t va; 7109978bd99SMark Johnston int domain; 7116d6a03d7SJeff Roberson int indx; 7129978bd99SMark Johnston 7136d6a03d7SJeff Roberson #if defined(DEBUG_REDZONE) 7146d6a03d7SJeff Roberson unsigned long osize = size; 7156d6a03d7SJeff Roberson #endif 71682c174a3SMateusz Guzik MPASS((flags & M_EXEC) == 0); 7176d6a03d7SJeff Roberson #ifdef MALLOC_DEBUG 71882c174a3SMateusz Guzik va = NULL; 71982c174a3SMateusz Guzik if (malloc_dbg(&va, &size, mtp, flags) != 0) 72082c174a3SMateusz Guzik return (va); 7216d6a03d7SJeff Roberson #endif 72282c174a3SMateusz Guzik if (size <= kmem_zmax) { 7239978bd99SMark Johnston vm_domainset_iter_policy_init(&di, ds, &domain, &flags); 7249978bd99SMark Johnston do { 72582c174a3SMateusz Guzik va = malloc_domain(&size, &indx, mtp, domain, flags); 72682c174a3SMateusz Guzik } while (va == NULL && 7276d6a03d7SJeff Roberson vm_domainset_iter_policy(&di, &domain) == 0); 72882c174a3SMateusz Guzik malloc_type_zone_allocated(mtp, va == NULL ? 0 : size, indx); 7296d6a03d7SJeff Roberson } else { 7306d6a03d7SJeff Roberson /* Policy is handled by kmem. */ 73182c174a3SMateusz Guzik va = malloc_large(&size, ds, flags); 73282c174a3SMateusz Guzik malloc_type_allocated(mtp, va == NULL ? 0 : size); 73382c174a3SMateusz Guzik } 73482c174a3SMateusz Guzik if (__predict_false(va == NULL)) { 73582c174a3SMateusz Guzik KASSERT((flags & M_WAITOK) == 0, 73682c174a3SMateusz Guzik ("malloc(M_WAITOK) returned NULL")); 73782c174a3SMateusz Guzik t_malloc_fail = time_uptime; 73882c174a3SMateusz Guzik } 73982c174a3SMateusz Guzik #ifdef DEBUG_REDZONE 74082c174a3SMateusz Guzik if (va != NULL) 74182c174a3SMateusz Guzik va = redzone_setup(va, osize); 74282c174a3SMateusz Guzik #endif 74382c174a3SMateusz Guzik return (va); 7446d6a03d7SJeff Roberson } 7459978bd99SMark Johnston 74682c174a3SMateusz Guzik /* 74782c174a3SMateusz Guzik * Allocate an executable area. 74882c174a3SMateusz Guzik */ 74982c174a3SMateusz Guzik void * 75082c174a3SMateusz Guzik malloc_exec(size_t size, struct malloc_type *mtp, int flags) 75182c174a3SMateusz Guzik { 75282c174a3SMateusz Guzik caddr_t va; 75382c174a3SMateusz Guzik #if defined(DEBUG_REDZONE) 75482c174a3SMateusz Guzik unsigned long osize = size; 7556d6a03d7SJeff Roberson #endif 75682c174a3SMateusz Guzik 75782c174a3SMateusz Guzik flags |= M_EXEC; 75882c174a3SMateusz Guzik #ifdef MALLOC_DEBUG 75982c174a3SMateusz Guzik va = NULL; 76082c174a3SMateusz Guzik if (malloc_dbg(&va, &size, mtp, flags) != 0) 76182c174a3SMateusz Guzik return (va); 76282c174a3SMateusz Guzik #endif 76382c174a3SMateusz Guzik va = malloc_large(&size, DOMAINSET_RR(), flags); 76482c174a3SMateusz Guzik malloc_type_allocated(mtp, va == NULL ? 0 : size); 76582c174a3SMateusz Guzik if (__predict_false(va == NULL)) { 76682c174a3SMateusz Guzik KASSERT((flags & M_WAITOK) == 0, 76782c174a3SMateusz Guzik ("malloc(M_WAITOK) returned NULL")); 76882c174a3SMateusz Guzik t_malloc_fail = time_uptime; 76982c174a3SMateusz Guzik } 77082c174a3SMateusz Guzik #ifdef DEBUG_REDZONE 77182c174a3SMateusz Guzik if (va != NULL) 77282c174a3SMateusz Guzik va = redzone_setup(va, osize); 77382c174a3SMateusz Guzik #endif 77482c174a3SMateusz Guzik return ((void *) va); 77582c174a3SMateusz Guzik } 77682c174a3SMateusz Guzik 77782c174a3SMateusz Guzik void * 77882c174a3SMateusz Guzik malloc_domainset_exec(size_t size, struct malloc_type *mtp, struct domainset *ds, 77982c174a3SMateusz Guzik int flags) 78082c174a3SMateusz Guzik { 78182c174a3SMateusz Guzik caddr_t va; 78282c174a3SMateusz Guzik #if defined(DEBUG_REDZONE) 78382c174a3SMateusz Guzik unsigned long osize = size; 78482c174a3SMateusz Guzik #endif 78582c174a3SMateusz Guzik 78682c174a3SMateusz Guzik flags |= M_EXEC; 78782c174a3SMateusz Guzik #ifdef MALLOC_DEBUG 78882c174a3SMateusz Guzik va = NULL; 78982c174a3SMateusz Guzik if (malloc_dbg(&va, &size, mtp, flags) != 0) 79082c174a3SMateusz Guzik return (va); 79182c174a3SMateusz Guzik #endif 79282c174a3SMateusz Guzik /* Policy is handled by kmem. */ 79382c174a3SMateusz Guzik va = malloc_large(&size, ds, flags); 79482c174a3SMateusz Guzik malloc_type_allocated(mtp, va == NULL ? 0 : size); 79582c174a3SMateusz Guzik if (__predict_false(va == NULL)) { 79682c174a3SMateusz Guzik KASSERT((flags & M_WAITOK) == 0, 79782c174a3SMateusz Guzik ("malloc(M_WAITOK) returned NULL")); 79882c174a3SMateusz Guzik t_malloc_fail = time_uptime; 79982c174a3SMateusz Guzik } 80082c174a3SMateusz Guzik #ifdef DEBUG_REDZONE 80182c174a3SMateusz Guzik if (va != NULL) 80282c174a3SMateusz Guzik va = redzone_setup(va, osize); 80382c174a3SMateusz Guzik #endif 80482c174a3SMateusz Guzik return (va); 8059978bd99SMark Johnston } 8069978bd99SMark Johnston 8079978bd99SMark Johnston void * 808fd91e076SKristof Provost mallocarray(size_t nmemb, size_t size, struct malloc_type *type, int flags) 809fd91e076SKristof Provost { 810fd91e076SKristof Provost 811c02fc960SConrad Meyer if (WOULD_OVERFLOW(nmemb, size)) 812c02fc960SConrad Meyer panic("mallocarray: %zu * %zu overflowed", nmemb, size); 813fd91e076SKristof Provost 814fd91e076SKristof Provost return (malloc(size * nmemb, type, flags)); 815fd91e076SKristof Provost } 816fd91e076SKristof Provost 817ab3185d1SJeff Roberson #ifdef INVARIANTS 818ab3185d1SJeff Roberson static void 819ab3185d1SJeff Roberson free_save_type(void *addr, struct malloc_type *mtp, u_long size) 820ab3185d1SJeff Roberson { 821ab3185d1SJeff Roberson struct malloc_type **mtpp = addr; 822ab3185d1SJeff Roberson 823ab3185d1SJeff Roberson /* 824ab3185d1SJeff Roberson * Cache a pointer to the malloc_type that most recently freed 825ab3185d1SJeff Roberson * this memory here. This way we know who is most likely to 826ab3185d1SJeff Roberson * have stepped on it later. 827ab3185d1SJeff Roberson * 828ab3185d1SJeff Roberson * This code assumes that size is a multiple of 8 bytes for 829ab3185d1SJeff Roberson * 64 bit machines 830ab3185d1SJeff Roberson */ 831ab3185d1SJeff Roberson mtpp = (struct malloc_type **) ((unsigned long)mtpp & ~UMA_ALIGN_PTR); 832ab3185d1SJeff Roberson mtpp += (size - sizeof(struct malloc_type *)) / 833ab3185d1SJeff Roberson sizeof(struct malloc_type *); 834ab3185d1SJeff Roberson *mtpp = mtp; 835ab3185d1SJeff Roberson } 836ab3185d1SJeff Roberson #endif 837ab3185d1SJeff Roberson 838ab3185d1SJeff Roberson #ifdef MALLOC_DEBUG 839ab3185d1SJeff Roberson static int 840ab3185d1SJeff Roberson free_dbg(void **addrp, struct malloc_type *mtp) 841ab3185d1SJeff Roberson { 842ab3185d1SJeff Roberson void *addr; 843ab3185d1SJeff Roberson 844ab3185d1SJeff Roberson addr = *addrp; 845bdcc2226SMateusz Guzik KASSERT(mtp->ks_version == M_VERSION, ("free: bad malloc type version")); 846ab3185d1SJeff Roberson KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 847ab3185d1SJeff Roberson ("free: called with spinlock or critical section held")); 848ab3185d1SJeff Roberson 849ab3185d1SJeff Roberson /* free(NULL, ...) does nothing */ 850ab3185d1SJeff Roberson if (addr == NULL) 851ab3185d1SJeff Roberson return (EJUSTRETURN); 852ab3185d1SJeff Roberson 853ab3185d1SJeff Roberson #ifdef DEBUG_MEMGUARD 854ab3185d1SJeff Roberson if (is_memguard_addr(addr)) { 855ab3185d1SJeff Roberson memguard_free(addr); 856ab3185d1SJeff Roberson return (EJUSTRETURN); 857ab3185d1SJeff Roberson } 858ab3185d1SJeff Roberson #endif 859ab3185d1SJeff Roberson 860ab3185d1SJeff Roberson #ifdef DEBUG_REDZONE 861ab3185d1SJeff Roberson redzone_check(addr); 862ab3185d1SJeff Roberson *addrp = redzone_addr_ntor(addr); 863ab3185d1SJeff Roberson #endif 864ab3185d1SJeff Roberson 865ab3185d1SJeff Roberson return (0); 866ab3185d1SJeff Roberson } 867ab3185d1SJeff Roberson #endif 868ab3185d1SJeff Roberson 869fd91e076SKristof Provost /* 8701c7c3c6aSMatthew Dillon * free: 8711c7c3c6aSMatthew Dillon * 872df8bae1dSRodney W. Grimes * Free a block of memory allocated by malloc. 8731c7c3c6aSMatthew Dillon * 8741c7c3c6aSMatthew Dillon * This routine may not block. 875df8bae1dSRodney W. Grimes */ 876df8bae1dSRodney W. Grimes void 87763a7e0a3SRobert Watson free(void *addr, struct malloc_type *mtp) 878df8bae1dSRodney W. Grimes { 879584061b4SJeff Roberson uma_zone_t zone; 88099571dc3SJeff Roberson uma_slab_t slab; 88199571dc3SJeff Roberson u_long size; 882254c6cb3SPoul-Henning Kamp 883ab3185d1SJeff Roberson #ifdef MALLOC_DEBUG 884ab3185d1SJeff Roberson if (free_dbg(&addr, mtp) != 0) 885ab3185d1SJeff Roberson return; 886ab3185d1SJeff Roberson #endif 88744a8ff31SArchie Cobbs /* free(NULL, ...) does nothing */ 88844a8ff31SArchie Cobbs if (addr == NULL) 88944a8ff31SArchie Cobbs return; 89044a8ff31SArchie Cobbs 891584061b4SJeff Roberson vtozoneslab((vm_offset_t)addr & (~UMA_SLAB_MASK), &zone, &slab); 8928355f576SJeff Roberson if (slab == NULL) 8936f267175SJeff Roberson panic("free: address %p(%p) has not been allocated.\n", 89499571dc3SJeff Roberson addr, (void *)((u_long)addr & (~UMA_SLAB_MASK))); 89599571dc3SJeff Roberson 8966d6a03d7SJeff Roberson if (__predict_true(!malloc_large_slab(slab))) { 897584061b4SJeff Roberson size = zone->uz_size; 8988f70816cSJeff Roberson #ifdef INVARIANTS 899ab3185d1SJeff Roberson free_save_type(addr, mtp, size); 9008f70816cSJeff Roberson #endif 901584061b4SJeff Roberson uma_zfree_arg(zone, addr, slab); 90214bf02f8SJohn Dyson } else { 9036d6a03d7SJeff Roberson size = malloc_large_size(slab); 9046d6a03d7SJeff Roberson free_large(addr, size); 90514bf02f8SJohn Dyson } 90663a7e0a3SRobert Watson malloc_type_freed(mtp, size); 907df8bae1dSRodney W. Grimes } 908df8bae1dSRodney W. Grimes 90945035becSMatt Macy /* 91045035becSMatt Macy * zfree: 91145035becSMatt Macy * 91245035becSMatt Macy * Zero then free a block of memory allocated by malloc. 91345035becSMatt Macy * 91445035becSMatt Macy * This routine may not block. 91545035becSMatt Macy */ 91645035becSMatt Macy void 91745035becSMatt Macy zfree(void *addr, struct malloc_type *mtp) 91845035becSMatt Macy { 91945035becSMatt Macy uma_zone_t zone; 92045035becSMatt Macy uma_slab_t slab; 92145035becSMatt Macy u_long size; 92245035becSMatt Macy 92345035becSMatt Macy #ifdef MALLOC_DEBUG 92445035becSMatt Macy if (free_dbg(&addr, mtp) != 0) 92545035becSMatt Macy return; 92645035becSMatt Macy #endif 92745035becSMatt Macy /* free(NULL, ...) does nothing */ 92845035becSMatt Macy if (addr == NULL) 92945035becSMatt Macy return; 93045035becSMatt Macy 93145035becSMatt Macy vtozoneslab((vm_offset_t)addr & (~UMA_SLAB_MASK), &zone, &slab); 93245035becSMatt Macy if (slab == NULL) 93345035becSMatt Macy panic("free: address %p(%p) has not been allocated.\n", 93445035becSMatt Macy addr, (void *)((u_long)addr & (~UMA_SLAB_MASK))); 93545035becSMatt Macy 93645035becSMatt Macy if (__predict_true(!malloc_large_slab(slab))) { 93745035becSMatt Macy size = zone->uz_size; 93845035becSMatt Macy #ifdef INVARIANTS 93945035becSMatt Macy free_save_type(addr, mtp, size); 94045035becSMatt Macy #endif 94145035becSMatt Macy explicit_bzero(addr, size); 94245035becSMatt Macy uma_zfree_arg(zone, addr, slab); 94345035becSMatt Macy } else { 94445035becSMatt Macy size = malloc_large_size(slab); 94545035becSMatt Macy explicit_bzero(addr, size); 94645035becSMatt Macy free_large(addr, size); 94745035becSMatt Macy } 94845035becSMatt Macy malloc_type_freed(mtp, size); 94945035becSMatt Macy } 95045035becSMatt Macy 951df8bae1dSRodney W. Grimes /* 95244a8ff31SArchie Cobbs * realloc: change the size of a memory block 95344a8ff31SArchie Cobbs */ 95444a8ff31SArchie Cobbs void * 955bd555da9SConrad Meyer realloc(void *addr, size_t size, struct malloc_type *mtp, int flags) 95644a8ff31SArchie Cobbs { 957584061b4SJeff Roberson uma_zone_t zone; 9588355f576SJeff Roberson uma_slab_t slab; 95944a8ff31SArchie Cobbs unsigned long alloc; 96044a8ff31SArchie Cobbs void *newaddr; 96144a8ff31SArchie Cobbs 962bdcc2226SMateusz Guzik KASSERT(mtp->ks_version == M_VERSION, 963bdcc2226SMateusz Guzik ("realloc: bad malloc type version")); 964d9e2e68dSMark Johnston KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 9651067a2baSJonathan T. Looney ("realloc: called with spinlock or critical section held")); 9661067a2baSJonathan T. Looney 96744a8ff31SArchie Cobbs /* realloc(NULL, ...) is equivalent to malloc(...) */ 96844a8ff31SArchie Cobbs if (addr == NULL) 96963a7e0a3SRobert Watson return (malloc(size, mtp, flags)); 97063a7e0a3SRobert Watson 97163a7e0a3SRobert Watson /* 97263a7e0a3SRobert Watson * XXX: Should report free of old memory and alloc of new memory to 97363a7e0a3SRobert Watson * per-CPU stats. 97463a7e0a3SRobert Watson */ 97544a8ff31SArchie Cobbs 976e4eb384bSBosko Milekic #ifdef DEBUG_MEMGUARD 9776d3ed393SMatthew D Fleming if (is_memguard_addr(addr)) 9786d3ed393SMatthew D Fleming return (memguard_realloc(addr, size, mtp, flags)); 979e4eb384bSBosko Milekic #endif 980e4eb384bSBosko Milekic 981847a2a17SPawel Jakub Dawidek #ifdef DEBUG_REDZONE 982847a2a17SPawel Jakub Dawidek slab = NULL; 983b476ae7fSJeff Roberson zone = NULL; 984847a2a17SPawel Jakub Dawidek alloc = redzone_get_size(addr); 985847a2a17SPawel Jakub Dawidek #else 986584061b4SJeff Roberson vtozoneslab((vm_offset_t)addr & (~UMA_SLAB_MASK), &zone, &slab); 9878355f576SJeff Roberson 98844a8ff31SArchie Cobbs /* Sanity check */ 9898355f576SJeff Roberson KASSERT(slab != NULL, 99044a8ff31SArchie Cobbs ("realloc: address %p out of range", (void *)addr)); 99144a8ff31SArchie Cobbs 99244a8ff31SArchie Cobbs /* Get the size of the original block */ 9936d6a03d7SJeff Roberson if (!malloc_large_slab(slab)) 994584061b4SJeff Roberson alloc = zone->uz_size; 9958355f576SJeff Roberson else 9966d6a03d7SJeff Roberson alloc = malloc_large_size(slab); 99744a8ff31SArchie Cobbs 99844a8ff31SArchie Cobbs /* Reuse the original block if appropriate */ 99944a8ff31SArchie Cobbs if (size <= alloc 100044a8ff31SArchie Cobbs && (size > (alloc >> REALLOC_FRACTION) || alloc == MINALLOCSIZE)) 100144a8ff31SArchie Cobbs return (addr); 1002847a2a17SPawel Jakub Dawidek #endif /* !DEBUG_REDZONE */ 100344a8ff31SArchie Cobbs 100444a8ff31SArchie Cobbs /* Allocate a new, bigger (or smaller) block */ 100563a7e0a3SRobert Watson if ((newaddr = malloc(size, mtp, flags)) == NULL) 100644a8ff31SArchie Cobbs return (NULL); 100744a8ff31SArchie Cobbs 100844a8ff31SArchie Cobbs /* Copy over original contents */ 100944a8ff31SArchie Cobbs bcopy(addr, newaddr, min(size, alloc)); 101063a7e0a3SRobert Watson free(addr, mtp); 101144a8ff31SArchie Cobbs return (newaddr); 101244a8ff31SArchie Cobbs } 101344a8ff31SArchie Cobbs 101444a8ff31SArchie Cobbs /* 101544a8ff31SArchie Cobbs * reallocf: same as realloc() but free memory on failure. 101644a8ff31SArchie Cobbs */ 101744a8ff31SArchie Cobbs void * 1018bd555da9SConrad Meyer reallocf(void *addr, size_t size, struct malloc_type *mtp, int flags) 101944a8ff31SArchie Cobbs { 102044a8ff31SArchie Cobbs void *mem; 102144a8ff31SArchie Cobbs 102263a7e0a3SRobert Watson if ((mem = realloc(addr, size, mtp, flags)) == NULL) 102363a7e0a3SRobert Watson free(addr, mtp); 102444a8ff31SArchie Cobbs return (mem); 102544a8ff31SArchie Cobbs } 102644a8ff31SArchie Cobbs 10275d4bf057SVladimir Kondratyev /* 102816b971edSMateusz Guzik * malloc_size: returns the number of bytes allocated for a request of the 102916b971edSMateusz Guzik * specified size 103016b971edSMateusz Guzik */ 103116b971edSMateusz Guzik size_t 103216b971edSMateusz Guzik malloc_size(size_t size) 103316b971edSMateusz Guzik { 103416b971edSMateusz Guzik int indx; 103516b971edSMateusz Guzik 103616b971edSMateusz Guzik if (size > kmem_zmax) 103716b971edSMateusz Guzik return (0); 103816b971edSMateusz Guzik if (size & KMEM_ZMASK) 103916b971edSMateusz Guzik size = (size & ~KMEM_ZMASK) + KMEM_ZBASE; 104016b971edSMateusz Guzik indx = kmemsize[size >> KMEM_ZSHIFT]; 104116b971edSMateusz Guzik return (kmemzones[indx].kz_size); 104216b971edSMateusz Guzik } 104316b971edSMateusz Guzik 104416b971edSMateusz Guzik /* 10455d4bf057SVladimir Kondratyev * malloc_usable_size: returns the usable size of the allocation. 10465d4bf057SVladimir Kondratyev */ 10475d4bf057SVladimir Kondratyev size_t 10485d4bf057SVladimir Kondratyev malloc_usable_size(const void *addr) 10495d4bf057SVladimir Kondratyev { 10505d4bf057SVladimir Kondratyev #ifndef DEBUG_REDZONE 10515d4bf057SVladimir Kondratyev uma_zone_t zone; 10525d4bf057SVladimir Kondratyev uma_slab_t slab; 10535d4bf057SVladimir Kondratyev #endif 10545d4bf057SVladimir Kondratyev u_long size; 10555d4bf057SVladimir Kondratyev 10565d4bf057SVladimir Kondratyev if (addr == NULL) 10575d4bf057SVladimir Kondratyev return (0); 10585d4bf057SVladimir Kondratyev 10595d4bf057SVladimir Kondratyev #ifdef DEBUG_MEMGUARD 10605d4bf057SVladimir Kondratyev if (is_memguard_addr(__DECONST(void *, addr))) 10615d4bf057SVladimir Kondratyev return (memguard_get_req_size(addr)); 10625d4bf057SVladimir Kondratyev #endif 10635d4bf057SVladimir Kondratyev 10645d4bf057SVladimir Kondratyev #ifdef DEBUG_REDZONE 10655d4bf057SVladimir Kondratyev size = redzone_get_size(__DECONST(void *, addr)); 10665d4bf057SVladimir Kondratyev #else 10675d4bf057SVladimir Kondratyev vtozoneslab((vm_offset_t)addr & (~UMA_SLAB_MASK), &zone, &slab); 10685d4bf057SVladimir Kondratyev if (slab == NULL) 10695d4bf057SVladimir Kondratyev panic("malloc_usable_size: address %p(%p) is not allocated.\n", 10705d4bf057SVladimir Kondratyev addr, (void *)((u_long)addr & (~UMA_SLAB_MASK))); 10715d4bf057SVladimir Kondratyev 10725d4bf057SVladimir Kondratyev if (!malloc_large_slab(slab)) 10735d4bf057SVladimir Kondratyev size = zone->uz_size; 10745d4bf057SVladimir Kondratyev else 10755d4bf057SVladimir Kondratyev size = malloc_large_size(slab); 10765d4bf057SVladimir Kondratyev #endif 10775d4bf057SVladimir Kondratyev return (size); 10785d4bf057SVladimir Kondratyev } 10795d4bf057SVladimir Kondratyev 1080c70af487SAlan Cox CTASSERT(VM_KMEM_SIZE_SCALE >= 1); 1081c70af487SAlan Cox 10825df87b21SJeff Roberson /* 1083c70af487SAlan Cox * Initialize the kernel memory (kmem) arena. 10845df87b21SJeff Roberson */ 10855df87b21SJeff Roberson void 10865df87b21SJeff Roberson kmeminit(void) 10875df87b21SJeff Roberson { 1088af3b2549SHans Petter Selasky u_long mem_size; 1089af3b2549SHans Petter Selasky u_long tmp; 109069ef67f9SJason Evans 1091af3b2549SHans Petter Selasky #ifdef VM_KMEM_SIZE 1092af3b2549SHans Petter Selasky if (vm_kmem_size == 0) 1093af3b2549SHans Petter Selasky vm_kmem_size = VM_KMEM_SIZE; 1094af3b2549SHans Petter Selasky #endif 1095af3b2549SHans Petter Selasky #ifdef VM_KMEM_SIZE_MIN 1096af3b2549SHans Petter Selasky if (vm_kmem_size_min == 0) 1097af3b2549SHans Petter Selasky vm_kmem_size_min = VM_KMEM_SIZE_MIN; 1098af3b2549SHans Petter Selasky #endif 1099af3b2549SHans Petter Selasky #ifdef VM_KMEM_SIZE_MAX 1100af3b2549SHans Petter Selasky if (vm_kmem_size_max == 0) 1101af3b2549SHans Petter Selasky vm_kmem_size_max = VM_KMEM_SIZE_MAX; 1102af3b2549SHans Petter Selasky #endif 11038a58a9f6SJohn Dyson /* 1104c70af487SAlan Cox * Calculate the amount of kernel virtual address (KVA) space that is 1105c70af487SAlan Cox * preallocated to the kmem arena. In order to support a wide range 1106c70af487SAlan Cox * of machines, it is a function of the physical memory size, 1107c70af487SAlan Cox * specifically, 11088a58a9f6SJohn Dyson * 1109c70af487SAlan Cox * min(max(physical memory size / VM_KMEM_SIZE_SCALE, 1110c70af487SAlan Cox * VM_KMEM_SIZE_MIN), VM_KMEM_SIZE_MAX) 1111c70af487SAlan Cox * 1112c70af487SAlan Cox * Every architecture must define an integral value for 1113c70af487SAlan Cox * VM_KMEM_SIZE_SCALE. However, the definitions of VM_KMEM_SIZE_MIN 1114c70af487SAlan Cox * and VM_KMEM_SIZE_MAX, which represent respectively the floor and 1115c70af487SAlan Cox * ceiling on this preallocation, are optional. Typically, 1116c70af487SAlan Cox * VM_KMEM_SIZE_MAX is itself a function of the available KVA space on 1117c70af487SAlan Cox * a given architecture. 11188a58a9f6SJohn Dyson */ 111944f1c916SBryan Drewery mem_size = vm_cnt.v_page_count; 11207c51714eSSean Bruno if (mem_size <= 32768) /* delphij XXX 128MB */ 11217c51714eSSean Bruno kmem_zmax = PAGE_SIZE; 11228a58a9f6SJohn Dyson 1123c70af487SAlan Cox if (vm_kmem_size_scale < 1) 1124c70af487SAlan Cox vm_kmem_size_scale = VM_KMEM_SIZE_SCALE; 1125c70af487SAlan Cox 1126af3b2549SHans Petter Selasky /* 1127af3b2549SHans Petter Selasky * Check if we should use defaults for the "vm_kmem_size" 1128af3b2549SHans Petter Selasky * variable: 1129af3b2549SHans Petter Selasky */ 1130af3b2549SHans Petter Selasky if (vm_kmem_size == 0) { 113128b740daSKonstantin Belousov vm_kmem_size = mem_size / vm_kmem_size_scale; 113228b740daSKonstantin Belousov vm_kmem_size = vm_kmem_size * PAGE_SIZE < vm_kmem_size ? 113328b740daSKonstantin Belousov vm_kmem_size_max : vm_kmem_size * PAGE_SIZE; 1134c70af487SAlan Cox if (vm_kmem_size_min > 0 && vm_kmem_size < vm_kmem_size_min) 11350e5179e4SStephane E. Potvin vm_kmem_size = vm_kmem_size_min; 1136479439b4SDag-Erling Smørgrav if (vm_kmem_size_max > 0 && vm_kmem_size >= vm_kmem_size_max) 1137479439b4SDag-Erling Smørgrav vm_kmem_size = vm_kmem_size_max; 1138af3b2549SHans Petter Selasky } 113928b740daSKonstantin Belousov if (vm_kmem_size == 0) 114028b740daSKonstantin Belousov panic("Tune VM_KMEM_SIZE_* for the platform"); 11418a58a9f6SJohn Dyson 114227b8623fSDavid Greenman /* 1143af3b2549SHans Petter Selasky * The amount of KVA space that is preallocated to the 1144c70af487SAlan Cox * kmem arena can be set statically at compile-time or manually 1145c70af487SAlan Cox * through the kernel environment. However, it is still limited to 1146c70af487SAlan Cox * twice the physical memory size, which has been sufficient to handle 1147c70af487SAlan Cox * the most severe cases of external fragmentation in the kmem arena. 114827b8623fSDavid Greenman */ 1149c749c003SAlan Cox if (vm_kmem_size / 2 / PAGE_SIZE > mem_size) 1150c749c003SAlan Cox vm_kmem_size = 2 * mem_size * PAGE_SIZE; 11518a58a9f6SJohn Dyson 1152e137643eSOlivier Houchard vm_kmem_size = round_page(vm_kmem_size); 1153e3813573SMatthew D Fleming #ifdef DEBUG_MEMGUARD 1154f806cdcfSMatthew D Fleming tmp = memguard_fudge(vm_kmem_size, kernel_map); 1155e3813573SMatthew D Fleming #else 1156e3813573SMatthew D Fleming tmp = vm_kmem_size; 1157e3813573SMatthew D Fleming #endif 11582e47807cSJeff Roberson uma_set_limit(tmp); 11598355f576SJeff Roberson 1160e4eb384bSBosko Milekic #ifdef DEBUG_MEMGUARD 1161e4eb384bSBosko Milekic /* 1162e4eb384bSBosko Milekic * Initialize MemGuard if support compiled in. MemGuard is a 1163e4eb384bSBosko Milekic * replacement allocator used for detecting tamper-after-free 1164e4eb384bSBosko Milekic * scenarios as they occur. It is only used for debugging. 1165e4eb384bSBosko Milekic */ 11662e47807cSJeff Roberson memguard_init(kernel_arena); 1167e4eb384bSBosko Milekic #endif 11685df87b21SJeff Roberson } 11695df87b21SJeff Roberson 11705df87b21SJeff Roberson /* 11715df87b21SJeff Roberson * Initialize the kernel memory allocator 11725df87b21SJeff Roberson */ 11735df87b21SJeff Roberson /* ARGSUSED*/ 11745df87b21SJeff Roberson static void 11755df87b21SJeff Roberson mallocinit(void *dummy) 11765df87b21SJeff Roberson { 11775df87b21SJeff Roberson int i; 11785df87b21SJeff Roberson uint8_t indx; 11795df87b21SJeff Roberson 11805df87b21SJeff Roberson mtx_init(&malloc_mtx, "malloc", NULL, MTX_DEF); 11815df87b21SJeff Roberson 11825df87b21SJeff Roberson kmeminit(); 1183e4eb384bSBosko Milekic 11847001d850SXin LI if (kmem_zmax < PAGE_SIZE || kmem_zmax > KMEM_ZMAX) 11857001d850SXin LI kmem_zmax = KMEM_ZMAX; 11867001d850SXin LI 11879afff6b1SMateusz Guzik mt_stats_zone = uma_zcreate("mt_stats_zone", 11889afff6b1SMateusz Guzik sizeof(struct malloc_type_stats), NULL, NULL, NULL, NULL, 11899afff6b1SMateusz Guzik UMA_ALIGN_PTR, UMA_ZONE_PCPU); 11906f267175SJeff Roberson for (i = 0, indx = 0; kmemzones[indx].kz_size != 0; indx++) { 11916f267175SJeff Roberson int size = kmemzones[indx].kz_size; 1192eaa17d42SRyan Libby const char *name = kmemzones[indx].kz_name; 1193d7854da1SMatthew D Fleming int subzone; 11948355f576SJeff Roberson 1195d7854da1SMatthew D Fleming for (subzone = 0; subzone < numzones; subzone++) { 1196d7854da1SMatthew D Fleming kmemzones[indx].kz_zone[subzone] = 1197d7854da1SMatthew D Fleming uma_zcreate(name, size, 11988efc4effSJeff Roberson #ifdef INVARIANTS 11998f70816cSJeff Roberson mtrash_ctor, mtrash_dtor, mtrash_init, mtrash_fini, 12008efc4effSJeff Roberson #else 12018efc4effSJeff Roberson NULL, NULL, NULL, NULL, 12028efc4effSJeff Roberson #endif 12038efc4effSJeff Roberson UMA_ALIGN_PTR, UMA_ZONE_MALLOC); 1204d7854da1SMatthew D Fleming } 12058355f576SJeff Roberson for (;i <= size; i+= KMEM_ZBASE) 12066f267175SJeff Roberson kmemsize[i >> KMEM_ZSHIFT] = indx; 1207df8bae1dSRodney W. Grimes } 1208254c6cb3SPoul-Henning Kamp } 1209af3b2549SHans Petter Selasky SYSINIT(kmem, SI_SUB_KMEM, SI_ORDER_SECOND, mallocinit, NULL); 1210254c6cb3SPoul-Henning Kamp 1211db669378SPeter Wemm void 121287efd4d5SRobert Watson malloc_init(void *data) 1213254c6cb3SPoul-Henning Kamp { 121463a7e0a3SRobert Watson struct malloc_type_internal *mtip; 121563a7e0a3SRobert Watson struct malloc_type *mtp; 121663a7e0a3SRobert Watson 121744f1c916SBryan Drewery KASSERT(vm_cnt.v_page_count != 0, ("malloc_register before vm_init")); 121863a7e0a3SRobert Watson 121963a7e0a3SRobert Watson mtp = data; 1220bdcc2226SMateusz Guzik if (mtp->ks_version != M_VERSION) 1221*e25d8b67SMateusz Guzik panic("malloc_init: type %s with unsupported version %lu", 1222*e25d8b67SMateusz Guzik mtp->ks_shortdesc, mtp->ks_version); 1223bb1c7df8SRobert Watson 1224bdcc2226SMateusz Guzik mtip = &mtp->ks_mti; 12259afff6b1SMateusz Guzik mtip->mti_stats = uma_zalloc_pcpu(mt_stats_zone, M_WAITOK | M_ZERO); 1226c9e05ccdSMateusz Guzik mtp_set_subzone(mtp); 1227254c6cb3SPoul-Henning Kamp 12286f267175SJeff Roberson mtx_lock(&malloc_mtx); 122963a7e0a3SRobert Watson mtp->ks_next = kmemstatistics; 123063a7e0a3SRobert Watson kmemstatistics = mtp; 1231cd814b26SRobert Watson kmemcount++; 12326f267175SJeff Roberson mtx_unlock(&malloc_mtx); 1233df8bae1dSRodney W. Grimes } 1234db669378SPeter Wemm 1235db669378SPeter Wemm void 123687efd4d5SRobert Watson malloc_uninit(void *data) 1237db669378SPeter Wemm { 123863a7e0a3SRobert Watson struct malloc_type_internal *mtip; 12392a143d5bSPawel Jakub Dawidek struct malloc_type_stats *mtsp; 124063a7e0a3SRobert Watson struct malloc_type *mtp, *temp; 12412a143d5bSPawel Jakub Dawidek long temp_allocs, temp_bytes; 12422a143d5bSPawel Jakub Dawidek int i; 1243db669378SPeter Wemm 124463a7e0a3SRobert Watson mtp = data; 1245bdcc2226SMateusz Guzik KASSERT(mtp->ks_version == M_VERSION, 1246bdcc2226SMateusz Guzik ("malloc_uninit: bad malloc type version")); 1247bb1c7df8SRobert Watson 12486f267175SJeff Roberson mtx_lock(&malloc_mtx); 1249bdcc2226SMateusz Guzik mtip = &mtp->ks_mti; 125063a7e0a3SRobert Watson if (mtp != kmemstatistics) { 125163a7e0a3SRobert Watson for (temp = kmemstatistics; temp != NULL; 125263a7e0a3SRobert Watson temp = temp->ks_next) { 1253f121baaaSBrian Somers if (temp->ks_next == mtp) { 125463a7e0a3SRobert Watson temp->ks_next = mtp->ks_next; 1255f121baaaSBrian Somers break; 1256db669378SPeter Wemm } 1257f121baaaSBrian Somers } 1258f121baaaSBrian Somers KASSERT(temp, 1259f121baaaSBrian Somers ("malloc_uninit: type '%s' not found", mtp->ks_shortdesc)); 126063a7e0a3SRobert Watson } else 126163a7e0a3SRobert Watson kmemstatistics = mtp->ks_next; 1262cd814b26SRobert Watson kmemcount--; 12636f267175SJeff Roberson mtx_unlock(&malloc_mtx); 12642a143d5bSPawel Jakub Dawidek 12652a143d5bSPawel Jakub Dawidek /* 12662a143d5bSPawel Jakub Dawidek * Look for memory leaks. 12672a143d5bSPawel Jakub Dawidek */ 12682a143d5bSPawel Jakub Dawidek temp_allocs = temp_bytes = 0; 12699afff6b1SMateusz Guzik for (i = 0; i <= mp_maxid; i++) { 12709afff6b1SMateusz Guzik mtsp = zpcpu_get_cpu(mtip->mti_stats, i); 12712a143d5bSPawel Jakub Dawidek temp_allocs += mtsp->mts_numallocs; 12722a143d5bSPawel Jakub Dawidek temp_allocs -= mtsp->mts_numfrees; 12732a143d5bSPawel Jakub Dawidek temp_bytes += mtsp->mts_memalloced; 12742a143d5bSPawel Jakub Dawidek temp_bytes -= mtsp->mts_memfreed; 12752a143d5bSPawel Jakub Dawidek } 12762a143d5bSPawel Jakub Dawidek if (temp_allocs > 0 || temp_bytes > 0) { 12772a143d5bSPawel Jakub Dawidek printf("Warning: memory type %s leaked memory on destroy " 12782a143d5bSPawel Jakub Dawidek "(%ld allocations, %ld bytes leaked).\n", mtp->ks_shortdesc, 12792a143d5bSPawel Jakub Dawidek temp_allocs, temp_bytes); 12802a143d5bSPawel Jakub Dawidek } 12812a143d5bSPawel Jakub Dawidek 12829afff6b1SMateusz Guzik uma_zfree_pcpu(mt_stats_zone, mtip->mti_stats); 1283db669378SPeter Wemm } 12846f267175SJeff Roberson 1285d362c40dSPawel Jakub Dawidek struct malloc_type * 1286d362c40dSPawel Jakub Dawidek malloc_desc2type(const char *desc) 1287d362c40dSPawel Jakub Dawidek { 1288d362c40dSPawel Jakub Dawidek struct malloc_type *mtp; 1289d362c40dSPawel Jakub Dawidek 1290d362c40dSPawel Jakub Dawidek mtx_assert(&malloc_mtx, MA_OWNED); 1291d362c40dSPawel Jakub Dawidek for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) { 1292d362c40dSPawel Jakub Dawidek if (strcmp(mtp->ks_shortdesc, desc) == 0) 1293d362c40dSPawel Jakub Dawidek return (mtp); 1294d362c40dSPawel Jakub Dawidek } 1295d362c40dSPawel Jakub Dawidek return (NULL); 1296d362c40dSPawel Jakub Dawidek } 1297d362c40dSPawel Jakub Dawidek 12986f267175SJeff Roberson static int 1299cd814b26SRobert Watson sysctl_kern_malloc_stats(SYSCTL_HANDLER_ARGS) 1300cd814b26SRobert Watson { 1301cd814b26SRobert Watson struct malloc_type_stream_header mtsh; 1302cd814b26SRobert Watson struct malloc_type_internal *mtip; 13039afff6b1SMateusz Guzik struct malloc_type_stats *mtsp, zeromts; 1304cd814b26SRobert Watson struct malloc_type_header mth; 1305cd814b26SRobert Watson struct malloc_type *mtp; 13064e657159SMatthew D Fleming int error, i; 1307cd814b26SRobert Watson struct sbuf sbuf; 1308cd814b26SRobert Watson 130900f0e671SMatthew D Fleming error = sysctl_wire_old_buffer(req, 0); 131000f0e671SMatthew D Fleming if (error != 0) 131100f0e671SMatthew D Fleming return (error); 13124e657159SMatthew D Fleming sbuf_new_for_sysctl(&sbuf, NULL, 128, req); 13131eafc078SIan Lepore sbuf_clear_flags(&sbuf, SBUF_INCLUDENUL); 1314cd814b26SRobert Watson mtx_lock(&malloc_mtx); 1315cd814b26SRobert Watson 13169afff6b1SMateusz Guzik bzero(&zeromts, sizeof(zeromts)); 13179afff6b1SMateusz Guzik 1318cd814b26SRobert Watson /* 1319cd814b26SRobert Watson * Insert stream header. 1320cd814b26SRobert Watson */ 1321cd814b26SRobert Watson bzero(&mtsh, sizeof(mtsh)); 1322cd814b26SRobert Watson mtsh.mtsh_version = MALLOC_TYPE_STREAM_VERSION; 1323cd814b26SRobert Watson mtsh.mtsh_maxcpus = MAXCPU; 1324cd814b26SRobert Watson mtsh.mtsh_count = kmemcount; 13254e657159SMatthew D Fleming (void)sbuf_bcat(&sbuf, &mtsh, sizeof(mtsh)); 1326cd814b26SRobert Watson 1327cd814b26SRobert Watson /* 1328cd814b26SRobert Watson * Insert alternating sequence of type headers and type statistics. 1329cd814b26SRobert Watson */ 1330cd814b26SRobert Watson for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) { 1331bdcc2226SMateusz Guzik mtip = &mtp->ks_mti; 1332cd814b26SRobert Watson 1333cd814b26SRobert Watson /* 1334cd814b26SRobert Watson * Insert type header. 1335cd814b26SRobert Watson */ 1336cd814b26SRobert Watson bzero(&mth, sizeof(mth)); 1337cd814b26SRobert Watson strlcpy(mth.mth_name, mtp->ks_shortdesc, MALLOC_MAX_NAME); 13384e657159SMatthew D Fleming (void)sbuf_bcat(&sbuf, &mth, sizeof(mth)); 1339cd814b26SRobert Watson 1340cd814b26SRobert Watson /* 1341cd814b26SRobert Watson * Insert type statistics for each CPU. 1342cd814b26SRobert Watson */ 13439afff6b1SMateusz Guzik for (i = 0; i <= mp_maxid; i++) { 13449afff6b1SMateusz Guzik mtsp = zpcpu_get_cpu(mtip->mti_stats, i); 13459afff6b1SMateusz Guzik (void)sbuf_bcat(&sbuf, mtsp, sizeof(*mtsp)); 1346cd814b26SRobert Watson } 13479afff6b1SMateusz Guzik /* 13489afff6b1SMateusz Guzik * Fill in the missing CPUs. 13499afff6b1SMateusz Guzik */ 13509afff6b1SMateusz Guzik for (; i < MAXCPU; i++) { 13519afff6b1SMateusz Guzik (void)sbuf_bcat(&sbuf, &zeromts, sizeof(zeromts)); 13529afff6b1SMateusz Guzik } 1353cd814b26SRobert Watson } 1354cd814b26SRobert Watson mtx_unlock(&malloc_mtx); 13554e657159SMatthew D Fleming error = sbuf_finish(&sbuf); 1356cd814b26SRobert Watson sbuf_delete(&sbuf); 1357cd814b26SRobert Watson return (error); 1358cd814b26SRobert Watson } 1359cd814b26SRobert Watson 13607029da5cSPawel Biernacki SYSCTL_PROC(_kern, OID_AUTO, malloc_stats, 13617029da5cSPawel Biernacki CTLFLAG_RD | CTLTYPE_STRUCT | CTLFLAG_MPSAFE, 0, 0, 13627029da5cSPawel Biernacki sysctl_kern_malloc_stats, "s,malloc_type_ustats", 1363cd814b26SRobert Watson "Return malloc types"); 1364cd814b26SRobert Watson 1365cd814b26SRobert Watson SYSCTL_INT(_kern, OID_AUTO, malloc_count, CTLFLAG_RD, &kmemcount, 0, 1366cd814b26SRobert Watson "Count of kernel malloc types"); 1367cd814b26SRobert Watson 136891dd776cSJohn Birrell void 136991dd776cSJohn Birrell malloc_type_list(malloc_type_list_func_t *func, void *arg) 137091dd776cSJohn Birrell { 137191dd776cSJohn Birrell struct malloc_type *mtp, **bufmtp; 137291dd776cSJohn Birrell int count, i; 137391dd776cSJohn Birrell size_t buflen; 137491dd776cSJohn Birrell 137591dd776cSJohn Birrell mtx_lock(&malloc_mtx); 137691dd776cSJohn Birrell restart: 137791dd776cSJohn Birrell mtx_assert(&malloc_mtx, MA_OWNED); 137891dd776cSJohn Birrell count = kmemcount; 137991dd776cSJohn Birrell mtx_unlock(&malloc_mtx); 138091dd776cSJohn Birrell 138191dd776cSJohn Birrell buflen = sizeof(struct malloc_type *) * count; 138291dd776cSJohn Birrell bufmtp = malloc(buflen, M_TEMP, M_WAITOK); 138391dd776cSJohn Birrell 138491dd776cSJohn Birrell mtx_lock(&malloc_mtx); 138591dd776cSJohn Birrell 138691dd776cSJohn Birrell if (count < kmemcount) { 138791dd776cSJohn Birrell free(bufmtp, M_TEMP); 138891dd776cSJohn Birrell goto restart; 138991dd776cSJohn Birrell } 139091dd776cSJohn Birrell 139191dd776cSJohn Birrell for (mtp = kmemstatistics, i = 0; mtp != NULL; mtp = mtp->ks_next, i++) 139291dd776cSJohn Birrell bufmtp[i] = mtp; 139391dd776cSJohn Birrell 139491dd776cSJohn Birrell mtx_unlock(&malloc_mtx); 139591dd776cSJohn Birrell 139691dd776cSJohn Birrell for (i = 0; i < count; i++) 139791dd776cSJohn Birrell (func)(bufmtp[i], arg); 139891dd776cSJohn Birrell 139991dd776cSJohn Birrell free(bufmtp, M_TEMP); 140091dd776cSJohn Birrell } 140191dd776cSJohn Birrell 1402909ed16cSRobert Watson #ifdef DDB 140346d70077SConrad Meyer static int64_t 140446d70077SConrad Meyer get_malloc_stats(const struct malloc_type_internal *mtip, uint64_t *allocs, 140546d70077SConrad Meyer uint64_t *inuse) 1406909ed16cSRobert Watson { 140746d70077SConrad Meyer const struct malloc_type_stats *mtsp; 140846d70077SConrad Meyer uint64_t frees, alloced, freed; 1409909ed16cSRobert Watson int i; 1410909ed16cSRobert Watson 141146d70077SConrad Meyer *allocs = 0; 1412909ed16cSRobert Watson frees = 0; 141324076d13SRobert Watson alloced = 0; 141424076d13SRobert Watson freed = 0; 14159afff6b1SMateusz Guzik for (i = 0; i <= mp_maxid; i++) { 14169afff6b1SMateusz Guzik mtsp = zpcpu_get_cpu(mtip->mti_stats, i); 141746d70077SConrad Meyer 141846d70077SConrad Meyer *allocs += mtsp->mts_numallocs; 141926e9d9b0SMark Johnston frees += mtsp->mts_numfrees; 142026e9d9b0SMark Johnston alloced += mtsp->mts_memalloced; 142126e9d9b0SMark Johnston freed += mtsp->mts_memfreed; 1422909ed16cSRobert Watson } 142346d70077SConrad Meyer *inuse = *allocs - frees; 142446d70077SConrad Meyer return (alloced - freed); 142546d70077SConrad Meyer } 142646d70077SConrad Meyer 142746d70077SConrad Meyer DB_SHOW_COMMAND(malloc, db_show_malloc) 142846d70077SConrad Meyer { 142946d70077SConrad Meyer const char *fmt_hdr, *fmt_entry; 143046d70077SConrad Meyer struct malloc_type *mtp; 143146d70077SConrad Meyer uint64_t allocs, inuse; 143246d70077SConrad Meyer int64_t size; 143346d70077SConrad Meyer /* variables for sorting */ 143446d70077SConrad Meyer struct malloc_type *last_mtype, *cur_mtype; 143546d70077SConrad Meyer int64_t cur_size, last_size; 143646d70077SConrad Meyer int ties; 143746d70077SConrad Meyer 143846d70077SConrad Meyer if (modif[0] == 'i') { 143946d70077SConrad Meyer fmt_hdr = "%s,%s,%s,%s\n"; 144046d70077SConrad Meyer fmt_entry = "\"%s\",%ju,%jdK,%ju\n"; 144146d70077SConrad Meyer } else { 144246d70077SConrad Meyer fmt_hdr = "%18s %12s %12s %12s\n"; 144346d70077SConrad Meyer fmt_entry = "%18s %12ju %12jdK %12ju\n"; 144446d70077SConrad Meyer } 144546d70077SConrad Meyer 144646d70077SConrad Meyer db_printf(fmt_hdr, "Type", "InUse", "MemUse", "Requests"); 144746d70077SConrad Meyer 144846d70077SConrad Meyer /* Select sort, largest size first. */ 144946d70077SConrad Meyer last_mtype = NULL; 145046d70077SConrad Meyer last_size = INT64_MAX; 145146d70077SConrad Meyer for (;;) { 145246d70077SConrad Meyer cur_mtype = NULL; 145346d70077SConrad Meyer cur_size = -1; 145446d70077SConrad Meyer ties = 0; 145546d70077SConrad Meyer 145646d70077SConrad Meyer for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) { 145746d70077SConrad Meyer /* 145846d70077SConrad Meyer * In the case of size ties, print out mtypes 145946d70077SConrad Meyer * in the order they are encountered. That is, 146046d70077SConrad Meyer * when we encounter the most recently output 146146d70077SConrad Meyer * mtype, we have already printed all preceding 146246d70077SConrad Meyer * ties, and we must print all following ties. 146346d70077SConrad Meyer */ 146446d70077SConrad Meyer if (mtp == last_mtype) { 146546d70077SConrad Meyer ties = 1; 146646d70077SConrad Meyer continue; 146746d70077SConrad Meyer } 1468bdcc2226SMateusz Guzik size = get_malloc_stats(&mtp->ks_mti, &allocs, 146946d70077SConrad Meyer &inuse); 147046d70077SConrad Meyer if (size > cur_size && size < last_size + ties) { 147146d70077SConrad Meyer cur_size = size; 147246d70077SConrad Meyer cur_mtype = mtp; 147346d70077SConrad Meyer } 147446d70077SConrad Meyer } 147546d70077SConrad Meyer if (cur_mtype == NULL) 147646d70077SConrad Meyer break; 147746d70077SConrad Meyer 1478bdcc2226SMateusz Guzik size = get_malloc_stats(&cur_mtype->ks_mti, &allocs, &inuse); 147946d70077SConrad Meyer db_printf(fmt_entry, cur_mtype->ks_shortdesc, inuse, 148046d70077SConrad Meyer howmany(size, 1024), allocs); 148146d70077SConrad Meyer 1482687c94aaSJohn Baldwin if (db_pager_quit) 1483687c94aaSJohn Baldwin break; 148446d70077SConrad Meyer 148546d70077SConrad Meyer last_mtype = cur_mtype; 148646d70077SConrad Meyer last_size = cur_size; 1487909ed16cSRobert Watson } 1488909ed16cSRobert Watson } 1489d7854da1SMatthew D Fleming 1490d7854da1SMatthew D Fleming #if MALLOC_DEBUG_MAXZONES > 1 1491d7854da1SMatthew D Fleming DB_SHOW_COMMAND(multizone_matches, db_show_multizone_matches) 1492d7854da1SMatthew D Fleming { 1493d7854da1SMatthew D Fleming struct malloc_type_internal *mtip; 1494d7854da1SMatthew D Fleming struct malloc_type *mtp; 1495d7854da1SMatthew D Fleming u_int subzone; 1496d7854da1SMatthew D Fleming 1497d7854da1SMatthew D Fleming if (!have_addr) { 1498d7854da1SMatthew D Fleming db_printf("Usage: show multizone_matches <malloc type/addr>\n"); 1499d7854da1SMatthew D Fleming return; 1500d7854da1SMatthew D Fleming } 1501d7854da1SMatthew D Fleming mtp = (void *)addr; 1502bdcc2226SMateusz Guzik if (mtp->ks_version != M_VERSION) { 1503bdcc2226SMateusz Guzik db_printf("Version %lx does not match expected %x\n", 1504bdcc2226SMateusz Guzik mtp->ks_version, M_VERSION); 1505d7854da1SMatthew D Fleming return; 1506d7854da1SMatthew D Fleming } 1507d7854da1SMatthew D Fleming 1508bdcc2226SMateusz Guzik mtip = &mtp->ks_mti; 1509d7854da1SMatthew D Fleming subzone = mtip->mti_zone; 1510d7854da1SMatthew D Fleming 1511d7854da1SMatthew D Fleming for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) { 1512bdcc2226SMateusz Guzik mtip = &mtp->ks_mti; 1513d7854da1SMatthew D Fleming if (mtip->mti_zone != subzone) 1514d7854da1SMatthew D Fleming continue; 1515d7854da1SMatthew D Fleming db_printf("%s\n", mtp->ks_shortdesc); 1516687c94aaSJohn Baldwin if (db_pager_quit) 1517687c94aaSJohn Baldwin break; 1518d7854da1SMatthew D Fleming } 1519d7854da1SMatthew D Fleming } 1520d7854da1SMatthew D Fleming #endif /* MALLOC_DEBUG_MAXZONES > 1 */ 1521d7854da1SMatthew D Fleming #endif /* DDB */ 1522909ed16cSRobert Watson 15235e914b96SJeff Roberson #ifdef MALLOC_PROFILE 15245e914b96SJeff Roberson 15255e914b96SJeff Roberson static int 15265e914b96SJeff Roberson sysctl_kern_mprof(SYSCTL_HANDLER_ARGS) 15275e914b96SJeff Roberson { 152863a7e0a3SRobert Watson struct sbuf sbuf; 15295e914b96SJeff Roberson uint64_t count; 15305e914b96SJeff Roberson uint64_t waste; 15315e914b96SJeff Roberson uint64_t mem; 15325e914b96SJeff Roberson int error; 15335e914b96SJeff Roberson int rsize; 15345e914b96SJeff Roberson int size; 15355e914b96SJeff Roberson int i; 15365e914b96SJeff Roberson 15375e914b96SJeff Roberson waste = 0; 15385e914b96SJeff Roberson mem = 0; 15395e914b96SJeff Roberson 154000f0e671SMatthew D Fleming error = sysctl_wire_old_buffer(req, 0); 154100f0e671SMatthew D Fleming if (error != 0) 154200f0e671SMatthew D Fleming return (error); 15434e657159SMatthew D Fleming sbuf_new_for_sysctl(&sbuf, NULL, 128, req); 154463a7e0a3SRobert Watson sbuf_printf(&sbuf, 15455e914b96SJeff Roberson "\n Size Requests Real Size\n"); 15465e914b96SJeff Roberson for (i = 0; i < KMEM_ZSIZE; i++) { 15475e914b96SJeff Roberson size = i << KMEM_ZSHIFT; 15485e914b96SJeff Roberson rsize = kmemzones[kmemsize[i]].kz_size; 15495e914b96SJeff Roberson count = (long long unsigned)krequests[i]; 15505e914b96SJeff Roberson 155163a7e0a3SRobert Watson sbuf_printf(&sbuf, "%6d%28llu%11d\n", size, 155263a7e0a3SRobert Watson (unsigned long long)count, rsize); 15535e914b96SJeff Roberson 15545e914b96SJeff Roberson if ((rsize * count) > (size * count)) 15555e914b96SJeff Roberson waste += (rsize * count) - (size * count); 15565e914b96SJeff Roberson mem += (rsize * count); 15575e914b96SJeff Roberson } 155863a7e0a3SRobert Watson sbuf_printf(&sbuf, 15595e914b96SJeff Roberson "\nTotal memory used:\t%30llu\nTotal Memory wasted:\t%30llu\n", 15605e914b96SJeff Roberson (unsigned long long)mem, (unsigned long long)waste); 15614e657159SMatthew D Fleming error = sbuf_finish(&sbuf); 156263a7e0a3SRobert Watson sbuf_delete(&sbuf); 15635e914b96SJeff Roberson return (error); 15645e914b96SJeff Roberson } 15655e914b96SJeff Roberson 15667029da5cSPawel Biernacki SYSCTL_OID(_kern, OID_AUTO, mprof, 15677029da5cSPawel Biernacki CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_NEEDGIANT, NULL, 0, 15687029da5cSPawel Biernacki sysctl_kern_mprof, "A", 15697029da5cSPawel Biernacki "Malloc Profiling"); 15705e914b96SJeff Roberson #endif /* MALLOC_PROFILE */ 1571