19454b2d8SWarner Losh /*- 2df8bae1dSRodney W. Grimes * Copyright (c) 1987, 1991, 1993 363a7e0a3SRobert Watson * The Regents of the University of California. 4bb1c7df8SRobert Watson * Copyright (c) 2005-2009 Robert N. M. Watson 563a7e0a3SRobert Watson * All rights reserved. 6df8bae1dSRodney W. Grimes * 7df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 8df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 9df8bae1dSRodney W. Grimes * are met: 10df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 12df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 13df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 14df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 15df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 16df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 17df8bae1dSRodney W. Grimes * without specific prior written permission. 18df8bae1dSRodney W. Grimes * 19df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29df8bae1dSRodney W. Grimes * SUCH DAMAGE. 30df8bae1dSRodney W. Grimes * 31df8bae1dSRodney W. Grimes * @(#)kern_malloc.c 8.3 (Berkeley) 1/4/94 32df8bae1dSRodney W. Grimes */ 33df8bae1dSRodney W. Grimes 340ce3f16dSRobert Watson /* 350ce3f16dSRobert Watson * Kernel malloc(9) implementation -- general purpose kernel memory allocator 360ce3f16dSRobert Watson * based on memory types. Back end is implemented using the UMA(9) zone 370ce3f16dSRobert Watson * allocator. A set of fixed-size buckets are used for smaller allocations, 380ce3f16dSRobert Watson * and a special UMA allocation interface is used for larger allocations. 390ce3f16dSRobert Watson * Callers declare memory types, and statistics are maintained independently 400ce3f16dSRobert Watson * for each memory type. Statistics are maintained per-CPU for performance 410ce3f16dSRobert Watson * reasons. See malloc(9) and comments in malloc.h for a detailed 420ce3f16dSRobert Watson * description. 430ce3f16dSRobert Watson */ 440ce3f16dSRobert Watson 45677b542eSDavid E. O'Brien #include <sys/cdefs.h> 46677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 47677b542eSDavid E. O'Brien 48909ed16cSRobert Watson #include "opt_ddb.h" 4991dd776cSJohn Birrell #include "opt_kdtrace.h" 508a58a9f6SJohn Dyson #include "opt_vm.h" 518a58a9f6SJohn Dyson 52df8bae1dSRodney W. Grimes #include <sys/param.h> 5326f9a767SRodney W. Grimes #include <sys/systm.h> 542d50560aSMarcel Moolenaar #include <sys/kdb.h> 55df8bae1dSRodney W. Grimes #include <sys/kernel.h> 56fb919e4dSMark Murray #include <sys/lock.h> 57df8bae1dSRodney W. Grimes #include <sys/malloc.h> 5854e7152cSDavid Greenman #include <sys/mbuf.h> 59eec258d2SJohn Baldwin #include <sys/mutex.h> 60efeaf95aSDavid Greenman #include <sys/vmmeter.h> 61a448b62aSJake Burkholder #include <sys/proc.h> 6263a7e0a3SRobert Watson #include <sys/sbuf.h> 636f267175SJeff Roberson #include <sys/sysctl.h> 641fb14a47SPoul-Henning Kamp #include <sys/time.h> 659a02e8c6SJason Evans 66df8bae1dSRodney W. Grimes #include <vm/vm.h> 6799571dc3SJeff Roberson #include <vm/pmap.h> 68efeaf95aSDavid Greenman #include <vm/vm_param.h> 69df8bae1dSRodney W. Grimes #include <vm/vm_kern.h> 70efeaf95aSDavid Greenman #include <vm/vm_extern.h> 713075778bSJohn Dyson #include <vm/vm_map.h> 7299571dc3SJeff Roberson #include <vm/vm_page.h> 738355f576SJeff Roberson #include <vm/uma.h> 748355f576SJeff Roberson #include <vm/uma_int.h> 758efc4effSJeff Roberson #include <vm/uma_dbg.h> 76df8bae1dSRodney W. Grimes 77e4eb384bSBosko Milekic #ifdef DEBUG_MEMGUARD 78e4eb384bSBosko Milekic #include <vm/memguard.h> 79e4eb384bSBosko Milekic #endif 80847a2a17SPawel Jakub Dawidek #ifdef DEBUG_REDZONE 81847a2a17SPawel Jakub Dawidek #include <vm/redzone.h> 82847a2a17SPawel Jakub Dawidek #endif 83e4eb384bSBosko Milekic 84984982d6SPoul-Henning Kamp #if defined(INVARIANTS) && defined(__i386__) 85984982d6SPoul-Henning Kamp #include <machine/cpu.h> 86984982d6SPoul-Henning Kamp #endif 87984982d6SPoul-Henning Kamp 88909ed16cSRobert Watson #include <ddb/ddb.h> 89909ed16cSRobert Watson 9091dd776cSJohn Birrell #ifdef KDTRACE_HOOKS 9191dd776cSJohn Birrell #include <sys/dtrace_bsd.h> 9291dd776cSJohn Birrell 9391dd776cSJohn Birrell dtrace_malloc_probe_func_t dtrace_malloc_probe; 9491dd776cSJohn Birrell #endif 9591dd776cSJohn Birrell 9644a8ff31SArchie Cobbs /* 9744a8ff31SArchie Cobbs * When realloc() is called, if the new size is sufficiently smaller than 9844a8ff31SArchie Cobbs * the old size, realloc() will allocate a new, smaller block to avoid 9944a8ff31SArchie Cobbs * wasting memory. 'Sufficiently smaller' is defined as: newsize <= 10044a8ff31SArchie Cobbs * oldsize / 2^n, where REALLOC_FRACTION defines the value of 'n'. 10144a8ff31SArchie Cobbs */ 10244a8ff31SArchie Cobbs #ifndef REALLOC_FRACTION 10344a8ff31SArchie Cobbs #define REALLOC_FRACTION 1 /* new block if <= half the size */ 10444a8ff31SArchie Cobbs #endif 10544a8ff31SArchie Cobbs 1060ce3f16dSRobert Watson /* 1070ce3f16dSRobert Watson * Centrally define some common malloc types. 1080ce3f16dSRobert Watson */ 1093b6fb885SPoul-Henning Kamp MALLOC_DEFINE(M_CACHE, "cache", "Various Dynamically allocated caches"); 1109ef246c6SBruce Evans MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory"); 1119ef246c6SBruce Evans MALLOC_DEFINE(M_TEMP, "temp", "misc temporary data buffers"); 1129ef246c6SBruce Evans 11382cd038dSYoshinobu Inoue MALLOC_DEFINE(M_IP6OPT, "ip6opt", "IPv6 options"); 11482cd038dSYoshinobu Inoue MALLOC_DEFINE(M_IP6NDP, "ip6ndp", "IPv6 Neighbor Discovery"); 11582cd038dSYoshinobu Inoue 1164d77a549SAlfred Perlstein static void kmeminit(void *); 117237fdd78SRobert Watson SYSINIT(kmem, SI_SUB_KMEM, SI_ORDER_FIRST, kmeminit, NULL); 1182b14f991SJulian Elischer 119a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_FREE, "free", "should be on free list"); 120a1c995b6SPoul-Henning Kamp 121db669378SPeter Wemm static struct malloc_type *kmemstatistics; 122dc2e1e3fSRobert Watson static vm_offset_t kmembase; 123dc2e1e3fSRobert Watson static vm_offset_t kmemlimit; 124cd814b26SRobert Watson static int kmemcount; 1251f6889a1SMatthew Dillon 1268355f576SJeff Roberson #define KMEM_ZSHIFT 4 1278355f576SJeff Roberson #define KMEM_ZBASE 16 1288355f576SJeff Roberson #define KMEM_ZMASK (KMEM_ZBASE - 1) 1298355f576SJeff Roberson 1309fb535deSJeff Roberson #define KMEM_ZMAX PAGE_SIZE 1318355f576SJeff Roberson #define KMEM_ZSIZE (KMEM_ZMAX >> KMEM_ZSHIFT) 13260ae52f7SEd Schouten static uint8_t kmemsize[KMEM_ZSIZE + 1]; 1336f267175SJeff Roberson 134d7854da1SMatthew D Fleming #ifndef MALLOC_DEBUG_MAXZONES 135d7854da1SMatthew D Fleming #define MALLOC_DEBUG_MAXZONES 1 136d7854da1SMatthew D Fleming #endif 137d7854da1SMatthew D Fleming static int numzones = MALLOC_DEBUG_MAXZONES; 138d7854da1SMatthew D Fleming 1390ce3f16dSRobert Watson /* 1400ce3f16dSRobert Watson * Small malloc(9) memory allocations are allocated from a set of UMA buckets 1410ce3f16dSRobert Watson * of various sizes. 1420ce3f16dSRobert Watson * 1430ce3f16dSRobert Watson * XXX: The comment here used to read "These won't be powers of two for 1440ce3f16dSRobert Watson * long." It's possible that a significant amount of wasted memory could be 1450ce3f16dSRobert Watson * recovered by tuning the sizes of these buckets. 1460ce3f16dSRobert Watson */ 1478355f576SJeff Roberson struct { 1486f267175SJeff Roberson int kz_size; 1496f267175SJeff Roberson char *kz_name; 150d7854da1SMatthew D Fleming uma_zone_t kz_zone[MALLOC_DEBUG_MAXZONES]; 1516f267175SJeff Roberson } kmemzones[] = { 152d7854da1SMatthew D Fleming {16, "16", }, 153d7854da1SMatthew D Fleming {32, "32", }, 154d7854da1SMatthew D Fleming {64, "64", }, 155d7854da1SMatthew D Fleming {128, "128", }, 156d7854da1SMatthew D Fleming {256, "256", }, 157d7854da1SMatthew D Fleming {512, "512", }, 158d7854da1SMatthew D Fleming {1024, "1024", }, 159d7854da1SMatthew D Fleming {2048, "2048", }, 160d7854da1SMatthew D Fleming {4096, "4096", }, 1619fb535deSJeff Roberson #if PAGE_SIZE > 4096 162d7854da1SMatthew D Fleming {8192, "8192", }, 1639fb535deSJeff Roberson #if PAGE_SIZE > 8192 164d7854da1SMatthew D Fleming {16384, "16384", }, 1659fb535deSJeff Roberson #if PAGE_SIZE > 16384 166d7854da1SMatthew D Fleming {32768, "32768", }, 1679fb535deSJeff Roberson #if PAGE_SIZE > 32768 168d7854da1SMatthew D Fleming {65536, "65536", }, 1699fb535deSJeff Roberson #if PAGE_SIZE > 65536 1709fb535deSJeff Roberson #error "Unsupported PAGE_SIZE" 1719fb535deSJeff Roberson #endif /* 65536 */ 1729fb535deSJeff Roberson #endif /* 32768 */ 1739fb535deSJeff Roberson #endif /* 16384 */ 1749fb535deSJeff Roberson #endif /* 8192 */ 1759fb535deSJeff Roberson #endif /* 4096 */ 1768355f576SJeff Roberson {0, NULL}, 1778355f576SJeff Roberson }; 1788355f576SJeff Roberson 1790ce3f16dSRobert Watson /* 1800ce3f16dSRobert Watson * Zone to allocate malloc type descriptions from. For ABI reasons, memory 1810ce3f16dSRobert Watson * types are described by a data structure passed by the declaring code, but 1820ce3f16dSRobert Watson * the malloc(9) implementation has its own data structure describing the 1830ce3f16dSRobert Watson * type and statistics. This permits the malloc(9)-internal data structures 1840ce3f16dSRobert Watson * to be modified without breaking binary-compiled kernel modules that 1850ce3f16dSRobert Watson * declare malloc types. 1860ce3f16dSRobert Watson */ 18763a7e0a3SRobert Watson static uma_zone_t mt_zone; 18863a7e0a3SRobert Watson 189b89eaf4eSAlan Cox u_long vm_kmem_size; 190b89eaf4eSAlan Cox SYSCTL_ULONG(_vm, OID_AUTO, kmem_size, CTLFLAG_RD, &vm_kmem_size, 0, 19184344f9fSDag-Erling Smørgrav "Size of kernel memory"); 1925a34a9f0SJeff Roberson 193b89eaf4eSAlan Cox static u_long vm_kmem_size_min; 194b89eaf4eSAlan Cox SYSCTL_ULONG(_vm, OID_AUTO, kmem_size_min, CTLFLAG_RD, &vm_kmem_size_min, 0, 1950e5179e4SStephane E. Potvin "Minimum size of kernel memory"); 1960e5179e4SStephane E. Potvin 197b89eaf4eSAlan Cox static u_long vm_kmem_size_max; 198b89eaf4eSAlan Cox SYSCTL_ULONG(_vm, OID_AUTO, kmem_size_max, CTLFLAG_RD, &vm_kmem_size_max, 0, 199479439b4SDag-Erling Smørgrav "Maximum size of kernel memory"); 200479439b4SDag-Erling Smørgrav 201b89eaf4eSAlan Cox static u_int vm_kmem_size_scale; 202479439b4SDag-Erling Smørgrav SYSCTL_UINT(_vm, OID_AUTO, kmem_size_scale, CTLFLAG_RD, &vm_kmem_size_scale, 0, 203479439b4SDag-Erling Smørgrav "Scale factor for kernel memory size"); 204479439b4SDag-Erling Smørgrav 2055a34a9f0SJeff Roberson /* 20699571dc3SJeff Roberson * The malloc_mtx protects the kmemstatistics linked list. 2075a34a9f0SJeff Roberson */ 2085a34a9f0SJeff Roberson struct mtx malloc_mtx; 20969ef67f9SJason Evans 2105e914b96SJeff Roberson #ifdef MALLOC_PROFILE 2115e914b96SJeff Roberson uint64_t krequests[KMEM_ZSIZE + 1]; 2126f267175SJeff Roberson 2135e914b96SJeff Roberson static int sysctl_kern_mprof(SYSCTL_HANDLER_ARGS); 2145e914b96SJeff Roberson #endif 2155e914b96SJeff Roberson 216cd814b26SRobert Watson static int sysctl_kern_malloc_stats(SYSCTL_HANDLER_ARGS); 217df8bae1dSRodney W. Grimes 2180ce3f16dSRobert Watson /* 2190ce3f16dSRobert Watson * time_uptime of the last malloc(9) failure (induced or real). 2200ce3f16dSRobert Watson */ 2211fb14a47SPoul-Henning Kamp static time_t t_malloc_fail; 2221fb14a47SPoul-Henning Kamp 223d7854da1SMatthew D Fleming #if defined(MALLOC_MAKE_FAILURES) || (MALLOC_DEBUG_MAXZONES > 1) 224d7854da1SMatthew D Fleming SYSCTL_NODE(_debug, OID_AUTO, malloc, CTLFLAG_RD, 0, 225d7854da1SMatthew D Fleming "Kernel malloc debugging options"); 226d7854da1SMatthew D Fleming #endif 227d7854da1SMatthew D Fleming 228eae870cdSRobert Watson /* 2290ce3f16dSRobert Watson * malloc(9) fault injection -- cause malloc failures every (n) mallocs when 2300ce3f16dSRobert Watson * the caller specifies M_NOWAIT. If set to 0, no failures are caused. 231eae870cdSRobert Watson */ 2320ce3f16dSRobert Watson #ifdef MALLOC_MAKE_FAILURES 233eae870cdSRobert Watson static int malloc_failure_rate; 234eae870cdSRobert Watson static int malloc_nowait_count; 235eae870cdSRobert Watson static int malloc_failure_count; 236eae870cdSRobert Watson SYSCTL_INT(_debug_malloc, OID_AUTO, failure_rate, CTLFLAG_RW, 237eae870cdSRobert Watson &malloc_failure_rate, 0, "Every (n) mallocs with M_NOWAIT will fail"); 238f2538508SRobert Watson TUNABLE_INT("debug.malloc.failure_rate", &malloc_failure_rate); 239eae870cdSRobert Watson SYSCTL_INT(_debug_malloc, OID_AUTO, failure_count, CTLFLAG_RD, 240eae870cdSRobert Watson &malloc_failure_count, 0, "Number of imposed M_NOWAIT malloc failures"); 241eae870cdSRobert Watson #endif 242eae870cdSRobert Watson 243d7854da1SMatthew D Fleming /* 244d7854da1SMatthew D Fleming * malloc(9) uma zone separation -- sub-page buffer overruns in one 245d7854da1SMatthew D Fleming * malloc type will affect only a subset of other malloc types. 246d7854da1SMatthew D Fleming */ 247d7854da1SMatthew D Fleming #if MALLOC_DEBUG_MAXZONES > 1 248d7854da1SMatthew D Fleming static void 249d7854da1SMatthew D Fleming tunable_set_numzones(void) 250d7854da1SMatthew D Fleming { 251d7854da1SMatthew D Fleming 252d7854da1SMatthew D Fleming TUNABLE_INT_FETCH("debug.malloc.numzones", 253d7854da1SMatthew D Fleming &numzones); 254d7854da1SMatthew D Fleming 255d7854da1SMatthew D Fleming /* Sanity check the number of malloc uma zones. */ 256d7854da1SMatthew D Fleming if (numzones <= 0) 257d7854da1SMatthew D Fleming numzones = 1; 258d7854da1SMatthew D Fleming if (numzones > MALLOC_DEBUG_MAXZONES) 259d7854da1SMatthew D Fleming numzones = MALLOC_DEBUG_MAXZONES; 260d7854da1SMatthew D Fleming } 261d7854da1SMatthew D Fleming SYSINIT(numzones, SI_SUB_TUNABLES, SI_ORDER_ANY, tunable_set_numzones, NULL); 262d7854da1SMatthew D Fleming SYSCTL_INT(_debug_malloc, OID_AUTO, numzones, CTLFLAG_RDTUN, 263d7854da1SMatthew D Fleming &numzones, 0, "Number of malloc uma subzones"); 264d7854da1SMatthew D Fleming 265d7854da1SMatthew D Fleming /* 266d7854da1SMatthew D Fleming * Any number that changes regularly is an okay choice for the 267d7854da1SMatthew D Fleming * offset. Build numbers are pretty good of you have them. 268d7854da1SMatthew D Fleming */ 269d7854da1SMatthew D Fleming static u_int zone_offset = __FreeBSD_version; 270d7854da1SMatthew D Fleming TUNABLE_INT("debug.malloc.zone_offset", &zone_offset); 271d7854da1SMatthew D Fleming SYSCTL_UINT(_debug_malloc, OID_AUTO, zone_offset, CTLFLAG_RDTUN, 272d7854da1SMatthew D Fleming &zone_offset, 0, "Separate malloc types by examining the " 273d7854da1SMatthew D Fleming "Nth character in the malloc type short description."); 274d7854da1SMatthew D Fleming 275d7854da1SMatthew D Fleming static u_int 276d7854da1SMatthew D Fleming mtp_get_subzone(const char *desc) 277d7854da1SMatthew D Fleming { 278d7854da1SMatthew D Fleming size_t len; 279d7854da1SMatthew D Fleming u_int val; 280d7854da1SMatthew D Fleming 281d7854da1SMatthew D Fleming if (desc == NULL || (len = strlen(desc)) == 0) 282d7854da1SMatthew D Fleming return (0); 283d7854da1SMatthew D Fleming val = desc[zone_offset % len]; 284d7854da1SMatthew D Fleming return (val % numzones); 285d7854da1SMatthew D Fleming } 286d7854da1SMatthew D Fleming #elif MALLOC_DEBUG_MAXZONES == 0 287d7854da1SMatthew D Fleming #error "MALLOC_DEBUG_MAXZONES must be positive." 288d7854da1SMatthew D Fleming #else 289d7854da1SMatthew D Fleming static inline u_int 290d7854da1SMatthew D Fleming mtp_get_subzone(const char *desc) 291d7854da1SMatthew D Fleming { 292d7854da1SMatthew D Fleming 293d7854da1SMatthew D Fleming return (0); 294d7854da1SMatthew D Fleming } 295d7854da1SMatthew D Fleming #endif /* MALLOC_DEBUG_MAXZONES > 1 */ 296d7854da1SMatthew D Fleming 2971fb14a47SPoul-Henning Kamp int 2981fb14a47SPoul-Henning Kamp malloc_last_fail(void) 2991fb14a47SPoul-Henning Kamp { 3001fb14a47SPoul-Henning Kamp 3011fb14a47SPoul-Henning Kamp return (time_uptime - t_malloc_fail); 3021fb14a47SPoul-Henning Kamp } 3031fb14a47SPoul-Henning Kamp 304df8bae1dSRodney W. Grimes /* 3050ce3f16dSRobert Watson * An allocation has succeeded -- update malloc type statistics for the 3060ce3f16dSRobert Watson * amount of bucket size. Occurs within a critical section so that the 3070ce3f16dSRobert Watson * thread isn't preempted and doesn't migrate while updating per-PCU 3080ce3f16dSRobert Watson * statistics. 3094362fadaSBrian Feldman */ 3104362fadaSBrian Feldman static void 31163a7e0a3SRobert Watson malloc_type_zone_allocated(struct malloc_type *mtp, unsigned long size, 3124362fadaSBrian Feldman int zindx) 3134362fadaSBrian Feldman { 31463a7e0a3SRobert Watson struct malloc_type_internal *mtip; 31563a7e0a3SRobert Watson struct malloc_type_stats *mtsp; 31663a7e0a3SRobert Watson 31763a7e0a3SRobert Watson critical_enter(); 31863a7e0a3SRobert Watson mtip = mtp->ks_handle; 31963a7e0a3SRobert Watson mtsp = &mtip->mti_stats[curcpu]; 32073864adbSPawel Jakub Dawidek if (size > 0) { 32163a7e0a3SRobert Watson mtsp->mts_memalloced += size; 32263a7e0a3SRobert Watson mtsp->mts_numallocs++; 32373864adbSPawel Jakub Dawidek } 3244362fadaSBrian Feldman if (zindx != -1) 32563a7e0a3SRobert Watson mtsp->mts_size |= 1 << zindx; 32691dd776cSJohn Birrell 32791dd776cSJohn Birrell #ifdef KDTRACE_HOOKS 32891dd776cSJohn Birrell if (dtrace_malloc_probe != NULL) { 32991dd776cSJohn Birrell uint32_t probe_id = mtip->mti_probes[DTMALLOC_PROBE_MALLOC]; 33091dd776cSJohn Birrell if (probe_id != 0) 33191dd776cSJohn Birrell (dtrace_malloc_probe)(probe_id, 33291dd776cSJohn Birrell (uintptr_t) mtp, (uintptr_t) mtip, 33391dd776cSJohn Birrell (uintptr_t) mtsp, size, zindx); 33491dd776cSJohn Birrell } 33591dd776cSJohn Birrell #endif 33691dd776cSJohn Birrell 33763a7e0a3SRobert Watson critical_exit(); 3384362fadaSBrian Feldman } 3394362fadaSBrian Feldman 3404362fadaSBrian Feldman void 34163a7e0a3SRobert Watson malloc_type_allocated(struct malloc_type *mtp, unsigned long size) 3424362fadaSBrian Feldman { 34363a7e0a3SRobert Watson 34473864adbSPawel Jakub Dawidek if (size > 0) 34563a7e0a3SRobert Watson malloc_type_zone_allocated(mtp, size, -1); 3464362fadaSBrian Feldman } 3474362fadaSBrian Feldman 3484362fadaSBrian Feldman /* 3493805385eSRobert Watson * A free operation has occurred -- update malloc type statistics for the 3500ce3f16dSRobert Watson * amount of the bucket size. Occurs within a critical section so that the 3510ce3f16dSRobert Watson * thread isn't preempted and doesn't migrate while updating per-CPU 3520ce3f16dSRobert Watson * statistics. 3534362fadaSBrian Feldman */ 3544362fadaSBrian Feldman void 35563a7e0a3SRobert Watson malloc_type_freed(struct malloc_type *mtp, unsigned long size) 3564362fadaSBrian Feldman { 35763a7e0a3SRobert Watson struct malloc_type_internal *mtip; 35863a7e0a3SRobert Watson struct malloc_type_stats *mtsp; 35963a7e0a3SRobert Watson 36063a7e0a3SRobert Watson critical_enter(); 36163a7e0a3SRobert Watson mtip = mtp->ks_handle; 36263a7e0a3SRobert Watson mtsp = &mtip->mti_stats[curcpu]; 36363a7e0a3SRobert Watson mtsp->mts_memfreed += size; 36463a7e0a3SRobert Watson mtsp->mts_numfrees++; 36591dd776cSJohn Birrell 36691dd776cSJohn Birrell #ifdef KDTRACE_HOOKS 36791dd776cSJohn Birrell if (dtrace_malloc_probe != NULL) { 36891dd776cSJohn Birrell uint32_t probe_id = mtip->mti_probes[DTMALLOC_PROBE_FREE]; 36991dd776cSJohn Birrell if (probe_id != 0) 37091dd776cSJohn Birrell (dtrace_malloc_probe)(probe_id, 37191dd776cSJohn Birrell (uintptr_t) mtp, (uintptr_t) mtip, 37291dd776cSJohn Birrell (uintptr_t) mtsp, size, 0); 37391dd776cSJohn Birrell } 37491dd776cSJohn Birrell #endif 37591dd776cSJohn Birrell 37663a7e0a3SRobert Watson critical_exit(); 3774362fadaSBrian Feldman } 3784362fadaSBrian Feldman 3794362fadaSBrian Feldman /* 3801c7c3c6aSMatthew Dillon * malloc: 3811c7c3c6aSMatthew Dillon * 3821c7c3c6aSMatthew Dillon * Allocate a block of memory. 3831c7c3c6aSMatthew Dillon * 3841c7c3c6aSMatthew Dillon * If M_NOWAIT is set, this routine will not block and return NULL if 3851c7c3c6aSMatthew Dillon * the allocation fails. 386df8bae1dSRodney W. Grimes */ 387df8bae1dSRodney W. Grimes void * 38863a7e0a3SRobert Watson malloc(unsigned long size, struct malloc_type *mtp, int flags) 389df8bae1dSRodney W. Grimes { 3906f267175SJeff Roberson int indx; 391d7854da1SMatthew D Fleming struct malloc_type_internal *mtip; 3928355f576SJeff Roberson caddr_t va; 3938355f576SJeff Roberson uma_zone_t zone; 394847a2a17SPawel Jakub Dawidek #if defined(DIAGNOSTIC) || defined(DEBUG_REDZONE) 3954db4f5c8SPoul-Henning Kamp unsigned long osize = size; 3964db4f5c8SPoul-Henning Kamp #endif 397df8bae1dSRodney W. Grimes 398194a0abfSPoul-Henning Kamp #ifdef INVARIANTS 399bb1c7df8SRobert Watson KASSERT(mtp->ks_magic == M_MAGIC, ("malloc: bad malloc type magic")); 400d3c11994SPoul-Henning Kamp /* 40123198357SRuslan Ermilov * Check that exactly one of M_WAITOK or M_NOWAIT is specified. 402d3c11994SPoul-Henning Kamp */ 40323198357SRuslan Ermilov indx = flags & (M_WAITOK | M_NOWAIT); 404d3c11994SPoul-Henning Kamp if (indx != M_NOWAIT && indx != M_WAITOK) { 405d3c11994SPoul-Henning Kamp static struct timeval lasterr; 406d3c11994SPoul-Henning Kamp static int curerr, once; 407d3c11994SPoul-Henning Kamp if (once == 0 && ppsratecheck(&lasterr, &curerr, 1)) { 408d3c11994SPoul-Henning Kamp printf("Bad malloc flags: %x\n", indx); 4092d50560aSMarcel Moolenaar kdb_backtrace(); 410d3c11994SPoul-Henning Kamp flags |= M_WAITOK; 411d3c11994SPoul-Henning Kamp once++; 412d3c11994SPoul-Henning Kamp } 413d3c11994SPoul-Henning Kamp } 414194a0abfSPoul-Henning Kamp #endif 415eae870cdSRobert Watson #ifdef MALLOC_MAKE_FAILURES 416eae870cdSRobert Watson if ((flags & M_NOWAIT) && (malloc_failure_rate != 0)) { 417eae870cdSRobert Watson atomic_add_int(&malloc_nowait_count, 1); 418eae870cdSRobert Watson if ((malloc_nowait_count % malloc_failure_rate) == 0) { 419eae870cdSRobert Watson atomic_add_int(&malloc_failure_count, 1); 4203f6ee876SPoul-Henning Kamp t_malloc_fail = time_uptime; 421eae870cdSRobert Watson return (NULL); 422eae870cdSRobert Watson } 423eae870cdSRobert Watson } 424eae870cdSRobert Watson #endif 425d3c11994SPoul-Henning Kamp if (flags & M_WAITOK) 426b40ce416SJulian Elischer KASSERT(curthread->td_intr_nesting_level == 0, 427a163d034SWarner Losh ("malloc(M_WAITOK) in interrupt context")); 428e4eb384bSBosko Milekic 429e4eb384bSBosko Milekic #ifdef DEBUG_MEMGUARD 430*e3813573SMatthew D Fleming if (memguard_cmp(mtp, size)) { 431*e3813573SMatthew D Fleming va = memguard_alloc(size, flags); 432*e3813573SMatthew D Fleming if (va != NULL) 433*e3813573SMatthew D Fleming return (va); 434*e3813573SMatthew D Fleming /* This is unfortunate but should not be fatal. */ 435*e3813573SMatthew D Fleming } 436e4eb384bSBosko Milekic #endif 437e4eb384bSBosko Milekic 438847a2a17SPawel Jakub Dawidek #ifdef DEBUG_REDZONE 439847a2a17SPawel Jakub Dawidek size = redzone_size_ntor(size); 440847a2a17SPawel Jakub Dawidek #endif 441847a2a17SPawel Jakub Dawidek 4428355f576SJeff Roberson if (size <= KMEM_ZMAX) { 443d7854da1SMatthew D Fleming mtip = mtp->ks_handle; 4446f267175SJeff Roberson if (size & KMEM_ZMASK) 4456f267175SJeff Roberson size = (size & ~KMEM_ZMASK) + KMEM_ZBASE; 4466f267175SJeff Roberson indx = kmemsize[size >> KMEM_ZSHIFT]; 447d7854da1SMatthew D Fleming KASSERT(mtip->mti_zone < numzones, 448d7854da1SMatthew D Fleming ("mti_zone %u out of range %d", 449d7854da1SMatthew D Fleming mtip->mti_zone, numzones)); 450d7854da1SMatthew D Fleming zone = kmemzones[indx].kz_zone[mtip->mti_zone]; 4516f267175SJeff Roberson #ifdef MALLOC_PROFILE 4526f267175SJeff Roberson krequests[size >> KMEM_ZSHIFT]++; 4536f267175SJeff Roberson #endif 4548355f576SJeff Roberson va = uma_zalloc(zone, flags); 4554362fadaSBrian Feldman if (va != NULL) 456e20a199fSJeff Roberson size = zone->uz_size; 45763a7e0a3SRobert Watson malloc_type_zone_allocated(mtp, va == NULL ? 0 : size, indx); 4588355f576SJeff Roberson } else { 4596f267175SJeff Roberson size = roundup(size, PAGE_SIZE); 4608355f576SJeff Roberson zone = NULL; 4618355f576SJeff Roberson va = uma_large_malloc(size, flags); 46263a7e0a3SRobert Watson malloc_type_allocated(mtp, va == NULL ? 0 : size); 463df8bae1dSRodney W. Grimes } 4641282e9acSPoul-Henning Kamp if (flags & M_WAITOK) 465a163d034SWarner Losh KASSERT(va != NULL, ("malloc(M_WAITOK) returned NULL")); 4661282e9acSPoul-Henning Kamp else if (va == NULL) 4671fb14a47SPoul-Henning Kamp t_malloc_fail = time_uptime; 4684db4f5c8SPoul-Henning Kamp #ifdef DIAGNOSTIC 4691282e9acSPoul-Henning Kamp if (va != NULL && !(flags & M_ZERO)) { 4704db4f5c8SPoul-Henning Kamp memset(va, 0x70, osize); 4714db4f5c8SPoul-Henning Kamp } 4724db4f5c8SPoul-Henning Kamp #endif 473847a2a17SPawel Jakub Dawidek #ifdef DEBUG_REDZONE 474847a2a17SPawel Jakub Dawidek if (va != NULL) 475847a2a17SPawel Jakub Dawidek va = redzone_setup(va, osize); 476847a2a17SPawel Jakub Dawidek #endif 477df8bae1dSRodney W. Grimes return ((void *) va); 478df8bae1dSRodney W. Grimes } 479df8bae1dSRodney W. Grimes 480df8bae1dSRodney W. Grimes /* 4811c7c3c6aSMatthew Dillon * free: 4821c7c3c6aSMatthew Dillon * 483df8bae1dSRodney W. Grimes * Free a block of memory allocated by malloc. 4841c7c3c6aSMatthew Dillon * 4851c7c3c6aSMatthew Dillon * This routine may not block. 486df8bae1dSRodney W. Grimes */ 487df8bae1dSRodney W. Grimes void 48863a7e0a3SRobert Watson free(void *addr, struct malloc_type *mtp) 489df8bae1dSRodney W. Grimes { 49099571dc3SJeff Roberson uma_slab_t slab; 49199571dc3SJeff Roberson u_long size; 492254c6cb3SPoul-Henning Kamp 493bb1c7df8SRobert Watson KASSERT(mtp->ks_magic == M_MAGIC, ("free: bad malloc type magic")); 494bb1c7df8SRobert Watson 49544a8ff31SArchie Cobbs /* free(NULL, ...) does nothing */ 49644a8ff31SArchie Cobbs if (addr == NULL) 49744a8ff31SArchie Cobbs return; 49844a8ff31SArchie Cobbs 499e4eb384bSBosko Milekic #ifdef DEBUG_MEMGUARD 500*e3813573SMatthew D Fleming if (is_memguard_addr(addr)) { 501e4eb384bSBosko Milekic memguard_free(addr); 502e4eb384bSBosko Milekic return; 503e4eb384bSBosko Milekic } 504e4eb384bSBosko Milekic #endif 505e4eb384bSBosko Milekic 506847a2a17SPawel Jakub Dawidek #ifdef DEBUG_REDZONE 507847a2a17SPawel Jakub Dawidek redzone_check(addr); 508847a2a17SPawel Jakub Dawidek addr = redzone_addr_ntor(addr); 509847a2a17SPawel Jakub Dawidek #endif 510847a2a17SPawel Jakub Dawidek 51199571dc3SJeff Roberson slab = vtoslab((vm_offset_t)addr & (~UMA_SLAB_MASK)); 5128355f576SJeff Roberson 5138355f576SJeff Roberson if (slab == NULL) 5146f267175SJeff Roberson panic("free: address %p(%p) has not been allocated.\n", 51599571dc3SJeff Roberson addr, (void *)((u_long)addr & (~UMA_SLAB_MASK))); 51699571dc3SJeff Roberson 5178355f576SJeff Roberson 5188355f576SJeff Roberson if (!(slab->us_flags & UMA_SLAB_MALLOC)) { 5198f70816cSJeff Roberson #ifdef INVARIANTS 52063a7e0a3SRobert Watson struct malloc_type **mtpp = addr; 5218f70816cSJeff Roberson #endif 522099a0e58SBosko Milekic size = slab->us_keg->uk_size; 5238f70816cSJeff Roberson #ifdef INVARIANTS 5248f70816cSJeff Roberson /* 5258f70816cSJeff Roberson * Cache a pointer to the malloc_type that most recently freed 5268f70816cSJeff Roberson * this memory here. This way we know who is most likely to 5278f70816cSJeff Roberson * have stepped on it later. 5288f70816cSJeff Roberson * 5298f70816cSJeff Roberson * This code assumes that size is a multiple of 8 bytes for 5308f70816cSJeff Roberson * 64 bit machines 5318f70816cSJeff Roberson */ 53263a7e0a3SRobert Watson mtpp = (struct malloc_type **) 53363a7e0a3SRobert Watson ((unsigned long)mtpp & ~UMA_ALIGN_PTR); 53463a7e0a3SRobert Watson mtpp += (size - sizeof(struct malloc_type *)) / 5358f70816cSJeff Roberson sizeof(struct malloc_type *); 53663a7e0a3SRobert Watson *mtpp = mtp; 5378f70816cSJeff Roberson #endif 538099a0e58SBosko Milekic uma_zfree_arg(LIST_FIRST(&slab->us_keg->uk_zones), addr, slab); 53914bf02f8SJohn Dyson } else { 5408355f576SJeff Roberson size = slab->us_size; 5418355f576SJeff Roberson uma_large_free(slab); 54214bf02f8SJohn Dyson } 54363a7e0a3SRobert Watson malloc_type_freed(mtp, size); 544df8bae1dSRodney W. Grimes } 545df8bae1dSRodney W. Grimes 546df8bae1dSRodney W. Grimes /* 54744a8ff31SArchie Cobbs * realloc: change the size of a memory block 54844a8ff31SArchie Cobbs */ 54944a8ff31SArchie Cobbs void * 55063a7e0a3SRobert Watson realloc(void *addr, unsigned long size, struct malloc_type *mtp, int flags) 55144a8ff31SArchie Cobbs { 5528355f576SJeff Roberson uma_slab_t slab; 55344a8ff31SArchie Cobbs unsigned long alloc; 55444a8ff31SArchie Cobbs void *newaddr; 55544a8ff31SArchie Cobbs 556bb1c7df8SRobert Watson KASSERT(mtp->ks_magic == M_MAGIC, 557bb1c7df8SRobert Watson ("realloc: bad malloc type magic")); 558bb1c7df8SRobert Watson 55944a8ff31SArchie Cobbs /* realloc(NULL, ...) is equivalent to malloc(...) */ 56044a8ff31SArchie Cobbs if (addr == NULL) 56163a7e0a3SRobert Watson return (malloc(size, mtp, flags)); 56263a7e0a3SRobert Watson 56363a7e0a3SRobert Watson /* 56463a7e0a3SRobert Watson * XXX: Should report free of old memory and alloc of new memory to 56563a7e0a3SRobert Watson * per-CPU stats. 56663a7e0a3SRobert Watson */ 56744a8ff31SArchie Cobbs 568e4eb384bSBosko Milekic #ifdef DEBUG_MEMGUARD 569*e3813573SMatthew D Fleming if (is_memguard_addr(addr)) { 570e4eb384bSBosko Milekic slab = NULL; 571e4eb384bSBosko Milekic alloc = size; 572*e3813573SMatthew D Fleming goto remalloc; 573*e3813573SMatthew D Fleming } 574e4eb384bSBosko Milekic #endif 575e4eb384bSBosko Milekic 576847a2a17SPawel Jakub Dawidek #ifdef DEBUG_REDZONE 577847a2a17SPawel Jakub Dawidek slab = NULL; 578847a2a17SPawel Jakub Dawidek alloc = redzone_get_size(addr); 579847a2a17SPawel Jakub Dawidek #else 58099571dc3SJeff Roberson slab = vtoslab((vm_offset_t)addr & ~(UMA_SLAB_MASK)); 5818355f576SJeff Roberson 58244a8ff31SArchie Cobbs /* Sanity check */ 5838355f576SJeff Roberson KASSERT(slab != NULL, 58444a8ff31SArchie Cobbs ("realloc: address %p out of range", (void *)addr)); 58544a8ff31SArchie Cobbs 58644a8ff31SArchie Cobbs /* Get the size of the original block */ 587619f2841SPawel Jakub Dawidek if (!(slab->us_flags & UMA_SLAB_MALLOC)) 588099a0e58SBosko Milekic alloc = slab->us_keg->uk_size; 5898355f576SJeff Roberson else 5908355f576SJeff Roberson alloc = slab->us_size; 59144a8ff31SArchie Cobbs 59244a8ff31SArchie Cobbs /* Reuse the original block if appropriate */ 59344a8ff31SArchie Cobbs if (size <= alloc 59444a8ff31SArchie Cobbs && (size > (alloc >> REALLOC_FRACTION) || alloc == MINALLOCSIZE)) 59544a8ff31SArchie Cobbs return (addr); 596847a2a17SPawel Jakub Dawidek #endif /* !DEBUG_REDZONE */ 59744a8ff31SArchie Cobbs 598e4eb384bSBosko Milekic #ifdef DEBUG_MEMGUARD 599*e3813573SMatthew D Fleming remalloc: 600e4eb384bSBosko Milekic #endif 601e4eb384bSBosko Milekic 60244a8ff31SArchie Cobbs /* Allocate a new, bigger (or smaller) block */ 60363a7e0a3SRobert Watson if ((newaddr = malloc(size, mtp, flags)) == NULL) 60444a8ff31SArchie Cobbs return (NULL); 60544a8ff31SArchie Cobbs 60644a8ff31SArchie Cobbs /* Copy over original contents */ 60744a8ff31SArchie Cobbs bcopy(addr, newaddr, min(size, alloc)); 60863a7e0a3SRobert Watson free(addr, mtp); 60944a8ff31SArchie Cobbs return (newaddr); 61044a8ff31SArchie Cobbs } 61144a8ff31SArchie Cobbs 61244a8ff31SArchie Cobbs /* 61344a8ff31SArchie Cobbs * reallocf: same as realloc() but free memory on failure. 61444a8ff31SArchie Cobbs */ 61544a8ff31SArchie Cobbs void * 61663a7e0a3SRobert Watson reallocf(void *addr, unsigned long size, struct malloc_type *mtp, int flags) 61744a8ff31SArchie Cobbs { 61844a8ff31SArchie Cobbs void *mem; 61944a8ff31SArchie Cobbs 62063a7e0a3SRobert Watson if ((mem = realloc(addr, size, mtp, flags)) == NULL) 62163a7e0a3SRobert Watson free(addr, mtp); 62244a8ff31SArchie Cobbs return (mem); 62344a8ff31SArchie Cobbs } 62444a8ff31SArchie Cobbs 62544a8ff31SArchie Cobbs /* 626df8bae1dSRodney W. Grimes * Initialize the kernel memory allocator 627df8bae1dSRodney W. Grimes */ 6282b14f991SJulian Elischer /* ARGSUSED*/ 6292b14f991SJulian Elischer static void 63087efd4d5SRobert Watson kmeminit(void *dummy) 631df8bae1dSRodney W. Grimes { 63260ae52f7SEd Schouten uint8_t indx; 633*e3813573SMatthew D Fleming u_long mem_size, tmp; 6348355f576SJeff Roberson int i; 6358a58a9f6SJohn Dyson 6366008862bSJohn Baldwin mtx_init(&malloc_mtx, "malloc", NULL, MTX_DEF); 63769ef67f9SJason Evans 6388a58a9f6SJohn Dyson /* 6398a58a9f6SJohn Dyson * Try to auto-tune the kernel memory size, so that it is 6408a58a9f6SJohn Dyson * more applicable for a wider range of machine sizes. 6418a58a9f6SJohn Dyson * On an X86, a VM_KMEM_SIZE_SCALE value of 4 is good, while 6428a58a9f6SJohn Dyson * a VM_KMEM_SIZE of 12MB is a fair compromise. The 6438a58a9f6SJohn Dyson * VM_KMEM_SIZE_MAX is dependent on the maximum KVA space 6448a58a9f6SJohn Dyson * available, and on an X86 with a total KVA space of 256MB, 6458a58a9f6SJohn Dyson * try to keep VM_KMEM_SIZE_MAX at 80MB or below. 6468a58a9f6SJohn Dyson * 6478a58a9f6SJohn Dyson * Note that the kmem_map is also used by the zone allocator, 6488a58a9f6SJohn Dyson * so make sure that there is enough space. 6498a58a9f6SJohn Dyson */ 650099a0e58SBosko Milekic vm_kmem_size = VM_KMEM_SIZE + nmbclusters * PAGE_SIZE; 6512feb50bfSAttilio Rao mem_size = cnt.v_page_count; 6528a58a9f6SJohn Dyson 6538a58a9f6SJohn Dyson #if defined(VM_KMEM_SIZE_SCALE) 654479439b4SDag-Erling Smørgrav vm_kmem_size_scale = VM_KMEM_SIZE_SCALE; 6558a58a9f6SJohn Dyson #endif 656479439b4SDag-Erling Smørgrav TUNABLE_INT_FETCH("vm.kmem_size_scale", &vm_kmem_size_scale); 657479439b4SDag-Erling Smørgrav if (vm_kmem_size_scale > 0 && 658479439b4SDag-Erling Smørgrav (mem_size / vm_kmem_size_scale) > (vm_kmem_size / PAGE_SIZE)) 659479439b4SDag-Erling Smørgrav vm_kmem_size = (mem_size / vm_kmem_size_scale) * PAGE_SIZE; 6608a58a9f6SJohn Dyson 6610e5179e4SStephane E. Potvin #if defined(VM_KMEM_SIZE_MIN) 6620e5179e4SStephane E. Potvin vm_kmem_size_min = VM_KMEM_SIZE_MIN; 6630e5179e4SStephane E. Potvin #endif 664b89eaf4eSAlan Cox TUNABLE_ULONG_FETCH("vm.kmem_size_min", &vm_kmem_size_min); 6650e5179e4SStephane E. Potvin if (vm_kmem_size_min > 0 && vm_kmem_size < vm_kmem_size_min) { 6660e5179e4SStephane E. Potvin vm_kmem_size = vm_kmem_size_min; 6670e5179e4SStephane E. Potvin } 6680e5179e4SStephane E. Potvin 6698a58a9f6SJohn Dyson #if defined(VM_KMEM_SIZE_MAX) 670479439b4SDag-Erling Smørgrav vm_kmem_size_max = VM_KMEM_SIZE_MAX; 6718a58a9f6SJohn Dyson #endif 672b89eaf4eSAlan Cox TUNABLE_ULONG_FETCH("vm.kmem_size_max", &vm_kmem_size_max); 673479439b4SDag-Erling Smørgrav if (vm_kmem_size_max > 0 && vm_kmem_size >= vm_kmem_size_max) 674479439b4SDag-Erling Smørgrav vm_kmem_size = vm_kmem_size_max; 6758a58a9f6SJohn Dyson 6768de6e8e1SMike Smith /* Allow final override from the kernel environment */ 677b89eaf4eSAlan Cox TUNABLE_ULONG_FETCH("vm.kmem_size", &vm_kmem_size); 6788de6e8e1SMike Smith 67927b8623fSDavid Greenman /* 68027b8623fSDavid Greenman * Limit kmem virtual size to twice the physical memory. 68127b8623fSDavid Greenman * This allows for kmem map sparseness, but limits the size 68227b8623fSDavid Greenman * to something sane. Be careful to not overflow the 32bit 68327b8623fSDavid Greenman * ints while doing the check. 68427b8623fSDavid Greenman */ 6852feb50bfSAttilio Rao if (((vm_kmem_size / 2) / PAGE_SIZE) > cnt.v_page_count) 6862feb50bfSAttilio Rao vm_kmem_size = 2 * cnt.v_page_count * PAGE_SIZE; 6878a58a9f6SJohn Dyson 68808442f8aSBosko Milekic /* 6896819e13eSAlan Cox * Tune settings based on the kmem map's size at this time. 690347194c1SMike Silbersack */ 691347194c1SMike Silbersack init_param3(vm_kmem_size / PAGE_SIZE); 692347194c1SMike Silbersack 693*e3813573SMatthew D Fleming #ifdef DEBUG_MEMGUARD 694*e3813573SMatthew D Fleming tmp = memguard_fudge(vm_kmem_size, vm_kmem_size_max); 695*e3813573SMatthew D Fleming #else 696*e3813573SMatthew D Fleming tmp = vm_kmem_size; 697*e3813573SMatthew D Fleming #endif 698dc2e1e3fSRobert Watson kmem_map = kmem_suballoc(kernel_map, &kmembase, &kmemlimit, 699*e3813573SMatthew D Fleming tmp, TRUE); 7003075778bSJohn Dyson kmem_map->system_map = 1; 7018355f576SJeff Roberson 702e4eb384bSBosko Milekic #ifdef DEBUG_MEMGUARD 703e4eb384bSBosko Milekic /* 704e4eb384bSBosko Milekic * Initialize MemGuard if support compiled in. MemGuard is a 705e4eb384bSBosko Milekic * replacement allocator used for detecting tamper-after-free 706e4eb384bSBosko Milekic * scenarios as they occur. It is only used for debugging. 707e4eb384bSBosko Milekic */ 708*e3813573SMatthew D Fleming memguard_init(kmem_map); 709e4eb384bSBosko Milekic #endif 710e4eb384bSBosko Milekic 71199571dc3SJeff Roberson uma_startup2(); 7128355f576SJeff Roberson 71363a7e0a3SRobert Watson mt_zone = uma_zcreate("mt_zone", sizeof(struct malloc_type_internal), 71463a7e0a3SRobert Watson #ifdef INVARIANTS 71563a7e0a3SRobert Watson mtrash_ctor, mtrash_dtor, mtrash_init, mtrash_fini, 71663a7e0a3SRobert Watson #else 71763a7e0a3SRobert Watson NULL, NULL, NULL, NULL, 71863a7e0a3SRobert Watson #endif 71963a7e0a3SRobert Watson UMA_ALIGN_PTR, UMA_ZONE_MALLOC); 7206f267175SJeff Roberson for (i = 0, indx = 0; kmemzones[indx].kz_size != 0; indx++) { 7216f267175SJeff Roberson int size = kmemzones[indx].kz_size; 7226f267175SJeff Roberson char *name = kmemzones[indx].kz_name; 723d7854da1SMatthew D Fleming int subzone; 7248355f576SJeff Roberson 725d7854da1SMatthew D Fleming for (subzone = 0; subzone < numzones; subzone++) { 726d7854da1SMatthew D Fleming kmemzones[indx].kz_zone[subzone] = 727d7854da1SMatthew D Fleming uma_zcreate(name, size, 7288efc4effSJeff Roberson #ifdef INVARIANTS 7298f70816cSJeff Roberson mtrash_ctor, mtrash_dtor, mtrash_init, mtrash_fini, 7308efc4effSJeff Roberson #else 7318efc4effSJeff Roberson NULL, NULL, NULL, NULL, 7328efc4effSJeff Roberson #endif 7338efc4effSJeff Roberson UMA_ALIGN_PTR, UMA_ZONE_MALLOC); 734d7854da1SMatthew D Fleming } 7358355f576SJeff Roberson for (;i <= size; i+= KMEM_ZBASE) 7366f267175SJeff Roberson kmemsize[i >> KMEM_ZSHIFT] = indx; 7378355f576SJeff Roberson 738df8bae1dSRodney W. Grimes } 739254c6cb3SPoul-Henning Kamp } 740254c6cb3SPoul-Henning Kamp 741db669378SPeter Wemm void 74287efd4d5SRobert Watson malloc_init(void *data) 743254c6cb3SPoul-Henning Kamp { 74463a7e0a3SRobert Watson struct malloc_type_internal *mtip; 74563a7e0a3SRobert Watson struct malloc_type *mtp; 74663a7e0a3SRobert Watson 7472feb50bfSAttilio Rao KASSERT(cnt.v_page_count != 0, ("malloc_register before vm_init")); 74863a7e0a3SRobert Watson 74963a7e0a3SRobert Watson mtp = data; 750f121baaaSBrian Somers if (mtp->ks_magic != M_MAGIC) 751f121baaaSBrian Somers panic("malloc_init: bad malloc type magic"); 752bb1c7df8SRobert Watson 75363a7e0a3SRobert Watson mtip = uma_zalloc(mt_zone, M_WAITOK | M_ZERO); 75463a7e0a3SRobert Watson mtp->ks_handle = mtip; 755d7854da1SMatthew D Fleming mtip->mti_zone = mtp_get_subzone(mtp->ks_shortdesc); 756254c6cb3SPoul-Henning Kamp 7576f267175SJeff Roberson mtx_lock(&malloc_mtx); 75863a7e0a3SRobert Watson mtp->ks_next = kmemstatistics; 75963a7e0a3SRobert Watson kmemstatistics = mtp; 760cd814b26SRobert Watson kmemcount++; 7616f267175SJeff Roberson mtx_unlock(&malloc_mtx); 762df8bae1dSRodney W. Grimes } 763db669378SPeter Wemm 764db669378SPeter Wemm void 76587efd4d5SRobert Watson malloc_uninit(void *data) 766db669378SPeter Wemm { 76763a7e0a3SRobert Watson struct malloc_type_internal *mtip; 7682a143d5bSPawel Jakub Dawidek struct malloc_type_stats *mtsp; 76963a7e0a3SRobert Watson struct malloc_type *mtp, *temp; 77045d48bdaSPaul Saab uma_slab_t slab; 7712a143d5bSPawel Jakub Dawidek long temp_allocs, temp_bytes; 7722a143d5bSPawel Jakub Dawidek int i; 773db669378SPeter Wemm 77463a7e0a3SRobert Watson mtp = data; 775bb1c7df8SRobert Watson KASSERT(mtp->ks_magic == M_MAGIC, 776bb1c7df8SRobert Watson ("malloc_uninit: bad malloc type magic")); 77763a7e0a3SRobert Watson KASSERT(mtp->ks_handle != NULL, ("malloc_deregister: cookie NULL")); 778bb1c7df8SRobert Watson 7796f267175SJeff Roberson mtx_lock(&malloc_mtx); 78063a7e0a3SRobert Watson mtip = mtp->ks_handle; 78163a7e0a3SRobert Watson mtp->ks_handle = NULL; 78263a7e0a3SRobert Watson if (mtp != kmemstatistics) { 78363a7e0a3SRobert Watson for (temp = kmemstatistics; temp != NULL; 78463a7e0a3SRobert Watson temp = temp->ks_next) { 785f121baaaSBrian Somers if (temp->ks_next == mtp) { 78663a7e0a3SRobert Watson temp->ks_next = mtp->ks_next; 787f121baaaSBrian Somers break; 788db669378SPeter Wemm } 789f121baaaSBrian Somers } 790f121baaaSBrian Somers KASSERT(temp, 791f121baaaSBrian Somers ("malloc_uninit: type '%s' not found", mtp->ks_shortdesc)); 79263a7e0a3SRobert Watson } else 79363a7e0a3SRobert Watson kmemstatistics = mtp->ks_next; 794cd814b26SRobert Watson kmemcount--; 7956f267175SJeff Roberson mtx_unlock(&malloc_mtx); 7962a143d5bSPawel Jakub Dawidek 7972a143d5bSPawel Jakub Dawidek /* 7982a143d5bSPawel Jakub Dawidek * Look for memory leaks. 7992a143d5bSPawel Jakub Dawidek */ 8002a143d5bSPawel Jakub Dawidek temp_allocs = temp_bytes = 0; 8012a143d5bSPawel Jakub Dawidek for (i = 0; i < MAXCPU; i++) { 8022a143d5bSPawel Jakub Dawidek mtsp = &mtip->mti_stats[i]; 8032a143d5bSPawel Jakub Dawidek temp_allocs += mtsp->mts_numallocs; 8042a143d5bSPawel Jakub Dawidek temp_allocs -= mtsp->mts_numfrees; 8052a143d5bSPawel Jakub Dawidek temp_bytes += mtsp->mts_memalloced; 8062a143d5bSPawel Jakub Dawidek temp_bytes -= mtsp->mts_memfreed; 8072a143d5bSPawel Jakub Dawidek } 8082a143d5bSPawel Jakub Dawidek if (temp_allocs > 0 || temp_bytes > 0) { 8092a143d5bSPawel Jakub Dawidek printf("Warning: memory type %s leaked memory on destroy " 8102a143d5bSPawel Jakub Dawidek "(%ld allocations, %ld bytes leaked).\n", mtp->ks_shortdesc, 8112a143d5bSPawel Jakub Dawidek temp_allocs, temp_bytes); 8122a143d5bSPawel Jakub Dawidek } 8132a143d5bSPawel Jakub Dawidek 81445d48bdaSPaul Saab slab = vtoslab((vm_offset_t) mtip & (~UMA_SLAB_MASK)); 81545d48bdaSPaul Saab uma_zfree_arg(mt_zone, mtip, slab); 816db669378SPeter Wemm } 8176f267175SJeff Roberson 818d362c40dSPawel Jakub Dawidek struct malloc_type * 819d362c40dSPawel Jakub Dawidek malloc_desc2type(const char *desc) 820d362c40dSPawel Jakub Dawidek { 821d362c40dSPawel Jakub Dawidek struct malloc_type *mtp; 822d362c40dSPawel Jakub Dawidek 823d362c40dSPawel Jakub Dawidek mtx_assert(&malloc_mtx, MA_OWNED); 824d362c40dSPawel Jakub Dawidek for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) { 825d362c40dSPawel Jakub Dawidek if (strcmp(mtp->ks_shortdesc, desc) == 0) 826d362c40dSPawel Jakub Dawidek return (mtp); 827d362c40dSPawel Jakub Dawidek } 828d362c40dSPawel Jakub Dawidek return (NULL); 829d362c40dSPawel Jakub Dawidek } 830d362c40dSPawel Jakub Dawidek 8316f267175SJeff Roberson static int 832cd814b26SRobert Watson sysctl_kern_malloc_stats(SYSCTL_HANDLER_ARGS) 833cd814b26SRobert Watson { 834cd814b26SRobert Watson struct malloc_type_stream_header mtsh; 835cd814b26SRobert Watson struct malloc_type_internal *mtip; 836cd814b26SRobert Watson struct malloc_type_header mth; 837cd814b26SRobert Watson struct malloc_type *mtp; 838cd814b26SRobert Watson int buflen, count, error, i; 839cd814b26SRobert Watson struct sbuf sbuf; 840cd814b26SRobert Watson char *buffer; 841cd814b26SRobert Watson 842cd814b26SRobert Watson mtx_lock(&malloc_mtx); 843cd814b26SRobert Watson restart: 844cd814b26SRobert Watson mtx_assert(&malloc_mtx, MA_OWNED); 845cd814b26SRobert Watson count = kmemcount; 846cd814b26SRobert Watson mtx_unlock(&malloc_mtx); 847cd814b26SRobert Watson buflen = sizeof(mtsh) + count * (sizeof(mth) + 848cd814b26SRobert Watson sizeof(struct malloc_type_stats) * MAXCPU) + 1; 849cd814b26SRobert Watson buffer = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO); 850cd814b26SRobert Watson mtx_lock(&malloc_mtx); 851cd814b26SRobert Watson if (count < kmemcount) { 852cd814b26SRobert Watson free(buffer, M_TEMP); 853cd814b26SRobert Watson goto restart; 854cd814b26SRobert Watson } 855cd814b26SRobert Watson 856cd814b26SRobert Watson sbuf_new(&sbuf, buffer, buflen, SBUF_FIXEDLEN); 857cd814b26SRobert Watson 858cd814b26SRobert Watson /* 859cd814b26SRobert Watson * Insert stream header. 860cd814b26SRobert Watson */ 861cd814b26SRobert Watson bzero(&mtsh, sizeof(mtsh)); 862cd814b26SRobert Watson mtsh.mtsh_version = MALLOC_TYPE_STREAM_VERSION; 863cd814b26SRobert Watson mtsh.mtsh_maxcpus = MAXCPU; 864cd814b26SRobert Watson mtsh.mtsh_count = kmemcount; 865cd814b26SRobert Watson if (sbuf_bcat(&sbuf, &mtsh, sizeof(mtsh)) < 0) { 866cd814b26SRobert Watson mtx_unlock(&malloc_mtx); 867cd814b26SRobert Watson error = ENOMEM; 868cd814b26SRobert Watson goto out; 869cd814b26SRobert Watson } 870cd814b26SRobert Watson 871cd814b26SRobert Watson /* 872cd814b26SRobert Watson * Insert alternating sequence of type headers and type statistics. 873cd814b26SRobert Watson */ 874cd814b26SRobert Watson for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) { 875cd814b26SRobert Watson mtip = (struct malloc_type_internal *)mtp->ks_handle; 876cd814b26SRobert Watson 877cd814b26SRobert Watson /* 878cd814b26SRobert Watson * Insert type header. 879cd814b26SRobert Watson */ 880cd814b26SRobert Watson bzero(&mth, sizeof(mth)); 881cd814b26SRobert Watson strlcpy(mth.mth_name, mtp->ks_shortdesc, MALLOC_MAX_NAME); 882cd814b26SRobert Watson if (sbuf_bcat(&sbuf, &mth, sizeof(mth)) < 0) { 883cd814b26SRobert Watson mtx_unlock(&malloc_mtx); 884cd814b26SRobert Watson error = ENOMEM; 885cd814b26SRobert Watson goto out; 886cd814b26SRobert Watson } 887cd814b26SRobert Watson 888cd814b26SRobert Watson /* 889cd814b26SRobert Watson * Insert type statistics for each CPU. 890cd814b26SRobert Watson */ 891cd814b26SRobert Watson for (i = 0; i < MAXCPU; i++) { 892cd814b26SRobert Watson if (sbuf_bcat(&sbuf, &mtip->mti_stats[i], 893cd814b26SRobert Watson sizeof(mtip->mti_stats[i])) < 0) { 894cd814b26SRobert Watson mtx_unlock(&malloc_mtx); 895cd814b26SRobert Watson error = ENOMEM; 896cd814b26SRobert Watson goto out; 897cd814b26SRobert Watson } 898cd814b26SRobert Watson } 899cd814b26SRobert Watson } 900cd814b26SRobert Watson mtx_unlock(&malloc_mtx); 901cd814b26SRobert Watson sbuf_finish(&sbuf); 902cd814b26SRobert Watson error = SYSCTL_OUT(req, sbuf_data(&sbuf), sbuf_len(&sbuf)); 903cd814b26SRobert Watson out: 904cd814b26SRobert Watson sbuf_delete(&sbuf); 905cd814b26SRobert Watson free(buffer, M_TEMP); 906cd814b26SRobert Watson return (error); 907cd814b26SRobert Watson } 908cd814b26SRobert Watson 909cd814b26SRobert Watson SYSCTL_PROC(_kern, OID_AUTO, malloc_stats, CTLFLAG_RD|CTLTYPE_STRUCT, 910cd814b26SRobert Watson 0, 0, sysctl_kern_malloc_stats, "s,malloc_type_ustats", 911cd814b26SRobert Watson "Return malloc types"); 912cd814b26SRobert Watson 913cd814b26SRobert Watson SYSCTL_INT(_kern, OID_AUTO, malloc_count, CTLFLAG_RD, &kmemcount, 0, 914cd814b26SRobert Watson "Count of kernel malloc types"); 915cd814b26SRobert Watson 91691dd776cSJohn Birrell void 91791dd776cSJohn Birrell malloc_type_list(malloc_type_list_func_t *func, void *arg) 91891dd776cSJohn Birrell { 91991dd776cSJohn Birrell struct malloc_type *mtp, **bufmtp; 92091dd776cSJohn Birrell int count, i; 92191dd776cSJohn Birrell size_t buflen; 92291dd776cSJohn Birrell 92391dd776cSJohn Birrell mtx_lock(&malloc_mtx); 92491dd776cSJohn Birrell restart: 92591dd776cSJohn Birrell mtx_assert(&malloc_mtx, MA_OWNED); 92691dd776cSJohn Birrell count = kmemcount; 92791dd776cSJohn Birrell mtx_unlock(&malloc_mtx); 92891dd776cSJohn Birrell 92991dd776cSJohn Birrell buflen = sizeof(struct malloc_type *) * count; 93091dd776cSJohn Birrell bufmtp = malloc(buflen, M_TEMP, M_WAITOK); 93191dd776cSJohn Birrell 93291dd776cSJohn Birrell mtx_lock(&malloc_mtx); 93391dd776cSJohn Birrell 93491dd776cSJohn Birrell if (count < kmemcount) { 93591dd776cSJohn Birrell free(bufmtp, M_TEMP); 93691dd776cSJohn Birrell goto restart; 93791dd776cSJohn Birrell } 93891dd776cSJohn Birrell 93991dd776cSJohn Birrell for (mtp = kmemstatistics, i = 0; mtp != NULL; mtp = mtp->ks_next, i++) 94091dd776cSJohn Birrell bufmtp[i] = mtp; 94191dd776cSJohn Birrell 94291dd776cSJohn Birrell mtx_unlock(&malloc_mtx); 94391dd776cSJohn Birrell 94491dd776cSJohn Birrell for (i = 0; i < count; i++) 94591dd776cSJohn Birrell (func)(bufmtp[i], arg); 94691dd776cSJohn Birrell 94791dd776cSJohn Birrell free(bufmtp, M_TEMP); 94891dd776cSJohn Birrell } 94991dd776cSJohn Birrell 950909ed16cSRobert Watson #ifdef DDB 951909ed16cSRobert Watson DB_SHOW_COMMAND(malloc, db_show_malloc) 952909ed16cSRobert Watson { 953909ed16cSRobert Watson struct malloc_type_internal *mtip; 954909ed16cSRobert Watson struct malloc_type *mtp; 95560ae52f7SEd Schouten uint64_t allocs, frees; 95660ae52f7SEd Schouten uint64_t alloced, freed; 957909ed16cSRobert Watson int i; 958909ed16cSRobert Watson 95924076d13SRobert Watson db_printf("%18s %12s %12s %12s\n", "Type", "InUse", "MemUse", 96024076d13SRobert Watson "Requests"); 961909ed16cSRobert Watson for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) { 962909ed16cSRobert Watson mtip = (struct malloc_type_internal *)mtp->ks_handle; 963909ed16cSRobert Watson allocs = 0; 964909ed16cSRobert Watson frees = 0; 96524076d13SRobert Watson alloced = 0; 96624076d13SRobert Watson freed = 0; 967909ed16cSRobert Watson for (i = 0; i < MAXCPU; i++) { 968909ed16cSRobert Watson allocs += mtip->mti_stats[i].mts_numallocs; 969909ed16cSRobert Watson frees += mtip->mti_stats[i].mts_numfrees; 97024076d13SRobert Watson alloced += mtip->mti_stats[i].mts_memalloced; 97124076d13SRobert Watson freed += mtip->mti_stats[i].mts_memfreed; 972909ed16cSRobert Watson } 97324076d13SRobert Watson db_printf("%18s %12ju %12juK %12ju\n", 97424076d13SRobert Watson mtp->ks_shortdesc, allocs - frees, 97524076d13SRobert Watson (alloced - freed + 1023) / 1024, allocs); 976909ed16cSRobert Watson } 977909ed16cSRobert Watson } 978d7854da1SMatthew D Fleming 979d7854da1SMatthew D Fleming #if MALLOC_DEBUG_MAXZONES > 1 980d7854da1SMatthew D Fleming DB_SHOW_COMMAND(multizone_matches, db_show_multizone_matches) 981d7854da1SMatthew D Fleming { 982d7854da1SMatthew D Fleming struct malloc_type_internal *mtip; 983d7854da1SMatthew D Fleming struct malloc_type *mtp; 984d7854da1SMatthew D Fleming u_int subzone; 985d7854da1SMatthew D Fleming 986d7854da1SMatthew D Fleming if (!have_addr) { 987d7854da1SMatthew D Fleming db_printf("Usage: show multizone_matches <malloc type/addr>\n"); 988d7854da1SMatthew D Fleming return; 989d7854da1SMatthew D Fleming } 990d7854da1SMatthew D Fleming mtp = (void *)addr; 991d7854da1SMatthew D Fleming if (mtp->ks_magic != M_MAGIC) { 992d7854da1SMatthew D Fleming db_printf("Magic %lx does not match expected %x\n", 993d7854da1SMatthew D Fleming mtp->ks_magic, M_MAGIC); 994d7854da1SMatthew D Fleming return; 995d7854da1SMatthew D Fleming } 996d7854da1SMatthew D Fleming 997d7854da1SMatthew D Fleming mtip = mtp->ks_handle; 998d7854da1SMatthew D Fleming subzone = mtip->mti_zone; 999d7854da1SMatthew D Fleming 1000d7854da1SMatthew D Fleming for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) { 1001d7854da1SMatthew D Fleming mtip = mtp->ks_handle; 1002d7854da1SMatthew D Fleming if (mtip->mti_zone != subzone) 1003d7854da1SMatthew D Fleming continue; 1004d7854da1SMatthew D Fleming db_printf("%s\n", mtp->ks_shortdesc); 1005d7854da1SMatthew D Fleming } 1006d7854da1SMatthew D Fleming } 1007d7854da1SMatthew D Fleming #endif /* MALLOC_DEBUG_MAXZONES > 1 */ 1008d7854da1SMatthew D Fleming #endif /* DDB */ 1009909ed16cSRobert Watson 10105e914b96SJeff Roberson #ifdef MALLOC_PROFILE 10115e914b96SJeff Roberson 10125e914b96SJeff Roberson static int 10135e914b96SJeff Roberson sysctl_kern_mprof(SYSCTL_HANDLER_ARGS) 10145e914b96SJeff Roberson { 10155e914b96SJeff Roberson int linesize = 64; 101663a7e0a3SRobert Watson struct sbuf sbuf; 10175e914b96SJeff Roberson uint64_t count; 10185e914b96SJeff Roberson uint64_t waste; 10195e914b96SJeff Roberson uint64_t mem; 10205e914b96SJeff Roberson int bufsize; 10215e914b96SJeff Roberson int error; 10225e914b96SJeff Roberson char *buf; 10235e914b96SJeff Roberson int rsize; 10245e914b96SJeff Roberson int size; 10255e914b96SJeff Roberson int i; 10265e914b96SJeff Roberson 10275e914b96SJeff Roberson bufsize = linesize * (KMEM_ZSIZE + 1); 10285e914b96SJeff Roberson bufsize += 128; /* For the stats line */ 10295e914b96SJeff Roberson bufsize += 128; /* For the banner line */ 10305e914b96SJeff Roberson waste = 0; 10315e914b96SJeff Roberson mem = 0; 10325e914b96SJeff Roberson 103363a7e0a3SRobert Watson buf = malloc(bufsize, M_TEMP, M_WAITOK|M_ZERO); 103463a7e0a3SRobert Watson sbuf_new(&sbuf, buf, bufsize, SBUF_FIXEDLEN); 103563a7e0a3SRobert Watson sbuf_printf(&sbuf, 10365e914b96SJeff Roberson "\n Size Requests Real Size\n"); 10375e914b96SJeff Roberson for (i = 0; i < KMEM_ZSIZE; i++) { 10385e914b96SJeff Roberson size = i << KMEM_ZSHIFT; 10395e914b96SJeff Roberson rsize = kmemzones[kmemsize[i]].kz_size; 10405e914b96SJeff Roberson count = (long long unsigned)krequests[i]; 10415e914b96SJeff Roberson 104263a7e0a3SRobert Watson sbuf_printf(&sbuf, "%6d%28llu%11d\n", size, 104363a7e0a3SRobert Watson (unsigned long long)count, rsize); 10445e914b96SJeff Roberson 10455e914b96SJeff Roberson if ((rsize * count) > (size * count)) 10465e914b96SJeff Roberson waste += (rsize * count) - (size * count); 10475e914b96SJeff Roberson mem += (rsize * count); 10485e914b96SJeff Roberson } 104963a7e0a3SRobert Watson sbuf_printf(&sbuf, 10505e914b96SJeff Roberson "\nTotal memory used:\t%30llu\nTotal Memory wasted:\t%30llu\n", 10515e914b96SJeff Roberson (unsigned long long)mem, (unsigned long long)waste); 105263a7e0a3SRobert Watson sbuf_finish(&sbuf); 10535e914b96SJeff Roberson 105463a7e0a3SRobert Watson error = SYSCTL_OUT(req, sbuf_data(&sbuf), sbuf_len(&sbuf)); 10555e914b96SJeff Roberson 105663a7e0a3SRobert Watson sbuf_delete(&sbuf); 10575e914b96SJeff Roberson free(buf, M_TEMP); 10585e914b96SJeff Roberson return (error); 10595e914b96SJeff Roberson } 10605e914b96SJeff Roberson 10615e914b96SJeff Roberson SYSCTL_OID(_kern, OID_AUTO, mprof, CTLTYPE_STRING|CTLFLAG_RD, 10625e914b96SJeff Roberson NULL, 0, sysctl_kern_mprof, "A", "Malloc Profiling"); 10635e914b96SJeff Roberson #endif /* MALLOC_PROFILE */ 1064