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> 63*6d6a03d7SJeff 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> 82*6d6a03d7SJeff Roberson #include <vm/vm_phys.h> 83*6d6a03d7SJeff 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 * 1500ce3f16dSRobert Watson * XXX: The comment here used to read "These won't be powers of two for 1510ce3f16dSRobert Watson * long." It's possible that a significant amount of wasted memory could be 1520ce3f16dSRobert Watson * recovered by tuning the sizes of these buckets. 1530ce3f16dSRobert Watson */ 1548355f576SJeff Roberson struct { 1556f267175SJeff Roberson int kz_size; 1566f267175SJeff Roberson char *kz_name; 157d7854da1SMatthew D Fleming uma_zone_t kz_zone[MALLOC_DEBUG_MAXZONES]; 1586f267175SJeff Roberson } kmemzones[] = { 159d7854da1SMatthew D Fleming {16, "16", }, 160d7854da1SMatthew D Fleming {32, "32", }, 161d7854da1SMatthew D Fleming {64, "64", }, 162d7854da1SMatthew D Fleming {128, "128", }, 163d7854da1SMatthew D Fleming {256, "256", }, 164d7854da1SMatthew D Fleming {512, "512", }, 165d7854da1SMatthew D Fleming {1024, "1024", }, 166d7854da1SMatthew D Fleming {2048, "2048", }, 167d7854da1SMatthew D Fleming {4096, "4096", }, 168d7854da1SMatthew D Fleming {8192, "8192", }, 169d7854da1SMatthew D Fleming {16384, "16384", }, 170d7854da1SMatthew D Fleming {32768, "32768", }, 171d7854da1SMatthew D Fleming {65536, "65536", }, 1728355f576SJeff Roberson {0, NULL}, 1738355f576SJeff Roberson }; 1748355f576SJeff Roberson 1750ce3f16dSRobert Watson /* 1760ce3f16dSRobert Watson * Zone to allocate malloc type descriptions from. For ABI reasons, memory 1770ce3f16dSRobert Watson * types are described by a data structure passed by the declaring code, but 1780ce3f16dSRobert Watson * the malloc(9) implementation has its own data structure describing the 1790ce3f16dSRobert Watson * type and statistics. This permits the malloc(9)-internal data structures 1800ce3f16dSRobert Watson * to be modified without breaking binary-compiled kernel modules that 1810ce3f16dSRobert Watson * declare malloc types. 1820ce3f16dSRobert Watson */ 18363a7e0a3SRobert Watson static uma_zone_t mt_zone; 1849afff6b1SMateusz Guzik static uma_zone_t mt_stats_zone; 18563a7e0a3SRobert Watson 186b89eaf4eSAlan Cox u_long vm_kmem_size; 187d801e824SAndriy Gapon SYSCTL_ULONG(_vm, OID_AUTO, kmem_size, CTLFLAG_RDTUN, &vm_kmem_size, 0, 18884344f9fSDag-Erling Smørgrav "Size of kernel memory"); 1895a34a9f0SJeff Roberson 1907001d850SXin LI static u_long kmem_zmax = KMEM_ZMAX; 1917001d850SXin LI SYSCTL_ULONG(_vm, OID_AUTO, kmem_zmax, CTLFLAG_RDTUN, &kmem_zmax, 0, 1927001d850SXin LI "Maximum allocation size that malloc(9) would use UMA as backend"); 1937001d850SXin LI 194b89eaf4eSAlan Cox static u_long vm_kmem_size_min; 195d801e824SAndriy Gapon SYSCTL_ULONG(_vm, OID_AUTO, kmem_size_min, CTLFLAG_RDTUN, &vm_kmem_size_min, 0, 1960e5179e4SStephane E. Potvin "Minimum size of kernel memory"); 1970e5179e4SStephane E. Potvin 198b89eaf4eSAlan Cox static u_long vm_kmem_size_max; 199d801e824SAndriy Gapon SYSCTL_ULONG(_vm, OID_AUTO, kmem_size_max, CTLFLAG_RDTUN, &vm_kmem_size_max, 0, 200479439b4SDag-Erling Smørgrav "Maximum size of kernel memory"); 201479439b4SDag-Erling Smørgrav 2024813ad54SHans Petter Selasky static u_int vm_kmem_size_scale; 203d801e824SAndriy Gapon SYSCTL_UINT(_vm, OID_AUTO, kmem_size_scale, CTLFLAG_RDTUN, &vm_kmem_size_scale, 0, 204479439b4SDag-Erling Smørgrav "Scale factor for kernel memory size"); 205479439b4SDag-Erling Smørgrav 2067814c80aSAndriy Gapon static int sysctl_kmem_map_size(SYSCTL_HANDLER_ARGS); 2077814c80aSAndriy Gapon SYSCTL_PROC(_vm, OID_AUTO, kmem_map_size, 2087814c80aSAndriy Gapon CTLFLAG_RD | CTLTYPE_ULONG | CTLFLAG_MPSAFE, NULL, 0, 2095df87b21SJeff Roberson sysctl_kmem_map_size, "LU", "Current kmem allocation size"); 2107814c80aSAndriy Gapon 21195bb9d38SAndriy Gapon static int sysctl_kmem_map_free(SYSCTL_HANDLER_ARGS); 21295bb9d38SAndriy Gapon SYSCTL_PROC(_vm, OID_AUTO, kmem_map_free, 21395bb9d38SAndriy Gapon CTLFLAG_RD | CTLTYPE_ULONG | CTLFLAG_MPSAFE, NULL, 0, 2145df87b21SJeff Roberson sysctl_kmem_map_free, "LU", "Free space in kmem"); 21595bb9d38SAndriy Gapon 2165a34a9f0SJeff Roberson /* 21799571dc3SJeff Roberson * The malloc_mtx protects the kmemstatistics linked list. 2185a34a9f0SJeff Roberson */ 2195a34a9f0SJeff Roberson struct mtx malloc_mtx; 22069ef67f9SJason Evans 2215e914b96SJeff Roberson #ifdef MALLOC_PROFILE 2225e914b96SJeff Roberson uint64_t krequests[KMEM_ZSIZE + 1]; 2236f267175SJeff Roberson 2245e914b96SJeff Roberson static int sysctl_kern_mprof(SYSCTL_HANDLER_ARGS); 2255e914b96SJeff Roberson #endif 2265e914b96SJeff Roberson 227cd814b26SRobert Watson static int sysctl_kern_malloc_stats(SYSCTL_HANDLER_ARGS); 228df8bae1dSRodney W. Grimes 2290ce3f16dSRobert Watson /* 2300ce3f16dSRobert Watson * time_uptime of the last malloc(9) failure (induced or real). 2310ce3f16dSRobert Watson */ 2321fb14a47SPoul-Henning Kamp static time_t t_malloc_fail; 2331fb14a47SPoul-Henning Kamp 234d7854da1SMatthew D Fleming #if defined(MALLOC_MAKE_FAILURES) || (MALLOC_DEBUG_MAXZONES > 1) 2356472ac3dSEd Schouten static SYSCTL_NODE(_debug, OID_AUTO, malloc, CTLFLAG_RD, 0, 236d7854da1SMatthew D Fleming "Kernel malloc debugging options"); 237d7854da1SMatthew D Fleming #endif 238d7854da1SMatthew D Fleming 239eae870cdSRobert Watson /* 2400ce3f16dSRobert Watson * malloc(9) fault injection -- cause malloc failures every (n) mallocs when 2410ce3f16dSRobert Watson * the caller specifies M_NOWAIT. If set to 0, no failures are caused. 242eae870cdSRobert Watson */ 2430ce3f16dSRobert Watson #ifdef MALLOC_MAKE_FAILURES 244eae870cdSRobert Watson static int malloc_failure_rate; 245eae870cdSRobert Watson static int malloc_nowait_count; 246eae870cdSRobert Watson static int malloc_failure_count; 247af3b2549SHans Petter Selasky SYSCTL_INT(_debug_malloc, OID_AUTO, failure_rate, CTLFLAG_RWTUN, 248eae870cdSRobert Watson &malloc_failure_rate, 0, "Every (n) mallocs with M_NOWAIT will fail"); 249eae870cdSRobert Watson SYSCTL_INT(_debug_malloc, OID_AUTO, failure_count, CTLFLAG_RD, 250eae870cdSRobert Watson &malloc_failure_count, 0, "Number of imposed M_NOWAIT malloc failures"); 251eae870cdSRobert Watson #endif 252eae870cdSRobert Watson 2537814c80aSAndriy Gapon static int 2547814c80aSAndriy Gapon sysctl_kmem_map_size(SYSCTL_HANDLER_ARGS) 2557814c80aSAndriy Gapon { 2567814c80aSAndriy Gapon u_long size; 2577814c80aSAndriy Gapon 2582e47807cSJeff Roberson size = uma_size(); 2597814c80aSAndriy Gapon return (sysctl_handle_long(oidp, &size, 0, req)); 2607814c80aSAndriy Gapon } 2617814c80aSAndriy Gapon 26295bb9d38SAndriy Gapon static int 26395bb9d38SAndriy Gapon sysctl_kmem_map_free(SYSCTL_HANDLER_ARGS) 26495bb9d38SAndriy Gapon { 2652e47807cSJeff Roberson u_long size, limit; 26695bb9d38SAndriy Gapon 2672e47807cSJeff Roberson /* The sysctl is unsigned, implement as a saturation value. */ 2682e47807cSJeff Roberson size = uma_size(); 2692e47807cSJeff Roberson limit = uma_limit(); 2702e47807cSJeff Roberson if (size > limit) 2712e47807cSJeff Roberson size = 0; 2722e47807cSJeff Roberson else 2732e47807cSJeff Roberson size = limit - size; 27495bb9d38SAndriy Gapon return (sysctl_handle_long(oidp, &size, 0, req)); 27595bb9d38SAndriy Gapon } 27695bb9d38SAndriy Gapon 277d7854da1SMatthew D Fleming /* 278d7854da1SMatthew D Fleming * malloc(9) uma zone separation -- sub-page buffer overruns in one 279d7854da1SMatthew D Fleming * malloc type will affect only a subset of other malloc types. 280d7854da1SMatthew D Fleming */ 281d7854da1SMatthew D Fleming #if MALLOC_DEBUG_MAXZONES > 1 282d7854da1SMatthew D Fleming static void 283d7854da1SMatthew D Fleming tunable_set_numzones(void) 284d7854da1SMatthew D Fleming { 285d7854da1SMatthew D Fleming 286d7854da1SMatthew D Fleming TUNABLE_INT_FETCH("debug.malloc.numzones", 287d7854da1SMatthew D Fleming &numzones); 288d7854da1SMatthew D Fleming 289d7854da1SMatthew D Fleming /* Sanity check the number of malloc uma zones. */ 290d7854da1SMatthew D Fleming if (numzones <= 0) 291d7854da1SMatthew D Fleming numzones = 1; 292d7854da1SMatthew D Fleming if (numzones > MALLOC_DEBUG_MAXZONES) 293d7854da1SMatthew D Fleming numzones = MALLOC_DEBUG_MAXZONES; 294d7854da1SMatthew D Fleming } 295d7854da1SMatthew D Fleming SYSINIT(numzones, SI_SUB_TUNABLES, SI_ORDER_ANY, tunable_set_numzones, NULL); 296af3b2549SHans Petter Selasky SYSCTL_INT(_debug_malloc, OID_AUTO, numzones, CTLFLAG_RDTUN | CTLFLAG_NOFETCH, 297d7854da1SMatthew D Fleming &numzones, 0, "Number of malloc uma subzones"); 298d7854da1SMatthew D Fleming 299d7854da1SMatthew D Fleming /* 300d7854da1SMatthew D Fleming * Any number that changes regularly is an okay choice for the 301d7854da1SMatthew D Fleming * offset. Build numbers are pretty good of you have them. 302d7854da1SMatthew D Fleming */ 303d7854da1SMatthew D Fleming static u_int zone_offset = __FreeBSD_version; 304d7854da1SMatthew D Fleming TUNABLE_INT("debug.malloc.zone_offset", &zone_offset); 305d7854da1SMatthew D Fleming SYSCTL_UINT(_debug_malloc, OID_AUTO, zone_offset, CTLFLAG_RDTUN, 306d7854da1SMatthew D Fleming &zone_offset, 0, "Separate malloc types by examining the " 307d7854da1SMatthew D Fleming "Nth character in the malloc type short description."); 308d7854da1SMatthew D Fleming 309c9e05ccdSMateusz Guzik static void 310c9e05ccdSMateusz Guzik mtp_set_subzone(struct malloc_type *mtp) 311d7854da1SMatthew D Fleming { 312c9e05ccdSMateusz Guzik struct malloc_type_internal *mtip; 313c9e05ccdSMateusz Guzik const char *desc; 314d7854da1SMatthew D Fleming size_t len; 315d7854da1SMatthew D Fleming u_int val; 316d7854da1SMatthew D Fleming 317c9e05ccdSMateusz Guzik mtip = mtp->ks_handle; 318c9e05ccdSMateusz Guzik desc = mtp->ks_shortdesc; 319d7854da1SMatthew D Fleming if (desc == NULL || (len = strlen(desc)) == 0) 320c9e05ccdSMateusz Guzik val = 0; 321c9e05ccdSMateusz Guzik else 322d7854da1SMatthew D Fleming val = desc[zone_offset % len]; 323c9e05ccdSMateusz Guzik mtip->mti_zone = (val % numzones); 324c9e05ccdSMateusz Guzik } 325c9e05ccdSMateusz Guzik 326c9e05ccdSMateusz Guzik static inline u_int 327c9e05ccdSMateusz Guzik mtp_get_subzone(struct malloc_type *mtp) 328c9e05ccdSMateusz Guzik { 329c9e05ccdSMateusz Guzik struct malloc_type_internal *mtip; 330c9e05ccdSMateusz Guzik 331c9e05ccdSMateusz Guzik mtip = mtp->ks_handle; 332c9e05ccdSMateusz Guzik 333c9e05ccdSMateusz Guzik KASSERT(mtip->mti_zone < numzones, 334c9e05ccdSMateusz Guzik ("mti_zone %u out of range %d", 335c9e05ccdSMateusz Guzik mtip->mti_zone, numzones)); 336c9e05ccdSMateusz Guzik return (mtip->mti_zone); 337d7854da1SMatthew D Fleming } 338d7854da1SMatthew D Fleming #elif MALLOC_DEBUG_MAXZONES == 0 339d7854da1SMatthew D Fleming #error "MALLOC_DEBUG_MAXZONES must be positive." 340d7854da1SMatthew D Fleming #else 341c9e05ccdSMateusz Guzik static void 342c9e05ccdSMateusz Guzik mtp_set_subzone(struct malloc_type *mtp) 343c9e05ccdSMateusz Guzik { 344c9e05ccdSMateusz Guzik struct malloc_type_internal *mtip; 345c9e05ccdSMateusz Guzik 346c9e05ccdSMateusz Guzik mtip = mtp->ks_handle; 347c9e05ccdSMateusz Guzik mtip->mti_zone = 0; 348c9e05ccdSMateusz Guzik } 349c9e05ccdSMateusz Guzik 350d7854da1SMatthew D Fleming static inline u_int 351c9e05ccdSMateusz Guzik mtp_get_subzone(struct malloc_type *mtp) 352d7854da1SMatthew D Fleming { 353d7854da1SMatthew D Fleming 354d7854da1SMatthew D Fleming return (0); 355d7854da1SMatthew D Fleming } 356d7854da1SMatthew D Fleming #endif /* MALLOC_DEBUG_MAXZONES > 1 */ 357d7854da1SMatthew D Fleming 3581fb14a47SPoul-Henning Kamp int 3591fb14a47SPoul-Henning Kamp malloc_last_fail(void) 3601fb14a47SPoul-Henning Kamp { 3611fb14a47SPoul-Henning Kamp 3621fb14a47SPoul-Henning Kamp return (time_uptime - t_malloc_fail); 3631fb14a47SPoul-Henning Kamp } 3641fb14a47SPoul-Henning Kamp 365df8bae1dSRodney W. Grimes /* 3660ce3f16dSRobert Watson * An allocation has succeeded -- update malloc type statistics for the 3670ce3f16dSRobert Watson * amount of bucket size. Occurs within a critical section so that the 3680ce3f16dSRobert Watson * thread isn't preempted and doesn't migrate while updating per-PCU 3690ce3f16dSRobert Watson * statistics. 3704362fadaSBrian Feldman */ 3714362fadaSBrian Feldman static void 37263a7e0a3SRobert Watson malloc_type_zone_allocated(struct malloc_type *mtp, unsigned long size, 3734362fadaSBrian Feldman int zindx) 3744362fadaSBrian Feldman { 37563a7e0a3SRobert Watson struct malloc_type_internal *mtip; 37663a7e0a3SRobert Watson struct malloc_type_stats *mtsp; 37763a7e0a3SRobert Watson 37863a7e0a3SRobert Watson critical_enter(); 37963a7e0a3SRobert Watson mtip = mtp->ks_handle; 3809afff6b1SMateusz Guzik mtsp = zpcpu_get(mtip->mti_stats); 38173864adbSPawel Jakub Dawidek if (size > 0) { 38263a7e0a3SRobert Watson mtsp->mts_memalloced += size; 38363a7e0a3SRobert Watson mtsp->mts_numallocs++; 38473864adbSPawel Jakub Dawidek } 3854362fadaSBrian Feldman if (zindx != -1) 38663a7e0a3SRobert Watson mtsp->mts_size |= 1 << zindx; 38791dd776cSJohn Birrell 38891dd776cSJohn Birrell #ifdef KDTRACE_HOOKS 3897cd79421SMateusz Guzik if (__predict_false(dtrace_malloc_enabled)) { 39091dd776cSJohn Birrell uint32_t probe_id = mtip->mti_probes[DTMALLOC_PROBE_MALLOC]; 39191dd776cSJohn Birrell if (probe_id != 0) 39291dd776cSJohn Birrell (dtrace_malloc_probe)(probe_id, 39391dd776cSJohn Birrell (uintptr_t) mtp, (uintptr_t) mtip, 39491dd776cSJohn Birrell (uintptr_t) mtsp, size, zindx); 39591dd776cSJohn Birrell } 39691dd776cSJohn Birrell #endif 39791dd776cSJohn Birrell 39863a7e0a3SRobert Watson critical_exit(); 3994362fadaSBrian Feldman } 4004362fadaSBrian Feldman 4014362fadaSBrian Feldman void 40263a7e0a3SRobert Watson malloc_type_allocated(struct malloc_type *mtp, unsigned long size) 4034362fadaSBrian Feldman { 40463a7e0a3SRobert Watson 40573864adbSPawel Jakub Dawidek if (size > 0) 40663a7e0a3SRobert Watson malloc_type_zone_allocated(mtp, size, -1); 4074362fadaSBrian Feldman } 4084362fadaSBrian Feldman 4094362fadaSBrian Feldman /* 4103805385eSRobert Watson * A free operation has occurred -- update malloc type statistics for the 4110ce3f16dSRobert Watson * amount of the bucket size. Occurs within a critical section so that the 4120ce3f16dSRobert Watson * thread isn't preempted and doesn't migrate while updating per-CPU 4130ce3f16dSRobert Watson * statistics. 4144362fadaSBrian Feldman */ 4154362fadaSBrian Feldman void 41663a7e0a3SRobert Watson malloc_type_freed(struct malloc_type *mtp, unsigned long size) 4174362fadaSBrian Feldman { 41863a7e0a3SRobert Watson struct malloc_type_internal *mtip; 41963a7e0a3SRobert Watson struct malloc_type_stats *mtsp; 42063a7e0a3SRobert Watson 42163a7e0a3SRobert Watson critical_enter(); 42263a7e0a3SRobert Watson mtip = mtp->ks_handle; 4239afff6b1SMateusz Guzik mtsp = zpcpu_get(mtip->mti_stats); 42463a7e0a3SRobert Watson mtsp->mts_memfreed += size; 42563a7e0a3SRobert Watson mtsp->mts_numfrees++; 42691dd776cSJohn Birrell 42791dd776cSJohn Birrell #ifdef KDTRACE_HOOKS 4287cd79421SMateusz Guzik if (__predict_false(dtrace_malloc_enabled)) { 42991dd776cSJohn Birrell uint32_t probe_id = mtip->mti_probes[DTMALLOC_PROBE_FREE]; 43091dd776cSJohn Birrell if (probe_id != 0) 43191dd776cSJohn Birrell (dtrace_malloc_probe)(probe_id, 43291dd776cSJohn Birrell (uintptr_t) mtp, (uintptr_t) mtip, 43391dd776cSJohn Birrell (uintptr_t) mtsp, size, 0); 43491dd776cSJohn Birrell } 43591dd776cSJohn Birrell #endif 43691dd776cSJohn Birrell 43763a7e0a3SRobert Watson critical_exit(); 4384362fadaSBrian Feldman } 4394362fadaSBrian Feldman 4404362fadaSBrian Feldman /* 441f346986bSAlan Cox * contigmalloc: 442f346986bSAlan Cox * 443f346986bSAlan Cox * Allocate a block of physically contiguous memory. 444f346986bSAlan Cox * 445f346986bSAlan Cox * If M_NOWAIT is set, this routine will not block and return NULL if 446f346986bSAlan Cox * the allocation fails. 447f346986bSAlan Cox */ 448f346986bSAlan Cox void * 449f346986bSAlan Cox contigmalloc(unsigned long size, struct malloc_type *type, int flags, 450f346986bSAlan Cox vm_paddr_t low, vm_paddr_t high, unsigned long alignment, 451831ce4cbSJohn Baldwin vm_paddr_t boundary) 452f346986bSAlan Cox { 453f346986bSAlan Cox void *ret; 454f346986bSAlan Cox 45544d0efb2SAlan Cox ret = (void *)kmem_alloc_contig(size, flags, low, high, alignment, 45644d0efb2SAlan Cox boundary, VM_MEMATTR_DEFAULT); 457f346986bSAlan Cox if (ret != NULL) 458f346986bSAlan Cox malloc_type_allocated(type, round_page(size)); 459f346986bSAlan Cox return (ret); 460f346986bSAlan Cox } 461f346986bSAlan Cox 462ab3185d1SJeff Roberson void * 4639978bd99SMark Johnston contigmalloc_domainset(unsigned long size, struct malloc_type *type, 4649978bd99SMark Johnston struct domainset *ds, int flags, vm_paddr_t low, vm_paddr_t high, 465ab3185d1SJeff Roberson unsigned long alignment, vm_paddr_t boundary) 466ab3185d1SJeff Roberson { 467ab3185d1SJeff Roberson void *ret; 468ab3185d1SJeff Roberson 4699978bd99SMark Johnston ret = (void *)kmem_alloc_contig_domainset(ds, size, flags, low, high, 470ab3185d1SJeff Roberson alignment, boundary, VM_MEMATTR_DEFAULT); 471ab3185d1SJeff Roberson if (ret != NULL) 472ab3185d1SJeff Roberson malloc_type_allocated(type, round_page(size)); 473ab3185d1SJeff Roberson return (ret); 474ab3185d1SJeff Roberson } 475ab3185d1SJeff Roberson 476f346986bSAlan Cox /* 477f346986bSAlan Cox * contigfree: 478f346986bSAlan Cox * 479f346986bSAlan Cox * Free a block of memory allocated by contigmalloc. 480f346986bSAlan Cox * 481f346986bSAlan Cox * This routine may not block. 482f346986bSAlan Cox */ 483f346986bSAlan Cox void 484f346986bSAlan Cox contigfree(void *addr, unsigned long size, struct malloc_type *type) 485f346986bSAlan Cox { 486f346986bSAlan Cox 48749bfa624SAlan Cox kmem_free((vm_offset_t)addr, size); 488f346986bSAlan Cox malloc_type_freed(type, round_page(size)); 489f346986bSAlan Cox } 490f346986bSAlan Cox 491ab3185d1SJeff Roberson #ifdef MALLOC_DEBUG 492ab3185d1SJeff Roberson static int 4935a70796aSLi-Wen Hsu malloc_dbg(caddr_t *vap, size_t *sizep, struct malloc_type *mtp, 494ab3185d1SJeff Roberson int flags) 495df8bae1dSRodney W. Grimes { 496194a0abfSPoul-Henning Kamp #ifdef INVARIANTS 497ab3185d1SJeff Roberson int indx; 498ab3185d1SJeff Roberson 499bb1c7df8SRobert Watson KASSERT(mtp->ks_magic == M_MAGIC, ("malloc: bad malloc type magic")); 500d3c11994SPoul-Henning Kamp /* 50123198357SRuslan Ermilov * Check that exactly one of M_WAITOK or M_NOWAIT is specified. 502d3c11994SPoul-Henning Kamp */ 50323198357SRuslan Ermilov indx = flags & (M_WAITOK | M_NOWAIT); 504d3c11994SPoul-Henning Kamp if (indx != M_NOWAIT && indx != M_WAITOK) { 505d3c11994SPoul-Henning Kamp static struct timeval lasterr; 506d3c11994SPoul-Henning Kamp static int curerr, once; 507d3c11994SPoul-Henning Kamp if (once == 0 && ppsratecheck(&lasterr, &curerr, 1)) { 508d3c11994SPoul-Henning Kamp printf("Bad malloc flags: %x\n", indx); 5092d50560aSMarcel Moolenaar kdb_backtrace(); 510d3c11994SPoul-Henning Kamp flags |= M_WAITOK; 511d3c11994SPoul-Henning Kamp once++; 512d3c11994SPoul-Henning Kamp } 513d3c11994SPoul-Henning Kamp } 514194a0abfSPoul-Henning Kamp #endif 515eae870cdSRobert Watson #ifdef MALLOC_MAKE_FAILURES 516eae870cdSRobert Watson if ((flags & M_NOWAIT) && (malloc_failure_rate != 0)) { 517eae870cdSRobert Watson atomic_add_int(&malloc_nowait_count, 1); 518eae870cdSRobert Watson if ((malloc_nowait_count % malloc_failure_rate) == 0) { 519eae870cdSRobert Watson atomic_add_int(&malloc_failure_count, 1); 5203f6ee876SPoul-Henning Kamp t_malloc_fail = time_uptime; 521ab3185d1SJeff Roberson *vap = NULL; 522ab3185d1SJeff Roberson return (EJUSTRETURN); 523eae870cdSRobert Watson } 524eae870cdSRobert Watson } 525eae870cdSRobert Watson #endif 52606bf2a6aSMatt Macy if (flags & M_WAITOK) { 527b40ce416SJulian Elischer KASSERT(curthread->td_intr_nesting_level == 0, 528a163d034SWarner Losh ("malloc(M_WAITOK) in interrupt context")); 5295757b59fSGleb Smirnoff if (__predict_false(!THREAD_CAN_SLEEP())) { 530bac06038SGleb Smirnoff #ifdef EPOCH_TRACE 531bac06038SGleb Smirnoff epoch_trace_list(curthread); 532bac06038SGleb Smirnoff #endif 5335757b59fSGleb Smirnoff KASSERT(1, 5345757b59fSGleb Smirnoff ("malloc(M_WAITOK) with sleeping prohibited")); 5355757b59fSGleb Smirnoff } 53606bf2a6aSMatt Macy } 537d9e2e68dSMark Johnston KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 5381067a2baSJonathan T. Looney ("malloc: called with spinlock or critical section held")); 5391067a2baSJonathan T. Looney 540e4eb384bSBosko Milekic #ifdef DEBUG_MEMGUARD 541ab3185d1SJeff Roberson if (memguard_cmp_mtp(mtp, *sizep)) { 542ab3185d1SJeff Roberson *vap = memguard_alloc(*sizep, flags); 543ab3185d1SJeff Roberson if (*vap != NULL) 544ab3185d1SJeff Roberson return (EJUSTRETURN); 545e3813573SMatthew D Fleming /* This is unfortunate but should not be fatal. */ 546e3813573SMatthew D Fleming } 547e4eb384bSBosko Milekic #endif 548e4eb384bSBosko Milekic 549847a2a17SPawel Jakub Dawidek #ifdef DEBUG_REDZONE 550ab3185d1SJeff Roberson *sizep = redzone_size_ntor(*sizep); 551ab3185d1SJeff Roberson #endif 552ab3185d1SJeff Roberson 553ab3185d1SJeff Roberson return (0); 554ab3185d1SJeff Roberson } 555ab3185d1SJeff Roberson #endif 556ab3185d1SJeff Roberson 557ab3185d1SJeff Roberson /* 558*6d6a03d7SJeff Roberson * Handle large allocations and frees by using kmem_malloc directly. 559*6d6a03d7SJeff Roberson */ 560*6d6a03d7SJeff Roberson static inline bool 561*6d6a03d7SJeff Roberson malloc_large_slab(uma_slab_t slab) 562*6d6a03d7SJeff Roberson { 563*6d6a03d7SJeff Roberson uintptr_t va; 564*6d6a03d7SJeff Roberson 565*6d6a03d7SJeff Roberson va = (uintptr_t)slab; 566*6d6a03d7SJeff Roberson return ((va & 1) != 0); 567*6d6a03d7SJeff Roberson } 568*6d6a03d7SJeff Roberson 569*6d6a03d7SJeff Roberson static inline size_t 570*6d6a03d7SJeff Roberson malloc_large_size(uma_slab_t slab) 571*6d6a03d7SJeff Roberson { 572*6d6a03d7SJeff Roberson uintptr_t va; 573*6d6a03d7SJeff Roberson 574*6d6a03d7SJeff Roberson va = (uintptr_t)slab; 575*6d6a03d7SJeff Roberson return (va >> 1); 576*6d6a03d7SJeff Roberson } 577*6d6a03d7SJeff Roberson 578*6d6a03d7SJeff Roberson static caddr_t 579*6d6a03d7SJeff Roberson malloc_large(size_t *size, struct domainset *policy, int flags) 580*6d6a03d7SJeff Roberson { 581*6d6a03d7SJeff Roberson vm_offset_t va; 582*6d6a03d7SJeff Roberson size_t sz; 583*6d6a03d7SJeff Roberson 584*6d6a03d7SJeff Roberson sz = roundup(*size, PAGE_SIZE); 585*6d6a03d7SJeff Roberson va = kmem_malloc_domainset(policy, sz, flags); 586*6d6a03d7SJeff Roberson if (va != 0) { 587*6d6a03d7SJeff Roberson /* The low bit is unused for slab pointers. */ 588*6d6a03d7SJeff Roberson vsetzoneslab(va, NULL, (void *)((sz << 1) | 1)); 589*6d6a03d7SJeff Roberson uma_total_inc(sz); 590*6d6a03d7SJeff Roberson *size = sz; 591*6d6a03d7SJeff Roberson } 592*6d6a03d7SJeff Roberson return ((caddr_t)va); 593*6d6a03d7SJeff Roberson } 594*6d6a03d7SJeff Roberson 595*6d6a03d7SJeff Roberson static void 596*6d6a03d7SJeff Roberson free_large(void *addr, size_t size) 597*6d6a03d7SJeff Roberson { 598*6d6a03d7SJeff Roberson 599*6d6a03d7SJeff Roberson kmem_free((vm_offset_t)addr, size); 600*6d6a03d7SJeff Roberson uma_total_dec(size); 601*6d6a03d7SJeff Roberson } 602*6d6a03d7SJeff Roberson 603*6d6a03d7SJeff Roberson /* 604ab3185d1SJeff Roberson * malloc: 605ab3185d1SJeff Roberson * 606ab3185d1SJeff Roberson * Allocate a block of memory. 607ab3185d1SJeff Roberson * 608ab3185d1SJeff Roberson * If M_NOWAIT is set, this routine will not block and return NULL if 609ab3185d1SJeff Roberson * the allocation fails. 610ab3185d1SJeff Roberson */ 611ab3185d1SJeff Roberson void * 61234c538c3SMateusz Guzik (malloc)(size_t size, struct malloc_type *mtp, int flags) 613ab3185d1SJeff Roberson { 614ab3185d1SJeff Roberson int indx; 615ab3185d1SJeff Roberson caddr_t va; 616ab3185d1SJeff Roberson uma_zone_t zone; 617ab3185d1SJeff Roberson #if defined(DEBUG_REDZONE) 618ab3185d1SJeff Roberson unsigned long osize = size; 619ab3185d1SJeff Roberson #endif 620ab3185d1SJeff Roberson 621ab3185d1SJeff Roberson #ifdef MALLOC_DEBUG 6225072a5f4SMatt Macy va = NULL; 623ab3185d1SJeff Roberson if (malloc_dbg(&va, &size, mtp, flags) != 0) 624ab3185d1SJeff Roberson return (va); 625847a2a17SPawel Jakub Dawidek #endif 626847a2a17SPawel Jakub Dawidek 6270766f278SJonathan T. Looney if (size <= kmem_zmax && (flags & M_EXEC) == 0) { 6286f267175SJeff Roberson if (size & KMEM_ZMASK) 6296f267175SJeff Roberson size = (size & ~KMEM_ZMASK) + KMEM_ZBASE; 6306f267175SJeff Roberson indx = kmemsize[size >> KMEM_ZSHIFT]; 631c9e05ccdSMateusz Guzik zone = kmemzones[indx].kz_zone[mtp_get_subzone(mtp)]; 6326f267175SJeff Roberson #ifdef MALLOC_PROFILE 6336f267175SJeff Roberson krequests[size >> KMEM_ZSHIFT]++; 6346f267175SJeff Roberson #endif 6358355f576SJeff Roberson va = uma_zalloc(zone, flags); 6364362fadaSBrian Feldman if (va != NULL) 637e20a199fSJeff Roberson size = zone->uz_size; 63863a7e0a3SRobert Watson malloc_type_zone_allocated(mtp, va == NULL ? 0 : size, indx); 6398355f576SJeff Roberson } else { 640*6d6a03d7SJeff Roberson va = malloc_large(&size, DOMAINSET_RR(), flags); 64163a7e0a3SRobert Watson malloc_type_allocated(mtp, va == NULL ? 0 : size); 642df8bae1dSRodney W. Grimes } 6431282e9acSPoul-Henning Kamp if (flags & M_WAITOK) 644a163d034SWarner Losh KASSERT(va != NULL, ("malloc(M_WAITOK) returned NULL")); 6451282e9acSPoul-Henning Kamp else if (va == NULL) 6461fb14a47SPoul-Henning Kamp t_malloc_fail = time_uptime; 647ab3185d1SJeff Roberson #ifdef DEBUG_REDZONE 648ab3185d1SJeff Roberson if (va != NULL) 649ab3185d1SJeff Roberson va = redzone_setup(va, osize); 6504db4f5c8SPoul-Henning Kamp #endif 651ab3185d1SJeff Roberson return ((void *) va); 652ab3185d1SJeff Roberson } 653ab3185d1SJeff Roberson 6549978bd99SMark Johnston static void * 655*6d6a03d7SJeff Roberson malloc_domain(size_t size, int *indxp, struct malloc_type *mtp, int domain, 656*6d6a03d7SJeff Roberson int flags) 657ab3185d1SJeff Roberson { 658ab3185d1SJeff Roberson int indx; 659ab3185d1SJeff Roberson caddr_t va; 660ab3185d1SJeff Roberson uma_zone_t zone; 661ab3185d1SJeff Roberson 662*6d6a03d7SJeff Roberson KASSERT(size <= kmem_zmax && (flags & M_EXEC) == 0, 663*6d6a03d7SJeff Roberson ("malloc_domain: Called with bad flag / size combination.")); 664ab3185d1SJeff Roberson if (size & KMEM_ZMASK) 665ab3185d1SJeff Roberson size = (size & ~KMEM_ZMASK) + KMEM_ZBASE; 666ab3185d1SJeff Roberson indx = kmemsize[size >> KMEM_ZSHIFT]; 667c9e05ccdSMateusz Guzik zone = kmemzones[indx].kz_zone[mtp_get_subzone(mtp)]; 668ab3185d1SJeff Roberson #ifdef MALLOC_PROFILE 669ab3185d1SJeff Roberson krequests[size >> KMEM_ZSHIFT]++; 670ab3185d1SJeff Roberson #endif 671ab3185d1SJeff Roberson va = uma_zalloc_domain(zone, NULL, domain, flags); 672ab3185d1SJeff Roberson if (va != NULL) 673ab3185d1SJeff Roberson size = zone->uz_size; 674*6d6a03d7SJeff Roberson *indxp = indx; 675*6d6a03d7SJeff Roberson 676df8bae1dSRodney W. Grimes return ((void *) va); 677df8bae1dSRodney W. Grimes } 678df8bae1dSRodney W. Grimes 679fd91e076SKristof Provost void * 6809978bd99SMark Johnston malloc_domainset(size_t size, struct malloc_type *mtp, struct domainset *ds, 6819978bd99SMark Johnston int flags) 6829978bd99SMark Johnston { 6839978bd99SMark Johnston struct vm_domainset_iter di; 684*6d6a03d7SJeff Roberson caddr_t ret; 6859978bd99SMark Johnston int domain; 686*6d6a03d7SJeff Roberson int indx; 6879978bd99SMark Johnston 688*6d6a03d7SJeff Roberson #if defined(DEBUG_REDZONE) 689*6d6a03d7SJeff Roberson unsigned long osize = size; 690*6d6a03d7SJeff Roberson #endif 691*6d6a03d7SJeff Roberson #ifdef MALLOC_DEBUG 692*6d6a03d7SJeff Roberson ret= NULL; 693*6d6a03d7SJeff Roberson if (malloc_dbg(&ret, &size, mtp, flags) != 0) 694*6d6a03d7SJeff Roberson return (ret); 695*6d6a03d7SJeff Roberson #endif 696*6d6a03d7SJeff Roberson if (size <= kmem_zmax && (flags & M_EXEC) == 0) { 6979978bd99SMark Johnston vm_domainset_iter_policy_init(&di, ds, &domain, &flags); 6989978bd99SMark Johnston do { 699*6d6a03d7SJeff Roberson ret = malloc_domain(size, &indx, mtp, domain, flags); 700*6d6a03d7SJeff Roberson } while (ret == NULL && 701*6d6a03d7SJeff Roberson vm_domainset_iter_policy(&di, &domain) == 0); 702*6d6a03d7SJeff Roberson malloc_type_zone_allocated(mtp, ret == NULL ? 0 : size, indx); 703*6d6a03d7SJeff Roberson } else { 704*6d6a03d7SJeff Roberson /* Policy is handled by kmem. */ 705*6d6a03d7SJeff Roberson ret = malloc_large(&size, ds, flags); 706*6d6a03d7SJeff Roberson malloc_type_allocated(mtp, ret == NULL ? 0 : size); 707*6d6a03d7SJeff Roberson } 7089978bd99SMark Johnston 709*6d6a03d7SJeff Roberson if (flags & M_WAITOK) 710*6d6a03d7SJeff Roberson KASSERT(ret != NULL, ("malloc(M_WAITOK) returned NULL")); 711*6d6a03d7SJeff Roberson else if (ret == NULL) 712*6d6a03d7SJeff Roberson t_malloc_fail = time_uptime; 713*6d6a03d7SJeff Roberson #ifdef DEBUG_REDZONE 714*6d6a03d7SJeff Roberson if (ret != NULL) 715*6d6a03d7SJeff Roberson ret = redzone_setup(ret, osize); 716*6d6a03d7SJeff Roberson #endif 7179978bd99SMark Johnston return (ret); 7189978bd99SMark Johnston } 7199978bd99SMark Johnston 7209978bd99SMark Johnston void * 721fd91e076SKristof Provost mallocarray(size_t nmemb, size_t size, struct malloc_type *type, int flags) 722fd91e076SKristof Provost { 723fd91e076SKristof Provost 724c02fc960SConrad Meyer if (WOULD_OVERFLOW(nmemb, size)) 725c02fc960SConrad Meyer panic("mallocarray: %zu * %zu overflowed", nmemb, size); 726fd91e076SKristof Provost 727fd91e076SKristof Provost return (malloc(size * nmemb, type, flags)); 728fd91e076SKristof Provost } 729fd91e076SKristof Provost 730ab3185d1SJeff Roberson #ifdef INVARIANTS 731ab3185d1SJeff Roberson static void 732ab3185d1SJeff Roberson free_save_type(void *addr, struct malloc_type *mtp, u_long size) 733ab3185d1SJeff Roberson { 734ab3185d1SJeff Roberson struct malloc_type **mtpp = addr; 735ab3185d1SJeff Roberson 736ab3185d1SJeff Roberson /* 737ab3185d1SJeff Roberson * Cache a pointer to the malloc_type that most recently freed 738ab3185d1SJeff Roberson * this memory here. This way we know who is most likely to 739ab3185d1SJeff Roberson * have stepped on it later. 740ab3185d1SJeff Roberson * 741ab3185d1SJeff Roberson * This code assumes that size is a multiple of 8 bytes for 742ab3185d1SJeff Roberson * 64 bit machines 743ab3185d1SJeff Roberson */ 744ab3185d1SJeff Roberson mtpp = (struct malloc_type **) ((unsigned long)mtpp & ~UMA_ALIGN_PTR); 745ab3185d1SJeff Roberson mtpp += (size - sizeof(struct malloc_type *)) / 746ab3185d1SJeff Roberson sizeof(struct malloc_type *); 747ab3185d1SJeff Roberson *mtpp = mtp; 748ab3185d1SJeff Roberson } 749ab3185d1SJeff Roberson #endif 750ab3185d1SJeff Roberson 751ab3185d1SJeff Roberson #ifdef MALLOC_DEBUG 752ab3185d1SJeff Roberson static int 753ab3185d1SJeff Roberson free_dbg(void **addrp, struct malloc_type *mtp) 754ab3185d1SJeff Roberson { 755ab3185d1SJeff Roberson void *addr; 756ab3185d1SJeff Roberson 757ab3185d1SJeff Roberson addr = *addrp; 758ab3185d1SJeff Roberson KASSERT(mtp->ks_magic == M_MAGIC, ("free: bad malloc type magic")); 759ab3185d1SJeff Roberson KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 760ab3185d1SJeff Roberson ("free: called with spinlock or critical section held")); 761ab3185d1SJeff Roberson 762ab3185d1SJeff Roberson /* free(NULL, ...) does nothing */ 763ab3185d1SJeff Roberson if (addr == NULL) 764ab3185d1SJeff Roberson return (EJUSTRETURN); 765ab3185d1SJeff Roberson 766ab3185d1SJeff Roberson #ifdef DEBUG_MEMGUARD 767ab3185d1SJeff Roberson if (is_memguard_addr(addr)) { 768ab3185d1SJeff Roberson memguard_free(addr); 769ab3185d1SJeff Roberson return (EJUSTRETURN); 770ab3185d1SJeff Roberson } 771ab3185d1SJeff Roberson #endif 772ab3185d1SJeff Roberson 773ab3185d1SJeff Roberson #ifdef DEBUG_REDZONE 774ab3185d1SJeff Roberson redzone_check(addr); 775ab3185d1SJeff Roberson *addrp = redzone_addr_ntor(addr); 776ab3185d1SJeff Roberson #endif 777ab3185d1SJeff Roberson 778ab3185d1SJeff Roberson return (0); 779ab3185d1SJeff Roberson } 780ab3185d1SJeff Roberson #endif 781ab3185d1SJeff Roberson 782fd91e076SKristof Provost /* 7831c7c3c6aSMatthew Dillon * free: 7841c7c3c6aSMatthew Dillon * 785df8bae1dSRodney W. Grimes * Free a block of memory allocated by malloc. 7861c7c3c6aSMatthew Dillon * 7871c7c3c6aSMatthew Dillon * This routine may not block. 788df8bae1dSRodney W. Grimes */ 789df8bae1dSRodney W. Grimes void 79063a7e0a3SRobert Watson free(void *addr, struct malloc_type *mtp) 791df8bae1dSRodney W. Grimes { 792584061b4SJeff Roberson uma_zone_t zone; 79399571dc3SJeff Roberson uma_slab_t slab; 79499571dc3SJeff Roberson u_long size; 795254c6cb3SPoul-Henning Kamp 796ab3185d1SJeff Roberson #ifdef MALLOC_DEBUG 797ab3185d1SJeff Roberson if (free_dbg(&addr, mtp) != 0) 798ab3185d1SJeff Roberson return; 799ab3185d1SJeff Roberson #endif 80044a8ff31SArchie Cobbs /* free(NULL, ...) does nothing */ 80144a8ff31SArchie Cobbs if (addr == NULL) 80244a8ff31SArchie Cobbs return; 80344a8ff31SArchie Cobbs 804584061b4SJeff Roberson vtozoneslab((vm_offset_t)addr & (~UMA_SLAB_MASK), &zone, &slab); 8058355f576SJeff Roberson if (slab == NULL) 8066f267175SJeff Roberson panic("free: address %p(%p) has not been allocated.\n", 80799571dc3SJeff Roberson addr, (void *)((u_long)addr & (~UMA_SLAB_MASK))); 80899571dc3SJeff Roberson 809*6d6a03d7SJeff Roberson if (__predict_true(!malloc_large_slab(slab))) { 810584061b4SJeff Roberson size = zone->uz_size; 8118f70816cSJeff Roberson #ifdef INVARIANTS 812ab3185d1SJeff Roberson free_save_type(addr, mtp, size); 8138f70816cSJeff Roberson #endif 814584061b4SJeff Roberson uma_zfree_arg(zone, addr, slab); 81514bf02f8SJohn Dyson } else { 816*6d6a03d7SJeff Roberson size = malloc_large_size(slab); 817*6d6a03d7SJeff Roberson free_large(addr, size); 81814bf02f8SJohn Dyson } 81963a7e0a3SRobert Watson malloc_type_freed(mtp, size); 820df8bae1dSRodney W. Grimes } 821df8bae1dSRodney W. Grimes 822ab3185d1SJeff Roberson void 823ab3185d1SJeff Roberson free_domain(void *addr, struct malloc_type *mtp) 824ab3185d1SJeff Roberson { 825584061b4SJeff Roberson uma_zone_t zone; 826ab3185d1SJeff Roberson uma_slab_t slab; 827ab3185d1SJeff Roberson u_long size; 828ab3185d1SJeff Roberson 829ab3185d1SJeff Roberson #ifdef MALLOC_DEBUG 830ab3185d1SJeff Roberson if (free_dbg(&addr, mtp) != 0) 831ab3185d1SJeff Roberson return; 832ab3185d1SJeff Roberson #endif 833ab3185d1SJeff Roberson 834ab3185d1SJeff Roberson /* free(NULL, ...) does nothing */ 835ab3185d1SJeff Roberson if (addr == NULL) 836ab3185d1SJeff Roberson return; 837ab3185d1SJeff Roberson 838584061b4SJeff Roberson vtozoneslab((vm_offset_t)addr & (~UMA_SLAB_MASK), &zone, &slab); 839ab3185d1SJeff Roberson if (slab == NULL) 840ab3185d1SJeff Roberson panic("free_domain: address %p(%p) has not been allocated.\n", 841ab3185d1SJeff Roberson addr, (void *)((u_long)addr & (~UMA_SLAB_MASK))); 842ab3185d1SJeff Roberson 843*6d6a03d7SJeff Roberson if (__predict_true(!malloc_large_slab(slab))) { 844584061b4SJeff Roberson size = zone->uz_size; 845ab3185d1SJeff Roberson #ifdef INVARIANTS 846ab3185d1SJeff Roberson free_save_type(addr, mtp, size); 847ab3185d1SJeff Roberson #endif 848584061b4SJeff Roberson uma_zfree_domain(zone, addr, slab); 849ab3185d1SJeff Roberson } else { 850*6d6a03d7SJeff Roberson size = malloc_large_size(slab); 851*6d6a03d7SJeff Roberson free_large(addr, size); 852ab3185d1SJeff Roberson } 853ab3185d1SJeff Roberson malloc_type_freed(mtp, size); 854ab3185d1SJeff Roberson } 855ab3185d1SJeff Roberson 856df8bae1dSRodney W. Grimes /* 85744a8ff31SArchie Cobbs * realloc: change the size of a memory block 85844a8ff31SArchie Cobbs */ 85944a8ff31SArchie Cobbs void * 860bd555da9SConrad Meyer realloc(void *addr, size_t size, struct malloc_type *mtp, int flags) 86144a8ff31SArchie Cobbs { 862584061b4SJeff Roberson uma_zone_t zone; 8638355f576SJeff Roberson uma_slab_t slab; 86444a8ff31SArchie Cobbs unsigned long alloc; 86544a8ff31SArchie Cobbs void *newaddr; 86644a8ff31SArchie Cobbs 867bb1c7df8SRobert Watson KASSERT(mtp->ks_magic == M_MAGIC, 868bb1c7df8SRobert Watson ("realloc: bad malloc type magic")); 869d9e2e68dSMark Johnston KASSERT(curthread->td_critnest == 0 || SCHEDULER_STOPPED(), 8701067a2baSJonathan T. Looney ("realloc: called with spinlock or critical section held")); 8711067a2baSJonathan T. Looney 87244a8ff31SArchie Cobbs /* realloc(NULL, ...) is equivalent to malloc(...) */ 87344a8ff31SArchie Cobbs if (addr == NULL) 87463a7e0a3SRobert Watson return (malloc(size, mtp, flags)); 87563a7e0a3SRobert Watson 87663a7e0a3SRobert Watson /* 87763a7e0a3SRobert Watson * XXX: Should report free of old memory and alloc of new memory to 87863a7e0a3SRobert Watson * per-CPU stats. 87963a7e0a3SRobert Watson */ 88044a8ff31SArchie Cobbs 881e4eb384bSBosko Milekic #ifdef DEBUG_MEMGUARD 8826d3ed393SMatthew D Fleming if (is_memguard_addr(addr)) 8836d3ed393SMatthew D Fleming return (memguard_realloc(addr, size, mtp, flags)); 884e4eb384bSBosko Milekic #endif 885e4eb384bSBosko Milekic 886847a2a17SPawel Jakub Dawidek #ifdef DEBUG_REDZONE 887847a2a17SPawel Jakub Dawidek slab = NULL; 888b476ae7fSJeff Roberson zone = NULL; 889847a2a17SPawel Jakub Dawidek alloc = redzone_get_size(addr); 890847a2a17SPawel Jakub Dawidek #else 891584061b4SJeff Roberson vtozoneslab((vm_offset_t)addr & (~UMA_SLAB_MASK), &zone, &slab); 8928355f576SJeff Roberson 89344a8ff31SArchie Cobbs /* Sanity check */ 8948355f576SJeff Roberson KASSERT(slab != NULL, 89544a8ff31SArchie Cobbs ("realloc: address %p out of range", (void *)addr)); 89644a8ff31SArchie Cobbs 89744a8ff31SArchie Cobbs /* Get the size of the original block */ 898*6d6a03d7SJeff Roberson if (!malloc_large_slab(slab)) 899584061b4SJeff Roberson alloc = zone->uz_size; 9008355f576SJeff Roberson else 901*6d6a03d7SJeff Roberson alloc = malloc_large_size(slab); 90244a8ff31SArchie Cobbs 90344a8ff31SArchie Cobbs /* Reuse the original block if appropriate */ 90444a8ff31SArchie Cobbs if (size <= alloc 90544a8ff31SArchie Cobbs && (size > (alloc >> REALLOC_FRACTION) || alloc == MINALLOCSIZE)) 90644a8ff31SArchie Cobbs return (addr); 907847a2a17SPawel Jakub Dawidek #endif /* !DEBUG_REDZONE */ 90844a8ff31SArchie Cobbs 90944a8ff31SArchie Cobbs /* Allocate a new, bigger (or smaller) block */ 91063a7e0a3SRobert Watson if ((newaddr = malloc(size, mtp, flags)) == NULL) 91144a8ff31SArchie Cobbs return (NULL); 91244a8ff31SArchie Cobbs 91344a8ff31SArchie Cobbs /* Copy over original contents */ 91444a8ff31SArchie Cobbs bcopy(addr, newaddr, min(size, alloc)); 91563a7e0a3SRobert Watson free(addr, mtp); 91644a8ff31SArchie Cobbs return (newaddr); 91744a8ff31SArchie Cobbs } 91844a8ff31SArchie Cobbs 91944a8ff31SArchie Cobbs /* 92044a8ff31SArchie Cobbs * reallocf: same as realloc() but free memory on failure. 92144a8ff31SArchie Cobbs */ 92244a8ff31SArchie Cobbs void * 923bd555da9SConrad Meyer reallocf(void *addr, size_t size, struct malloc_type *mtp, int flags) 92444a8ff31SArchie Cobbs { 92544a8ff31SArchie Cobbs void *mem; 92644a8ff31SArchie Cobbs 92763a7e0a3SRobert Watson if ((mem = realloc(addr, size, mtp, flags)) == NULL) 92863a7e0a3SRobert Watson free(addr, mtp); 92944a8ff31SArchie Cobbs return (mem); 93044a8ff31SArchie Cobbs } 93144a8ff31SArchie Cobbs 932f9d498adSDimitry Andric #ifndef __sparc64__ 933c70af487SAlan Cox CTASSERT(VM_KMEM_SIZE_SCALE >= 1); 934f9d498adSDimitry Andric #endif 935c70af487SAlan Cox 9365df87b21SJeff Roberson /* 937c70af487SAlan Cox * Initialize the kernel memory (kmem) arena. 9385df87b21SJeff Roberson */ 9395df87b21SJeff Roberson void 9405df87b21SJeff Roberson kmeminit(void) 9415df87b21SJeff Roberson { 942af3b2549SHans Petter Selasky u_long mem_size; 943af3b2549SHans Petter Selasky u_long tmp; 94469ef67f9SJason Evans 945af3b2549SHans Petter Selasky #ifdef VM_KMEM_SIZE 946af3b2549SHans Petter Selasky if (vm_kmem_size == 0) 947af3b2549SHans Petter Selasky vm_kmem_size = VM_KMEM_SIZE; 948af3b2549SHans Petter Selasky #endif 949af3b2549SHans Petter Selasky #ifdef VM_KMEM_SIZE_MIN 950af3b2549SHans Petter Selasky if (vm_kmem_size_min == 0) 951af3b2549SHans Petter Selasky vm_kmem_size_min = VM_KMEM_SIZE_MIN; 952af3b2549SHans Petter Selasky #endif 953af3b2549SHans Petter Selasky #ifdef VM_KMEM_SIZE_MAX 954af3b2549SHans Petter Selasky if (vm_kmem_size_max == 0) 955af3b2549SHans Petter Selasky vm_kmem_size_max = VM_KMEM_SIZE_MAX; 956af3b2549SHans Petter Selasky #endif 9578a58a9f6SJohn Dyson /* 958c70af487SAlan Cox * Calculate the amount of kernel virtual address (KVA) space that is 959c70af487SAlan Cox * preallocated to the kmem arena. In order to support a wide range 960c70af487SAlan Cox * of machines, it is a function of the physical memory size, 961c70af487SAlan Cox * specifically, 9628a58a9f6SJohn Dyson * 963c70af487SAlan Cox * min(max(physical memory size / VM_KMEM_SIZE_SCALE, 964c70af487SAlan Cox * VM_KMEM_SIZE_MIN), VM_KMEM_SIZE_MAX) 965c70af487SAlan Cox * 966c70af487SAlan Cox * Every architecture must define an integral value for 967c70af487SAlan Cox * VM_KMEM_SIZE_SCALE. However, the definitions of VM_KMEM_SIZE_MIN 968c70af487SAlan Cox * and VM_KMEM_SIZE_MAX, which represent respectively the floor and 969c70af487SAlan Cox * ceiling on this preallocation, are optional. Typically, 970c70af487SAlan Cox * VM_KMEM_SIZE_MAX is itself a function of the available KVA space on 971c70af487SAlan Cox * a given architecture. 9728a58a9f6SJohn Dyson */ 97344f1c916SBryan Drewery mem_size = vm_cnt.v_page_count; 9747c51714eSSean Bruno if (mem_size <= 32768) /* delphij XXX 128MB */ 9757c51714eSSean Bruno kmem_zmax = PAGE_SIZE; 9768a58a9f6SJohn Dyson 977c70af487SAlan Cox if (vm_kmem_size_scale < 1) 978c70af487SAlan Cox vm_kmem_size_scale = VM_KMEM_SIZE_SCALE; 979c70af487SAlan Cox 980af3b2549SHans Petter Selasky /* 981af3b2549SHans Petter Selasky * Check if we should use defaults for the "vm_kmem_size" 982af3b2549SHans Petter Selasky * variable: 983af3b2549SHans Petter Selasky */ 984af3b2549SHans Petter Selasky if (vm_kmem_size == 0) { 98528b740daSKonstantin Belousov vm_kmem_size = mem_size / vm_kmem_size_scale; 98628b740daSKonstantin Belousov vm_kmem_size = vm_kmem_size * PAGE_SIZE < vm_kmem_size ? 98728b740daSKonstantin Belousov vm_kmem_size_max : vm_kmem_size * PAGE_SIZE; 988c70af487SAlan Cox if (vm_kmem_size_min > 0 && vm_kmem_size < vm_kmem_size_min) 9890e5179e4SStephane E. Potvin vm_kmem_size = vm_kmem_size_min; 990479439b4SDag-Erling Smørgrav if (vm_kmem_size_max > 0 && vm_kmem_size >= vm_kmem_size_max) 991479439b4SDag-Erling Smørgrav vm_kmem_size = vm_kmem_size_max; 992af3b2549SHans Petter Selasky } 99328b740daSKonstantin Belousov if (vm_kmem_size == 0) 99428b740daSKonstantin Belousov panic("Tune VM_KMEM_SIZE_* for the platform"); 9958a58a9f6SJohn Dyson 99627b8623fSDavid Greenman /* 997af3b2549SHans Petter Selasky * The amount of KVA space that is preallocated to the 998c70af487SAlan Cox * kmem arena can be set statically at compile-time or manually 999c70af487SAlan Cox * through the kernel environment. However, it is still limited to 1000c70af487SAlan Cox * twice the physical memory size, which has been sufficient to handle 1001c70af487SAlan Cox * the most severe cases of external fragmentation in the kmem arena. 100227b8623fSDavid Greenman */ 1003c749c003SAlan Cox if (vm_kmem_size / 2 / PAGE_SIZE > mem_size) 1004c749c003SAlan Cox vm_kmem_size = 2 * mem_size * PAGE_SIZE; 10058a58a9f6SJohn Dyson 1006e137643eSOlivier Houchard vm_kmem_size = round_page(vm_kmem_size); 1007e3813573SMatthew D Fleming #ifdef DEBUG_MEMGUARD 1008f806cdcfSMatthew D Fleming tmp = memguard_fudge(vm_kmem_size, kernel_map); 1009e3813573SMatthew D Fleming #else 1010e3813573SMatthew D Fleming tmp = vm_kmem_size; 1011e3813573SMatthew D Fleming #endif 10122e47807cSJeff Roberson uma_set_limit(tmp); 10138355f576SJeff Roberson 1014e4eb384bSBosko Milekic #ifdef DEBUG_MEMGUARD 1015e4eb384bSBosko Milekic /* 1016e4eb384bSBosko Milekic * Initialize MemGuard if support compiled in. MemGuard is a 1017e4eb384bSBosko Milekic * replacement allocator used for detecting tamper-after-free 1018e4eb384bSBosko Milekic * scenarios as they occur. It is only used for debugging. 1019e4eb384bSBosko Milekic */ 10202e47807cSJeff Roberson memguard_init(kernel_arena); 1021e4eb384bSBosko Milekic #endif 10225df87b21SJeff Roberson } 10235df87b21SJeff Roberson 10245df87b21SJeff Roberson /* 10255df87b21SJeff Roberson * Initialize the kernel memory allocator 10265df87b21SJeff Roberson */ 10275df87b21SJeff Roberson /* ARGSUSED*/ 10285df87b21SJeff Roberson static void 10295df87b21SJeff Roberson mallocinit(void *dummy) 10305df87b21SJeff Roberson { 10315df87b21SJeff Roberson int i; 10325df87b21SJeff Roberson uint8_t indx; 10335df87b21SJeff Roberson 10345df87b21SJeff Roberson mtx_init(&malloc_mtx, "malloc", NULL, MTX_DEF); 10355df87b21SJeff Roberson 10365df87b21SJeff Roberson kmeminit(); 1037e4eb384bSBosko Milekic 10387001d850SXin LI if (kmem_zmax < PAGE_SIZE || kmem_zmax > KMEM_ZMAX) 10397001d850SXin LI kmem_zmax = KMEM_ZMAX; 10407001d850SXin LI 10419afff6b1SMateusz Guzik mt_stats_zone = uma_zcreate("mt_stats_zone", 10429afff6b1SMateusz Guzik sizeof(struct malloc_type_stats), NULL, NULL, NULL, NULL, 10439afff6b1SMateusz Guzik UMA_ALIGN_PTR, UMA_ZONE_PCPU); 104463a7e0a3SRobert Watson mt_zone = uma_zcreate("mt_zone", sizeof(struct malloc_type_internal), 104563a7e0a3SRobert Watson #ifdef INVARIANTS 104663a7e0a3SRobert Watson mtrash_ctor, mtrash_dtor, mtrash_init, mtrash_fini, 104763a7e0a3SRobert Watson #else 104863a7e0a3SRobert Watson NULL, NULL, NULL, NULL, 104963a7e0a3SRobert Watson #endif 105063a7e0a3SRobert Watson UMA_ALIGN_PTR, UMA_ZONE_MALLOC); 10516f267175SJeff Roberson for (i = 0, indx = 0; kmemzones[indx].kz_size != 0; indx++) { 10526f267175SJeff Roberson int size = kmemzones[indx].kz_size; 10536f267175SJeff Roberson char *name = kmemzones[indx].kz_name; 1054d7854da1SMatthew D Fleming int subzone; 10558355f576SJeff Roberson 1056d7854da1SMatthew D Fleming for (subzone = 0; subzone < numzones; subzone++) { 1057d7854da1SMatthew D Fleming kmemzones[indx].kz_zone[subzone] = 1058d7854da1SMatthew D Fleming uma_zcreate(name, size, 10598efc4effSJeff Roberson #ifdef INVARIANTS 10608f70816cSJeff Roberson mtrash_ctor, mtrash_dtor, mtrash_init, mtrash_fini, 10618efc4effSJeff Roberson #else 10628efc4effSJeff Roberson NULL, NULL, NULL, NULL, 10638efc4effSJeff Roberson #endif 10648efc4effSJeff Roberson UMA_ALIGN_PTR, UMA_ZONE_MALLOC); 1065d7854da1SMatthew D Fleming } 10668355f576SJeff Roberson for (;i <= size; i+= KMEM_ZBASE) 10676f267175SJeff Roberson kmemsize[i >> KMEM_ZSHIFT] = indx; 10688355f576SJeff Roberson 1069df8bae1dSRodney W. Grimes } 1070254c6cb3SPoul-Henning Kamp } 1071af3b2549SHans Petter Selasky SYSINIT(kmem, SI_SUB_KMEM, SI_ORDER_SECOND, mallocinit, NULL); 1072254c6cb3SPoul-Henning Kamp 1073db669378SPeter Wemm void 107487efd4d5SRobert Watson malloc_init(void *data) 1075254c6cb3SPoul-Henning Kamp { 107663a7e0a3SRobert Watson struct malloc_type_internal *mtip; 107763a7e0a3SRobert Watson struct malloc_type *mtp; 107863a7e0a3SRobert Watson 107944f1c916SBryan Drewery KASSERT(vm_cnt.v_page_count != 0, ("malloc_register before vm_init")); 108063a7e0a3SRobert Watson 108163a7e0a3SRobert Watson mtp = data; 1082f121baaaSBrian Somers if (mtp->ks_magic != M_MAGIC) 1083f121baaaSBrian Somers panic("malloc_init: bad malloc type magic"); 1084bb1c7df8SRobert Watson 108563a7e0a3SRobert Watson mtip = uma_zalloc(mt_zone, M_WAITOK | M_ZERO); 10869afff6b1SMateusz Guzik mtip->mti_stats = uma_zalloc_pcpu(mt_stats_zone, M_WAITOK | M_ZERO); 108763a7e0a3SRobert Watson mtp->ks_handle = mtip; 1088c9e05ccdSMateusz Guzik mtp_set_subzone(mtp); 1089254c6cb3SPoul-Henning Kamp 10906f267175SJeff Roberson mtx_lock(&malloc_mtx); 109163a7e0a3SRobert Watson mtp->ks_next = kmemstatistics; 109263a7e0a3SRobert Watson kmemstatistics = mtp; 1093cd814b26SRobert Watson kmemcount++; 10946f267175SJeff Roberson mtx_unlock(&malloc_mtx); 1095df8bae1dSRodney W. Grimes } 1096db669378SPeter Wemm 1097db669378SPeter Wemm void 109887efd4d5SRobert Watson malloc_uninit(void *data) 1099db669378SPeter Wemm { 110063a7e0a3SRobert Watson struct malloc_type_internal *mtip; 11012a143d5bSPawel Jakub Dawidek struct malloc_type_stats *mtsp; 110263a7e0a3SRobert Watson struct malloc_type *mtp, *temp; 110345d48bdaSPaul Saab uma_slab_t slab; 11042a143d5bSPawel Jakub Dawidek long temp_allocs, temp_bytes; 11052a143d5bSPawel Jakub Dawidek int i; 1106db669378SPeter Wemm 110763a7e0a3SRobert Watson mtp = data; 1108bb1c7df8SRobert Watson KASSERT(mtp->ks_magic == M_MAGIC, 1109bb1c7df8SRobert Watson ("malloc_uninit: bad malloc type magic")); 111063a7e0a3SRobert Watson KASSERT(mtp->ks_handle != NULL, ("malloc_deregister: cookie NULL")); 1111bb1c7df8SRobert Watson 11126f267175SJeff Roberson mtx_lock(&malloc_mtx); 111363a7e0a3SRobert Watson mtip = mtp->ks_handle; 111463a7e0a3SRobert Watson mtp->ks_handle = NULL; 111563a7e0a3SRobert Watson if (mtp != kmemstatistics) { 111663a7e0a3SRobert Watson for (temp = kmemstatistics; temp != NULL; 111763a7e0a3SRobert Watson temp = temp->ks_next) { 1118f121baaaSBrian Somers if (temp->ks_next == mtp) { 111963a7e0a3SRobert Watson temp->ks_next = mtp->ks_next; 1120f121baaaSBrian Somers break; 1121db669378SPeter Wemm } 1122f121baaaSBrian Somers } 1123f121baaaSBrian Somers KASSERT(temp, 1124f121baaaSBrian Somers ("malloc_uninit: type '%s' not found", mtp->ks_shortdesc)); 112563a7e0a3SRobert Watson } else 112663a7e0a3SRobert Watson kmemstatistics = mtp->ks_next; 1127cd814b26SRobert Watson kmemcount--; 11286f267175SJeff Roberson mtx_unlock(&malloc_mtx); 11292a143d5bSPawel Jakub Dawidek 11302a143d5bSPawel Jakub Dawidek /* 11312a143d5bSPawel Jakub Dawidek * Look for memory leaks. 11322a143d5bSPawel Jakub Dawidek */ 11332a143d5bSPawel Jakub Dawidek temp_allocs = temp_bytes = 0; 11349afff6b1SMateusz Guzik for (i = 0; i <= mp_maxid; i++) { 11359afff6b1SMateusz Guzik mtsp = zpcpu_get_cpu(mtip->mti_stats, i); 11362a143d5bSPawel Jakub Dawidek temp_allocs += mtsp->mts_numallocs; 11372a143d5bSPawel Jakub Dawidek temp_allocs -= mtsp->mts_numfrees; 11382a143d5bSPawel Jakub Dawidek temp_bytes += mtsp->mts_memalloced; 11392a143d5bSPawel Jakub Dawidek temp_bytes -= mtsp->mts_memfreed; 11402a143d5bSPawel Jakub Dawidek } 11412a143d5bSPawel Jakub Dawidek if (temp_allocs > 0 || temp_bytes > 0) { 11422a143d5bSPawel Jakub Dawidek printf("Warning: memory type %s leaked memory on destroy " 11432a143d5bSPawel Jakub Dawidek "(%ld allocations, %ld bytes leaked).\n", mtp->ks_shortdesc, 11442a143d5bSPawel Jakub Dawidek temp_allocs, temp_bytes); 11452a143d5bSPawel Jakub Dawidek } 11462a143d5bSPawel Jakub Dawidek 114745d48bdaSPaul Saab slab = vtoslab((vm_offset_t) mtip & (~UMA_SLAB_MASK)); 11489afff6b1SMateusz Guzik uma_zfree_pcpu(mt_stats_zone, mtip->mti_stats); 114945d48bdaSPaul Saab uma_zfree_arg(mt_zone, mtip, slab); 1150db669378SPeter Wemm } 11516f267175SJeff Roberson 1152d362c40dSPawel Jakub Dawidek struct malloc_type * 1153d362c40dSPawel Jakub Dawidek malloc_desc2type(const char *desc) 1154d362c40dSPawel Jakub Dawidek { 1155d362c40dSPawel Jakub Dawidek struct malloc_type *mtp; 1156d362c40dSPawel Jakub Dawidek 1157d362c40dSPawel Jakub Dawidek mtx_assert(&malloc_mtx, MA_OWNED); 1158d362c40dSPawel Jakub Dawidek for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) { 1159d362c40dSPawel Jakub Dawidek if (strcmp(mtp->ks_shortdesc, desc) == 0) 1160d362c40dSPawel Jakub Dawidek return (mtp); 1161d362c40dSPawel Jakub Dawidek } 1162d362c40dSPawel Jakub Dawidek return (NULL); 1163d362c40dSPawel Jakub Dawidek } 1164d362c40dSPawel Jakub Dawidek 11656f267175SJeff Roberson static int 1166cd814b26SRobert Watson sysctl_kern_malloc_stats(SYSCTL_HANDLER_ARGS) 1167cd814b26SRobert Watson { 1168cd814b26SRobert Watson struct malloc_type_stream_header mtsh; 1169cd814b26SRobert Watson struct malloc_type_internal *mtip; 11709afff6b1SMateusz Guzik struct malloc_type_stats *mtsp, zeromts; 1171cd814b26SRobert Watson struct malloc_type_header mth; 1172cd814b26SRobert Watson struct malloc_type *mtp; 11734e657159SMatthew D Fleming int error, i; 1174cd814b26SRobert Watson struct sbuf sbuf; 1175cd814b26SRobert Watson 117600f0e671SMatthew D Fleming error = sysctl_wire_old_buffer(req, 0); 117700f0e671SMatthew D Fleming if (error != 0) 117800f0e671SMatthew D Fleming return (error); 11794e657159SMatthew D Fleming sbuf_new_for_sysctl(&sbuf, NULL, 128, req); 11801eafc078SIan Lepore sbuf_clear_flags(&sbuf, SBUF_INCLUDENUL); 1181cd814b26SRobert Watson mtx_lock(&malloc_mtx); 1182cd814b26SRobert Watson 11839afff6b1SMateusz Guzik bzero(&zeromts, sizeof(zeromts)); 11849afff6b1SMateusz Guzik 1185cd814b26SRobert Watson /* 1186cd814b26SRobert Watson * Insert stream header. 1187cd814b26SRobert Watson */ 1188cd814b26SRobert Watson bzero(&mtsh, sizeof(mtsh)); 1189cd814b26SRobert Watson mtsh.mtsh_version = MALLOC_TYPE_STREAM_VERSION; 1190cd814b26SRobert Watson mtsh.mtsh_maxcpus = MAXCPU; 1191cd814b26SRobert Watson mtsh.mtsh_count = kmemcount; 11924e657159SMatthew D Fleming (void)sbuf_bcat(&sbuf, &mtsh, sizeof(mtsh)); 1193cd814b26SRobert Watson 1194cd814b26SRobert Watson /* 1195cd814b26SRobert Watson * Insert alternating sequence of type headers and type statistics. 1196cd814b26SRobert Watson */ 1197cd814b26SRobert Watson for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) { 1198cd814b26SRobert Watson mtip = (struct malloc_type_internal *)mtp->ks_handle; 1199cd814b26SRobert Watson 1200cd814b26SRobert Watson /* 1201cd814b26SRobert Watson * Insert type header. 1202cd814b26SRobert Watson */ 1203cd814b26SRobert Watson bzero(&mth, sizeof(mth)); 1204cd814b26SRobert Watson strlcpy(mth.mth_name, mtp->ks_shortdesc, MALLOC_MAX_NAME); 12054e657159SMatthew D Fleming (void)sbuf_bcat(&sbuf, &mth, sizeof(mth)); 1206cd814b26SRobert Watson 1207cd814b26SRobert Watson /* 1208cd814b26SRobert Watson * Insert type statistics for each CPU. 1209cd814b26SRobert Watson */ 12109afff6b1SMateusz Guzik for (i = 0; i <= mp_maxid; i++) { 12119afff6b1SMateusz Guzik mtsp = zpcpu_get_cpu(mtip->mti_stats, i); 12129afff6b1SMateusz Guzik (void)sbuf_bcat(&sbuf, mtsp, sizeof(*mtsp)); 1213cd814b26SRobert Watson } 12149afff6b1SMateusz Guzik /* 12159afff6b1SMateusz Guzik * Fill in the missing CPUs. 12169afff6b1SMateusz Guzik */ 12179afff6b1SMateusz Guzik for (; i < MAXCPU; i++) { 12189afff6b1SMateusz Guzik (void)sbuf_bcat(&sbuf, &zeromts, sizeof(zeromts)); 12199afff6b1SMateusz Guzik } 12209afff6b1SMateusz Guzik 1221cd814b26SRobert Watson } 1222cd814b26SRobert Watson mtx_unlock(&malloc_mtx); 12234e657159SMatthew D Fleming error = sbuf_finish(&sbuf); 1224cd814b26SRobert Watson sbuf_delete(&sbuf); 1225cd814b26SRobert Watson return (error); 1226cd814b26SRobert Watson } 1227cd814b26SRobert Watson 1228cd814b26SRobert Watson SYSCTL_PROC(_kern, OID_AUTO, malloc_stats, CTLFLAG_RD|CTLTYPE_STRUCT, 1229cd814b26SRobert Watson 0, 0, sysctl_kern_malloc_stats, "s,malloc_type_ustats", 1230cd814b26SRobert Watson "Return malloc types"); 1231cd814b26SRobert Watson 1232cd814b26SRobert Watson SYSCTL_INT(_kern, OID_AUTO, malloc_count, CTLFLAG_RD, &kmemcount, 0, 1233cd814b26SRobert Watson "Count of kernel malloc types"); 1234cd814b26SRobert Watson 123591dd776cSJohn Birrell void 123691dd776cSJohn Birrell malloc_type_list(malloc_type_list_func_t *func, void *arg) 123791dd776cSJohn Birrell { 123891dd776cSJohn Birrell struct malloc_type *mtp, **bufmtp; 123991dd776cSJohn Birrell int count, i; 124091dd776cSJohn Birrell size_t buflen; 124191dd776cSJohn Birrell 124291dd776cSJohn Birrell mtx_lock(&malloc_mtx); 124391dd776cSJohn Birrell restart: 124491dd776cSJohn Birrell mtx_assert(&malloc_mtx, MA_OWNED); 124591dd776cSJohn Birrell count = kmemcount; 124691dd776cSJohn Birrell mtx_unlock(&malloc_mtx); 124791dd776cSJohn Birrell 124891dd776cSJohn Birrell buflen = sizeof(struct malloc_type *) * count; 124991dd776cSJohn Birrell bufmtp = malloc(buflen, M_TEMP, M_WAITOK); 125091dd776cSJohn Birrell 125191dd776cSJohn Birrell mtx_lock(&malloc_mtx); 125291dd776cSJohn Birrell 125391dd776cSJohn Birrell if (count < kmemcount) { 125491dd776cSJohn Birrell free(bufmtp, M_TEMP); 125591dd776cSJohn Birrell goto restart; 125691dd776cSJohn Birrell } 125791dd776cSJohn Birrell 125891dd776cSJohn Birrell for (mtp = kmemstatistics, i = 0; mtp != NULL; mtp = mtp->ks_next, i++) 125991dd776cSJohn Birrell bufmtp[i] = mtp; 126091dd776cSJohn Birrell 126191dd776cSJohn Birrell mtx_unlock(&malloc_mtx); 126291dd776cSJohn Birrell 126391dd776cSJohn Birrell for (i = 0; i < count; i++) 126491dd776cSJohn Birrell (func)(bufmtp[i], arg); 126591dd776cSJohn Birrell 126691dd776cSJohn Birrell free(bufmtp, M_TEMP); 126791dd776cSJohn Birrell } 126891dd776cSJohn Birrell 1269909ed16cSRobert Watson #ifdef DDB 127046d70077SConrad Meyer static int64_t 127146d70077SConrad Meyer get_malloc_stats(const struct malloc_type_internal *mtip, uint64_t *allocs, 127246d70077SConrad Meyer uint64_t *inuse) 1273909ed16cSRobert Watson { 127446d70077SConrad Meyer const struct malloc_type_stats *mtsp; 127546d70077SConrad Meyer uint64_t frees, alloced, freed; 1276909ed16cSRobert Watson int i; 1277909ed16cSRobert Watson 127846d70077SConrad Meyer *allocs = 0; 1279909ed16cSRobert Watson frees = 0; 128024076d13SRobert Watson alloced = 0; 128124076d13SRobert Watson freed = 0; 12829afff6b1SMateusz Guzik for (i = 0; i <= mp_maxid; i++) { 12839afff6b1SMateusz Guzik mtsp = zpcpu_get_cpu(mtip->mti_stats, i); 128446d70077SConrad Meyer 128546d70077SConrad Meyer *allocs += mtsp->mts_numallocs; 128626e9d9b0SMark Johnston frees += mtsp->mts_numfrees; 128726e9d9b0SMark Johnston alloced += mtsp->mts_memalloced; 128826e9d9b0SMark Johnston freed += mtsp->mts_memfreed; 1289909ed16cSRobert Watson } 129046d70077SConrad Meyer *inuse = *allocs - frees; 129146d70077SConrad Meyer return (alloced - freed); 129246d70077SConrad Meyer } 129346d70077SConrad Meyer 129446d70077SConrad Meyer DB_SHOW_COMMAND(malloc, db_show_malloc) 129546d70077SConrad Meyer { 129646d70077SConrad Meyer const char *fmt_hdr, *fmt_entry; 129746d70077SConrad Meyer struct malloc_type *mtp; 129846d70077SConrad Meyer uint64_t allocs, inuse; 129946d70077SConrad Meyer int64_t size; 130046d70077SConrad Meyer /* variables for sorting */ 130146d70077SConrad Meyer struct malloc_type *last_mtype, *cur_mtype; 130246d70077SConrad Meyer int64_t cur_size, last_size; 130346d70077SConrad Meyer int ties; 130446d70077SConrad Meyer 130546d70077SConrad Meyer if (modif[0] == 'i') { 130646d70077SConrad Meyer fmt_hdr = "%s,%s,%s,%s\n"; 130746d70077SConrad Meyer fmt_entry = "\"%s\",%ju,%jdK,%ju\n"; 130846d70077SConrad Meyer } else { 130946d70077SConrad Meyer fmt_hdr = "%18s %12s %12s %12s\n"; 131046d70077SConrad Meyer fmt_entry = "%18s %12ju %12jdK %12ju\n"; 131146d70077SConrad Meyer } 131246d70077SConrad Meyer 131346d70077SConrad Meyer db_printf(fmt_hdr, "Type", "InUse", "MemUse", "Requests"); 131446d70077SConrad Meyer 131546d70077SConrad Meyer /* Select sort, largest size first. */ 131646d70077SConrad Meyer last_mtype = NULL; 131746d70077SConrad Meyer last_size = INT64_MAX; 131846d70077SConrad Meyer for (;;) { 131946d70077SConrad Meyer cur_mtype = NULL; 132046d70077SConrad Meyer cur_size = -1; 132146d70077SConrad Meyer ties = 0; 132246d70077SConrad Meyer 132346d70077SConrad Meyer for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) { 132446d70077SConrad Meyer /* 132546d70077SConrad Meyer * In the case of size ties, print out mtypes 132646d70077SConrad Meyer * in the order they are encountered. That is, 132746d70077SConrad Meyer * when we encounter the most recently output 132846d70077SConrad Meyer * mtype, we have already printed all preceding 132946d70077SConrad Meyer * ties, and we must print all following ties. 133046d70077SConrad Meyer */ 133146d70077SConrad Meyer if (mtp == last_mtype) { 133246d70077SConrad Meyer ties = 1; 133346d70077SConrad Meyer continue; 133446d70077SConrad Meyer } 133546d70077SConrad Meyer size = get_malloc_stats(mtp->ks_handle, &allocs, 133646d70077SConrad Meyer &inuse); 133746d70077SConrad Meyer if (size > cur_size && size < last_size + ties) { 133846d70077SConrad Meyer cur_size = size; 133946d70077SConrad Meyer cur_mtype = mtp; 134046d70077SConrad Meyer } 134146d70077SConrad Meyer } 134246d70077SConrad Meyer if (cur_mtype == NULL) 134346d70077SConrad Meyer break; 134446d70077SConrad Meyer 134546d70077SConrad Meyer size = get_malloc_stats(cur_mtype->ks_handle, &allocs, &inuse); 134646d70077SConrad Meyer db_printf(fmt_entry, cur_mtype->ks_shortdesc, inuse, 134746d70077SConrad Meyer howmany(size, 1024), allocs); 134846d70077SConrad Meyer 1349687c94aaSJohn Baldwin if (db_pager_quit) 1350687c94aaSJohn Baldwin break; 135146d70077SConrad Meyer 135246d70077SConrad Meyer last_mtype = cur_mtype; 135346d70077SConrad Meyer last_size = cur_size; 1354909ed16cSRobert Watson } 1355909ed16cSRobert Watson } 1356d7854da1SMatthew D Fleming 1357d7854da1SMatthew D Fleming #if MALLOC_DEBUG_MAXZONES > 1 1358d7854da1SMatthew D Fleming DB_SHOW_COMMAND(multizone_matches, db_show_multizone_matches) 1359d7854da1SMatthew D Fleming { 1360d7854da1SMatthew D Fleming struct malloc_type_internal *mtip; 1361d7854da1SMatthew D Fleming struct malloc_type *mtp; 1362d7854da1SMatthew D Fleming u_int subzone; 1363d7854da1SMatthew D Fleming 1364d7854da1SMatthew D Fleming if (!have_addr) { 1365d7854da1SMatthew D Fleming db_printf("Usage: show multizone_matches <malloc type/addr>\n"); 1366d7854da1SMatthew D Fleming return; 1367d7854da1SMatthew D Fleming } 1368d7854da1SMatthew D Fleming mtp = (void *)addr; 1369d7854da1SMatthew D Fleming if (mtp->ks_magic != M_MAGIC) { 1370d7854da1SMatthew D Fleming db_printf("Magic %lx does not match expected %x\n", 1371d7854da1SMatthew D Fleming mtp->ks_magic, M_MAGIC); 1372d7854da1SMatthew D Fleming return; 1373d7854da1SMatthew D Fleming } 1374d7854da1SMatthew D Fleming 1375d7854da1SMatthew D Fleming mtip = mtp->ks_handle; 1376d7854da1SMatthew D Fleming subzone = mtip->mti_zone; 1377d7854da1SMatthew D Fleming 1378d7854da1SMatthew D Fleming for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) { 1379d7854da1SMatthew D Fleming mtip = mtp->ks_handle; 1380d7854da1SMatthew D Fleming if (mtip->mti_zone != subzone) 1381d7854da1SMatthew D Fleming continue; 1382d7854da1SMatthew D Fleming db_printf("%s\n", mtp->ks_shortdesc); 1383687c94aaSJohn Baldwin if (db_pager_quit) 1384687c94aaSJohn Baldwin break; 1385d7854da1SMatthew D Fleming } 1386d7854da1SMatthew D Fleming } 1387d7854da1SMatthew D Fleming #endif /* MALLOC_DEBUG_MAXZONES > 1 */ 1388d7854da1SMatthew D Fleming #endif /* DDB */ 1389909ed16cSRobert Watson 13905e914b96SJeff Roberson #ifdef MALLOC_PROFILE 13915e914b96SJeff Roberson 13925e914b96SJeff Roberson static int 13935e914b96SJeff Roberson sysctl_kern_mprof(SYSCTL_HANDLER_ARGS) 13945e914b96SJeff Roberson { 139563a7e0a3SRobert Watson struct sbuf sbuf; 13965e914b96SJeff Roberson uint64_t count; 13975e914b96SJeff Roberson uint64_t waste; 13985e914b96SJeff Roberson uint64_t mem; 13995e914b96SJeff Roberson int error; 14005e914b96SJeff Roberson int rsize; 14015e914b96SJeff Roberson int size; 14025e914b96SJeff Roberson int i; 14035e914b96SJeff Roberson 14045e914b96SJeff Roberson waste = 0; 14055e914b96SJeff Roberson mem = 0; 14065e914b96SJeff Roberson 140700f0e671SMatthew D Fleming error = sysctl_wire_old_buffer(req, 0); 140800f0e671SMatthew D Fleming if (error != 0) 140900f0e671SMatthew D Fleming return (error); 14104e657159SMatthew D Fleming sbuf_new_for_sysctl(&sbuf, NULL, 128, req); 141163a7e0a3SRobert Watson sbuf_printf(&sbuf, 14125e914b96SJeff Roberson "\n Size Requests Real Size\n"); 14135e914b96SJeff Roberson for (i = 0; i < KMEM_ZSIZE; i++) { 14145e914b96SJeff Roberson size = i << KMEM_ZSHIFT; 14155e914b96SJeff Roberson rsize = kmemzones[kmemsize[i]].kz_size; 14165e914b96SJeff Roberson count = (long long unsigned)krequests[i]; 14175e914b96SJeff Roberson 141863a7e0a3SRobert Watson sbuf_printf(&sbuf, "%6d%28llu%11d\n", size, 141963a7e0a3SRobert Watson (unsigned long long)count, rsize); 14205e914b96SJeff Roberson 14215e914b96SJeff Roberson if ((rsize * count) > (size * count)) 14225e914b96SJeff Roberson waste += (rsize * count) - (size * count); 14235e914b96SJeff Roberson mem += (rsize * count); 14245e914b96SJeff Roberson } 142563a7e0a3SRobert Watson sbuf_printf(&sbuf, 14265e914b96SJeff Roberson "\nTotal memory used:\t%30llu\nTotal Memory wasted:\t%30llu\n", 14275e914b96SJeff Roberson (unsigned long long)mem, (unsigned long long)waste); 14284e657159SMatthew D Fleming error = sbuf_finish(&sbuf); 142963a7e0a3SRobert Watson sbuf_delete(&sbuf); 14305e914b96SJeff Roberson return (error); 14315e914b96SJeff Roberson } 14325e914b96SJeff Roberson 14335e914b96SJeff Roberson SYSCTL_OID(_kern, OID_AUTO, mprof, CTLTYPE_STRING|CTLFLAG_RD, 14345e914b96SJeff Roberson NULL, 0, sysctl_kern_mprof, "A", "Malloc Profiling"); 14355e914b96SJeff Roberson #endif /* MALLOC_PROFILE */ 1436