19454b2d8SWarner Losh /*- 2df8bae1dSRodney W. Grimes * Copyright (c) 1987, 1991, 1993 363a7e0a3SRobert Watson * The Regents of the University of California. 463a7e0a3SRobert Watson * Copyright (c) 2005 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 34677b542eSDavid E. O'Brien #include <sys/cdefs.h> 35677b542eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 36677b542eSDavid E. O'Brien 37909ed16cSRobert Watson #include "opt_ddb.h" 388a58a9f6SJohn Dyson #include "opt_vm.h" 398a58a9f6SJohn Dyson 40df8bae1dSRodney W. Grimes #include <sys/param.h> 4126f9a767SRodney W. Grimes #include <sys/systm.h> 422d50560aSMarcel Moolenaar #include <sys/kdb.h> 43df8bae1dSRodney W. Grimes #include <sys/kernel.h> 44fb919e4dSMark Murray #include <sys/lock.h> 45df8bae1dSRodney W. Grimes #include <sys/malloc.h> 4654e7152cSDavid Greenman #include <sys/mbuf.h> 47eec258d2SJohn Baldwin #include <sys/mutex.h> 48efeaf95aSDavid Greenman #include <sys/vmmeter.h> 49a448b62aSJake Burkholder #include <sys/proc.h> 5063a7e0a3SRobert Watson #include <sys/sbuf.h> 516f267175SJeff Roberson #include <sys/sysctl.h> 521fb14a47SPoul-Henning Kamp #include <sys/time.h> 539a02e8c6SJason Evans 54df8bae1dSRodney W. Grimes #include <vm/vm.h> 5599571dc3SJeff Roberson #include <vm/pmap.h> 56efeaf95aSDavid Greenman #include <vm/vm_param.h> 57df8bae1dSRodney W. Grimes #include <vm/vm_kern.h> 58efeaf95aSDavid Greenman #include <vm/vm_extern.h> 593075778bSJohn Dyson #include <vm/vm_map.h> 6099571dc3SJeff Roberson #include <vm/vm_page.h> 618355f576SJeff Roberson #include <vm/uma.h> 628355f576SJeff Roberson #include <vm/uma_int.h> 638efc4effSJeff Roberson #include <vm/uma_dbg.h> 64df8bae1dSRodney W. Grimes 65e4eb384bSBosko Milekic #ifdef DEBUG_MEMGUARD 66e4eb384bSBosko Milekic #include <vm/memguard.h> 67e4eb384bSBosko Milekic #endif 68e4eb384bSBosko Milekic 69984982d6SPoul-Henning Kamp #if defined(INVARIANTS) && defined(__i386__) 70984982d6SPoul-Henning Kamp #include <machine/cpu.h> 71984982d6SPoul-Henning Kamp #endif 72984982d6SPoul-Henning Kamp 73909ed16cSRobert Watson #include <ddb/ddb.h> 74909ed16cSRobert Watson 7544a8ff31SArchie Cobbs /* 7644a8ff31SArchie Cobbs * When realloc() is called, if the new size is sufficiently smaller than 7744a8ff31SArchie Cobbs * the old size, realloc() will allocate a new, smaller block to avoid 7844a8ff31SArchie Cobbs * wasting memory. 'Sufficiently smaller' is defined as: newsize <= 7944a8ff31SArchie Cobbs * oldsize / 2^n, where REALLOC_FRACTION defines the value of 'n'. 8044a8ff31SArchie Cobbs */ 8144a8ff31SArchie Cobbs #ifndef REALLOC_FRACTION 8244a8ff31SArchie Cobbs #define REALLOC_FRACTION 1 /* new block if <= half the size */ 8344a8ff31SArchie Cobbs #endif 8444a8ff31SArchie Cobbs 853b6fb885SPoul-Henning Kamp MALLOC_DEFINE(M_CACHE, "cache", "Various Dynamically allocated caches"); 869ef246c6SBruce Evans MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory"); 879ef246c6SBruce Evans MALLOC_DEFINE(M_TEMP, "temp", "misc temporary data buffers"); 889ef246c6SBruce Evans 8982cd038dSYoshinobu Inoue MALLOC_DEFINE(M_IP6OPT, "ip6opt", "IPv6 options"); 9082cd038dSYoshinobu Inoue MALLOC_DEFINE(M_IP6NDP, "ip6ndp", "IPv6 Neighbor Discovery"); 9182cd038dSYoshinobu Inoue 924d77a549SAlfred Perlstein static void kmeminit(void *); 932b14f991SJulian Elischer SYSINIT(kmem, SI_SUB_KMEM, SI_ORDER_FIRST, kmeminit, NULL) 942b14f991SJulian Elischer 95a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_FREE, "free", "should be on free list"); 96a1c995b6SPoul-Henning Kamp 97db669378SPeter Wemm static struct malloc_type *kmemstatistics; 98254c6cb3SPoul-Henning Kamp static char *kmembase; 99043a2f3bSBruce Evans static char *kmemlimit; 100cd814b26SRobert Watson static int kmemcount; 1011f6889a1SMatthew Dillon 1028355f576SJeff Roberson #define KMEM_ZSHIFT 4 1038355f576SJeff Roberson #define KMEM_ZBASE 16 1048355f576SJeff Roberson #define KMEM_ZMASK (KMEM_ZBASE - 1) 1058355f576SJeff Roberson 1069fb535deSJeff Roberson #define KMEM_ZMAX PAGE_SIZE 1078355f576SJeff Roberson #define KMEM_ZSIZE (KMEM_ZMAX >> KMEM_ZSHIFT) 1086f267175SJeff Roberson static u_int8_t kmemsize[KMEM_ZSIZE + 1]; 1096f267175SJeff Roberson 1108355f576SJeff Roberson /* These won't be powers of two for long */ 1118355f576SJeff Roberson struct { 1126f267175SJeff Roberson int kz_size; 1136f267175SJeff Roberson char *kz_name; 1146f267175SJeff Roberson uma_zone_t kz_zone; 1156f267175SJeff Roberson } kmemzones[] = { 1166f267175SJeff Roberson {16, "16", NULL}, 1176f267175SJeff Roberson {32, "32", NULL}, 1186f267175SJeff Roberson {64, "64", NULL}, 1196f267175SJeff Roberson {128, "128", NULL}, 1206f267175SJeff Roberson {256, "256", NULL}, 1216f267175SJeff Roberson {512, "512", NULL}, 1226f267175SJeff Roberson {1024, "1024", NULL}, 1236f267175SJeff Roberson {2048, "2048", NULL}, 1246f267175SJeff Roberson {4096, "4096", NULL}, 1259fb535deSJeff Roberson #if PAGE_SIZE > 4096 1266f267175SJeff Roberson {8192, "8192", NULL}, 1279fb535deSJeff Roberson #if PAGE_SIZE > 8192 12843a7c4e9SRobert Watson {16384, "16384", NULL}, 1299fb535deSJeff Roberson #if PAGE_SIZE > 16384 130bd796eb2SRobert Watson {32768, "32768", NULL}, 1319fb535deSJeff Roberson #if PAGE_SIZE > 32768 132bd796eb2SRobert Watson {65536, "65536", NULL}, 1339fb535deSJeff Roberson #if PAGE_SIZE > 65536 1349fb535deSJeff Roberson #error "Unsupported PAGE_SIZE" 1359fb535deSJeff Roberson #endif /* 65536 */ 1369fb535deSJeff Roberson #endif /* 32768 */ 1379fb535deSJeff Roberson #endif /* 16384 */ 1389fb535deSJeff Roberson #endif /* 8192 */ 1399fb535deSJeff Roberson #endif /* 4096 */ 1408355f576SJeff Roberson {0, NULL}, 1418355f576SJeff Roberson }; 1428355f576SJeff Roberson 14363a7e0a3SRobert Watson static uma_zone_t mt_zone; 14463a7e0a3SRobert Watson 1456f267175SJeff Roberson u_int vm_kmem_size; 14684344f9fSDag-Erling Smørgrav SYSCTL_UINT(_vm, OID_AUTO, kmem_size, CTLFLAG_RD, &vm_kmem_size, 0, 14784344f9fSDag-Erling Smørgrav "Size of kernel memory"); 1485a34a9f0SJeff Roberson 149479439b4SDag-Erling Smørgrav u_int vm_kmem_size_max; 150479439b4SDag-Erling Smørgrav SYSCTL_UINT(_vm, OID_AUTO, kmem_size_max, CTLFLAG_RD, &vm_kmem_size_max, 0, 151479439b4SDag-Erling Smørgrav "Maximum size of kernel memory"); 152479439b4SDag-Erling Smørgrav 153479439b4SDag-Erling Smørgrav u_int vm_kmem_size_scale; 154479439b4SDag-Erling Smørgrav SYSCTL_UINT(_vm, OID_AUTO, kmem_size_scale, CTLFLAG_RD, &vm_kmem_size_scale, 0, 155479439b4SDag-Erling Smørgrav "Scale factor for kernel memory size"); 156479439b4SDag-Erling Smørgrav 1575a34a9f0SJeff Roberson /* 15899571dc3SJeff Roberson * The malloc_mtx protects the kmemstatistics linked list. 1595a34a9f0SJeff Roberson */ 1605a34a9f0SJeff Roberson 1615a34a9f0SJeff Roberson struct mtx malloc_mtx; 16269ef67f9SJason Evans 1635e914b96SJeff Roberson #ifdef MALLOC_PROFILE 1645e914b96SJeff Roberson uint64_t krequests[KMEM_ZSIZE + 1]; 1656f267175SJeff Roberson 1665e914b96SJeff Roberson static int sysctl_kern_mprof(SYSCTL_HANDLER_ARGS); 1675e914b96SJeff Roberson #endif 1685e914b96SJeff Roberson 1695e914b96SJeff Roberson static int sysctl_kern_malloc(SYSCTL_HANDLER_ARGS); 170cd814b26SRobert Watson static int sysctl_kern_malloc_stats(SYSCTL_HANDLER_ARGS); 171df8bae1dSRodney W. Grimes 1721fb14a47SPoul-Henning Kamp /* time_uptime of last malloc(9) failure */ 1731fb14a47SPoul-Henning Kamp static time_t t_malloc_fail; 1741fb14a47SPoul-Henning Kamp 175eae870cdSRobert Watson #ifdef MALLOC_MAKE_FAILURES 176eae870cdSRobert Watson /* 177eae870cdSRobert Watson * Causes malloc failures every (n) mallocs with M_NOWAIT. If set to 0, 178eae870cdSRobert Watson * doesn't cause failures. 179eae870cdSRobert Watson */ 180eae870cdSRobert Watson SYSCTL_NODE(_debug, OID_AUTO, malloc, CTLFLAG_RD, 0, 181eae870cdSRobert Watson "Kernel malloc debugging options"); 182eae870cdSRobert Watson 183eae870cdSRobert Watson static int malloc_failure_rate; 184eae870cdSRobert Watson static int malloc_nowait_count; 185eae870cdSRobert Watson static int malloc_failure_count; 186eae870cdSRobert Watson SYSCTL_INT(_debug_malloc, OID_AUTO, failure_rate, CTLFLAG_RW, 187eae870cdSRobert Watson &malloc_failure_rate, 0, "Every (n) mallocs with M_NOWAIT will fail"); 188f2538508SRobert Watson TUNABLE_INT("debug.malloc.failure_rate", &malloc_failure_rate); 189eae870cdSRobert Watson SYSCTL_INT(_debug_malloc, OID_AUTO, failure_count, CTLFLAG_RD, 190eae870cdSRobert Watson &malloc_failure_count, 0, "Number of imposed M_NOWAIT malloc failures"); 191eae870cdSRobert Watson #endif 192eae870cdSRobert Watson 1931fb14a47SPoul-Henning Kamp int 1941fb14a47SPoul-Henning Kamp malloc_last_fail(void) 1951fb14a47SPoul-Henning Kamp { 1961fb14a47SPoul-Henning Kamp 1971fb14a47SPoul-Henning Kamp return (time_uptime - t_malloc_fail); 1981fb14a47SPoul-Henning Kamp } 1991fb14a47SPoul-Henning Kamp 200df8bae1dSRodney W. Grimes /* 2014362fadaSBrian Feldman * Add this to the informational malloc_type bucket. 2024362fadaSBrian Feldman */ 2034362fadaSBrian Feldman static void 20463a7e0a3SRobert Watson malloc_type_zone_allocated(struct malloc_type *mtp, unsigned long size, 2054362fadaSBrian Feldman int zindx) 2064362fadaSBrian Feldman { 20763a7e0a3SRobert Watson struct malloc_type_internal *mtip; 20863a7e0a3SRobert Watson struct malloc_type_stats *mtsp; 20963a7e0a3SRobert Watson 21063a7e0a3SRobert Watson critical_enter(); 21163a7e0a3SRobert Watson mtip = mtp->ks_handle; 21263a7e0a3SRobert Watson mtsp = &mtip->mti_stats[curcpu]; 21373864adbSPawel Jakub Dawidek if (size > 0) { 21463a7e0a3SRobert Watson mtsp->mts_memalloced += size; 21563a7e0a3SRobert Watson mtsp->mts_numallocs++; 21673864adbSPawel Jakub Dawidek } 2174362fadaSBrian Feldman if (zindx != -1) 21863a7e0a3SRobert Watson mtsp->mts_size |= 1 << zindx; 21963a7e0a3SRobert Watson critical_exit(); 2204362fadaSBrian Feldman } 2214362fadaSBrian Feldman 2224362fadaSBrian Feldman void 22363a7e0a3SRobert Watson malloc_type_allocated(struct malloc_type *mtp, unsigned long size) 2244362fadaSBrian Feldman { 22563a7e0a3SRobert Watson 22673864adbSPawel Jakub Dawidek if (size > 0) 22763a7e0a3SRobert Watson malloc_type_zone_allocated(mtp, size, -1); 2284362fadaSBrian Feldman } 2294362fadaSBrian Feldman 2304362fadaSBrian Feldman /* 2314362fadaSBrian Feldman * Remove this allocation from the informational malloc_type bucket. 2324362fadaSBrian Feldman */ 2334362fadaSBrian Feldman void 23463a7e0a3SRobert Watson malloc_type_freed(struct malloc_type *mtp, unsigned long size) 2354362fadaSBrian Feldman { 23663a7e0a3SRobert Watson struct malloc_type_internal *mtip; 23763a7e0a3SRobert Watson struct malloc_type_stats *mtsp; 23863a7e0a3SRobert Watson 23963a7e0a3SRobert Watson critical_enter(); 24063a7e0a3SRobert Watson mtip = mtp->ks_handle; 24163a7e0a3SRobert Watson mtsp = &mtip->mti_stats[curcpu]; 24263a7e0a3SRobert Watson mtsp->mts_memfreed += size; 24363a7e0a3SRobert Watson mtsp->mts_numfrees++; 24463a7e0a3SRobert Watson critical_exit(); 2454362fadaSBrian Feldman } 2464362fadaSBrian Feldman 2474362fadaSBrian Feldman /* 2481c7c3c6aSMatthew Dillon * malloc: 2491c7c3c6aSMatthew Dillon * 2501c7c3c6aSMatthew Dillon * Allocate a block of memory. 2511c7c3c6aSMatthew Dillon * 2521c7c3c6aSMatthew Dillon * If M_NOWAIT is set, this routine will not block and return NULL if 2531c7c3c6aSMatthew Dillon * the allocation fails. 254df8bae1dSRodney W. Grimes */ 255df8bae1dSRodney W. Grimes void * 25663a7e0a3SRobert Watson malloc(unsigned long size, struct malloc_type *mtp, int flags) 257df8bae1dSRodney W. Grimes { 2586f267175SJeff Roberson int indx; 2598355f576SJeff Roberson caddr_t va; 2608355f576SJeff Roberson uma_zone_t zone; 261099a0e58SBosko Milekic uma_keg_t keg; 2624db4f5c8SPoul-Henning Kamp #ifdef DIAGNOSTIC 2634db4f5c8SPoul-Henning Kamp unsigned long osize = size; 2644db4f5c8SPoul-Henning Kamp #endif 265df8bae1dSRodney W. Grimes 266194a0abfSPoul-Henning Kamp #ifdef INVARIANTS 267d3c11994SPoul-Henning Kamp /* 26823198357SRuslan Ermilov * Check that exactly one of M_WAITOK or M_NOWAIT is specified. 269d3c11994SPoul-Henning Kamp */ 27023198357SRuslan Ermilov indx = flags & (M_WAITOK | M_NOWAIT); 271d3c11994SPoul-Henning Kamp if (indx != M_NOWAIT && indx != M_WAITOK) { 272d3c11994SPoul-Henning Kamp static struct timeval lasterr; 273d3c11994SPoul-Henning Kamp static int curerr, once; 274d3c11994SPoul-Henning Kamp if (once == 0 && ppsratecheck(&lasterr, &curerr, 1)) { 275d3c11994SPoul-Henning Kamp printf("Bad malloc flags: %x\n", indx); 2762d50560aSMarcel Moolenaar kdb_backtrace(); 277d3c11994SPoul-Henning Kamp flags |= M_WAITOK; 278d3c11994SPoul-Henning Kamp once++; 279d3c11994SPoul-Henning Kamp } 280d3c11994SPoul-Henning Kamp } 281194a0abfSPoul-Henning Kamp #endif 282708da94eSPoul-Henning Kamp #if 0 283708da94eSPoul-Henning Kamp if (size == 0) 2842d50560aSMarcel Moolenaar kdb_enter("zero size malloc"); 285708da94eSPoul-Henning Kamp #endif 286eae870cdSRobert Watson #ifdef MALLOC_MAKE_FAILURES 287eae870cdSRobert Watson if ((flags & M_NOWAIT) && (malloc_failure_rate != 0)) { 288eae870cdSRobert Watson atomic_add_int(&malloc_nowait_count, 1); 289eae870cdSRobert Watson if ((malloc_nowait_count % malloc_failure_rate) == 0) { 290eae870cdSRobert Watson atomic_add_int(&malloc_failure_count, 1); 2913f6ee876SPoul-Henning Kamp t_malloc_fail = time_uptime; 292eae870cdSRobert Watson return (NULL); 293eae870cdSRobert Watson } 294eae870cdSRobert Watson } 295eae870cdSRobert Watson #endif 296d3c11994SPoul-Henning Kamp if (flags & M_WAITOK) 297b40ce416SJulian Elischer KASSERT(curthread->td_intr_nesting_level == 0, 298a163d034SWarner Losh ("malloc(M_WAITOK) in interrupt context")); 299e4eb384bSBosko Milekic 300e4eb384bSBosko Milekic #ifdef DEBUG_MEMGUARD 301d362c40dSPawel Jakub Dawidek if (memguard_cmp(mtp)) 302e4eb384bSBosko Milekic return memguard_alloc(size, flags); 303e4eb384bSBosko Milekic #endif 304e4eb384bSBosko Milekic 3058355f576SJeff Roberson if (size <= KMEM_ZMAX) { 3066f267175SJeff Roberson if (size & KMEM_ZMASK) 3076f267175SJeff Roberson size = (size & ~KMEM_ZMASK) + KMEM_ZBASE; 3086f267175SJeff Roberson indx = kmemsize[size >> KMEM_ZSHIFT]; 3096f267175SJeff Roberson zone = kmemzones[indx].kz_zone; 310099a0e58SBosko Milekic keg = zone->uz_keg; 3116f267175SJeff Roberson #ifdef MALLOC_PROFILE 3126f267175SJeff Roberson krequests[size >> KMEM_ZSHIFT]++; 3136f267175SJeff Roberson #endif 3148355f576SJeff Roberson va = uma_zalloc(zone, flags); 3154362fadaSBrian Feldman if (va != NULL) 316099a0e58SBosko Milekic size = keg->uk_size; 31763a7e0a3SRobert Watson malloc_type_zone_allocated(mtp, va == NULL ? 0 : size, indx); 3188355f576SJeff Roberson } else { 3196f267175SJeff Roberson size = roundup(size, PAGE_SIZE); 3208355f576SJeff Roberson zone = NULL; 321099a0e58SBosko Milekic keg = NULL; 3228355f576SJeff Roberson va = uma_large_malloc(size, flags); 32363a7e0a3SRobert Watson malloc_type_allocated(mtp, va == NULL ? 0 : size); 324df8bae1dSRodney W. Grimes } 3251282e9acSPoul-Henning Kamp if (flags & M_WAITOK) 326a163d034SWarner Losh KASSERT(va != NULL, ("malloc(M_WAITOK) returned NULL")); 3271282e9acSPoul-Henning Kamp else if (va == NULL) 3281fb14a47SPoul-Henning Kamp t_malloc_fail = time_uptime; 3294db4f5c8SPoul-Henning Kamp #ifdef DIAGNOSTIC 3301282e9acSPoul-Henning Kamp if (va != NULL && !(flags & M_ZERO)) { 3314db4f5c8SPoul-Henning Kamp memset(va, 0x70, osize); 3324db4f5c8SPoul-Henning Kamp } 3334db4f5c8SPoul-Henning Kamp #endif 334df8bae1dSRodney W. Grimes return ((void *) va); 335df8bae1dSRodney W. Grimes } 336df8bae1dSRodney W. Grimes 337df8bae1dSRodney W. Grimes /* 3381c7c3c6aSMatthew Dillon * free: 3391c7c3c6aSMatthew Dillon * 340df8bae1dSRodney W. Grimes * Free a block of memory allocated by malloc. 3411c7c3c6aSMatthew Dillon * 3421c7c3c6aSMatthew Dillon * This routine may not block. 343df8bae1dSRodney W. Grimes */ 344df8bae1dSRodney W. Grimes void 34563a7e0a3SRobert Watson free(void *addr, struct malloc_type *mtp) 346df8bae1dSRodney W. Grimes { 34799571dc3SJeff Roberson uma_slab_t slab; 34899571dc3SJeff Roberson u_long size; 349254c6cb3SPoul-Henning Kamp 35044a8ff31SArchie Cobbs /* free(NULL, ...) does nothing */ 35144a8ff31SArchie Cobbs if (addr == NULL) 35244a8ff31SArchie Cobbs return; 35344a8ff31SArchie Cobbs 354e4eb384bSBosko Milekic #ifdef DEBUG_MEMGUARD 355d362c40dSPawel Jakub Dawidek if (memguard_cmp(mtp)) { 356e4eb384bSBosko Milekic memguard_free(addr); 357e4eb384bSBosko Milekic return; 358e4eb384bSBosko Milekic } 359e4eb384bSBosko Milekic #endif 360e4eb384bSBosko Milekic 3618355f576SJeff Roberson size = 0; 36269ef67f9SJason Evans 36399571dc3SJeff Roberson slab = vtoslab((vm_offset_t)addr & (~UMA_SLAB_MASK)); 3648355f576SJeff Roberson 3658355f576SJeff Roberson if (slab == NULL) 3666f267175SJeff Roberson panic("free: address %p(%p) has not been allocated.\n", 36799571dc3SJeff Roberson addr, (void *)((u_long)addr & (~UMA_SLAB_MASK))); 36899571dc3SJeff Roberson 3698355f576SJeff Roberson 3708355f576SJeff Roberson if (!(slab->us_flags & UMA_SLAB_MALLOC)) { 3718f70816cSJeff Roberson #ifdef INVARIANTS 37263a7e0a3SRobert Watson struct malloc_type **mtpp = addr; 3738f70816cSJeff Roberson #endif 374099a0e58SBosko Milekic size = slab->us_keg->uk_size; 3758f70816cSJeff Roberson #ifdef INVARIANTS 3768f70816cSJeff Roberson /* 3778f70816cSJeff Roberson * Cache a pointer to the malloc_type that most recently freed 3788f70816cSJeff Roberson * this memory here. This way we know who is most likely to 3798f70816cSJeff Roberson * have stepped on it later. 3808f70816cSJeff Roberson * 3818f70816cSJeff Roberson * This code assumes that size is a multiple of 8 bytes for 3828f70816cSJeff Roberson * 64 bit machines 3838f70816cSJeff Roberson */ 38463a7e0a3SRobert Watson mtpp = (struct malloc_type **) 38563a7e0a3SRobert Watson ((unsigned long)mtpp & ~UMA_ALIGN_PTR); 38663a7e0a3SRobert Watson mtpp += (size - sizeof(struct malloc_type *)) / 3878f70816cSJeff Roberson sizeof(struct malloc_type *); 38863a7e0a3SRobert Watson *mtpp = mtp; 3898f70816cSJeff Roberson #endif 390099a0e58SBosko Milekic uma_zfree_arg(LIST_FIRST(&slab->us_keg->uk_zones), addr, slab); 39114bf02f8SJohn Dyson } else { 3928355f576SJeff Roberson size = slab->us_size; 3938355f576SJeff Roberson uma_large_free(slab); 39414bf02f8SJohn Dyson } 39563a7e0a3SRobert Watson malloc_type_freed(mtp, size); 396df8bae1dSRodney W. Grimes } 397df8bae1dSRodney W. Grimes 398df8bae1dSRodney W. Grimes /* 39944a8ff31SArchie Cobbs * realloc: change the size of a memory block 40044a8ff31SArchie Cobbs */ 40144a8ff31SArchie Cobbs void * 40263a7e0a3SRobert Watson realloc(void *addr, unsigned long size, struct malloc_type *mtp, int flags) 40344a8ff31SArchie Cobbs { 4048355f576SJeff Roberson uma_slab_t slab; 40544a8ff31SArchie Cobbs unsigned long alloc; 40644a8ff31SArchie Cobbs void *newaddr; 40744a8ff31SArchie Cobbs 40844a8ff31SArchie Cobbs /* realloc(NULL, ...) is equivalent to malloc(...) */ 40944a8ff31SArchie Cobbs if (addr == NULL) 41063a7e0a3SRobert Watson return (malloc(size, mtp, flags)); 41163a7e0a3SRobert Watson 41263a7e0a3SRobert Watson /* 41363a7e0a3SRobert Watson * XXX: Should report free of old memory and alloc of new memory to 41463a7e0a3SRobert Watson * per-CPU stats. 41563a7e0a3SRobert Watson */ 41644a8ff31SArchie Cobbs 417e4eb384bSBosko Milekic #ifdef DEBUG_MEMGUARD 418d362c40dSPawel Jakub Dawidek if (memguard_cmp(mtp)) { 419e4eb384bSBosko Milekic slab = NULL; 420e4eb384bSBosko Milekic alloc = size; 421e4eb384bSBosko Milekic } else { 422e4eb384bSBosko Milekic #endif 423e4eb384bSBosko Milekic 42499571dc3SJeff Roberson slab = vtoslab((vm_offset_t)addr & ~(UMA_SLAB_MASK)); 4258355f576SJeff Roberson 42644a8ff31SArchie Cobbs /* Sanity check */ 4278355f576SJeff Roberson KASSERT(slab != NULL, 42844a8ff31SArchie Cobbs ("realloc: address %p out of range", (void *)addr)); 42944a8ff31SArchie Cobbs 43044a8ff31SArchie Cobbs /* Get the size of the original block */ 431619f2841SPawel Jakub Dawidek if (!(slab->us_flags & UMA_SLAB_MALLOC)) 432099a0e58SBosko Milekic alloc = slab->us_keg->uk_size; 4338355f576SJeff Roberson else 4348355f576SJeff Roberson alloc = slab->us_size; 43544a8ff31SArchie Cobbs 43644a8ff31SArchie Cobbs /* Reuse the original block if appropriate */ 43744a8ff31SArchie Cobbs if (size <= alloc 43844a8ff31SArchie Cobbs && (size > (alloc >> REALLOC_FRACTION) || alloc == MINALLOCSIZE)) 43944a8ff31SArchie Cobbs return (addr); 44044a8ff31SArchie Cobbs 441e4eb384bSBosko Milekic #ifdef DEBUG_MEMGUARD 442e4eb384bSBosko Milekic } 443e4eb384bSBosko Milekic #endif 444e4eb384bSBosko Milekic 44544a8ff31SArchie Cobbs /* Allocate a new, bigger (or smaller) block */ 44663a7e0a3SRobert Watson if ((newaddr = malloc(size, mtp, flags)) == NULL) 44744a8ff31SArchie Cobbs return (NULL); 44844a8ff31SArchie Cobbs 44944a8ff31SArchie Cobbs /* Copy over original contents */ 45044a8ff31SArchie Cobbs bcopy(addr, newaddr, min(size, alloc)); 45163a7e0a3SRobert Watson free(addr, mtp); 45244a8ff31SArchie Cobbs return (newaddr); 45344a8ff31SArchie Cobbs } 45444a8ff31SArchie Cobbs 45544a8ff31SArchie Cobbs /* 45644a8ff31SArchie Cobbs * reallocf: same as realloc() but free memory on failure. 45744a8ff31SArchie Cobbs */ 45844a8ff31SArchie Cobbs void * 45963a7e0a3SRobert Watson reallocf(void *addr, unsigned long size, struct malloc_type *mtp, int flags) 46044a8ff31SArchie Cobbs { 46144a8ff31SArchie Cobbs void *mem; 46244a8ff31SArchie Cobbs 46363a7e0a3SRobert Watson if ((mem = realloc(addr, size, mtp, flags)) == NULL) 46463a7e0a3SRobert Watson free(addr, mtp); 46544a8ff31SArchie Cobbs return (mem); 46644a8ff31SArchie Cobbs } 46744a8ff31SArchie Cobbs 46844a8ff31SArchie Cobbs /* 469df8bae1dSRodney W. Grimes * Initialize the kernel memory allocator 470df8bae1dSRodney W. Grimes */ 4712b14f991SJulian Elischer /* ARGSUSED*/ 4722b14f991SJulian Elischer static void 47387efd4d5SRobert Watson kmeminit(void *dummy) 474df8bae1dSRodney W. Grimes { 4756f267175SJeff Roberson u_int8_t indx; 47627b8623fSDavid Greenman u_long mem_size; 4778355f576SJeff Roberson int i; 4788a58a9f6SJohn Dyson 4796008862bSJohn Baldwin mtx_init(&malloc_mtx, "malloc", NULL, MTX_DEF); 48069ef67f9SJason Evans 4818a58a9f6SJohn Dyson /* 4828a58a9f6SJohn Dyson * Try to auto-tune the kernel memory size, so that it is 4838a58a9f6SJohn Dyson * more applicable for a wider range of machine sizes. 4848a58a9f6SJohn Dyson * On an X86, a VM_KMEM_SIZE_SCALE value of 4 is good, while 4858a58a9f6SJohn Dyson * a VM_KMEM_SIZE of 12MB is a fair compromise. The 4868a58a9f6SJohn Dyson * VM_KMEM_SIZE_MAX is dependent on the maximum KVA space 4878a58a9f6SJohn Dyson * available, and on an X86 with a total KVA space of 256MB, 4888a58a9f6SJohn Dyson * try to keep VM_KMEM_SIZE_MAX at 80MB or below. 4898a58a9f6SJohn Dyson * 4908a58a9f6SJohn Dyson * Note that the kmem_map is also used by the zone allocator, 4918a58a9f6SJohn Dyson * so make sure that there is enough space. 4928a58a9f6SJohn Dyson */ 493099a0e58SBosko Milekic vm_kmem_size = VM_KMEM_SIZE + nmbclusters * PAGE_SIZE; 4941795d0cdSPaul Saab mem_size = cnt.v_page_count; 4958a58a9f6SJohn Dyson 4968a58a9f6SJohn Dyson #if defined(VM_KMEM_SIZE_SCALE) 497479439b4SDag-Erling Smørgrav vm_kmem_size_scale = VM_KMEM_SIZE_SCALE; 4988a58a9f6SJohn Dyson #endif 499479439b4SDag-Erling Smørgrav TUNABLE_INT_FETCH("vm.kmem_size_scale", &vm_kmem_size_scale); 500479439b4SDag-Erling Smørgrav if (vm_kmem_size_scale > 0 && 501479439b4SDag-Erling Smørgrav (mem_size / vm_kmem_size_scale) > (vm_kmem_size / PAGE_SIZE)) 502479439b4SDag-Erling Smørgrav vm_kmem_size = (mem_size / vm_kmem_size_scale) * PAGE_SIZE; 5038a58a9f6SJohn Dyson 5048a58a9f6SJohn Dyson #if defined(VM_KMEM_SIZE_MAX) 505479439b4SDag-Erling Smørgrav vm_kmem_size_max = VM_KMEM_SIZE_MAX; 5068a58a9f6SJohn Dyson #endif 507479439b4SDag-Erling Smørgrav TUNABLE_INT_FETCH("vm.kmem_size_max", &vm_kmem_size_max); 508479439b4SDag-Erling Smørgrav if (vm_kmem_size_max > 0 && vm_kmem_size >= vm_kmem_size_max) 509479439b4SDag-Erling Smørgrav vm_kmem_size = vm_kmem_size_max; 5108a58a9f6SJohn Dyson 5118de6e8e1SMike Smith /* Allow final override from the kernel environment */ 51284344f9fSDag-Erling Smørgrav #ifndef BURN_BRIDGES 51384344f9fSDag-Erling Smørgrav if (TUNABLE_INT_FETCH("kern.vm.kmem.size", &vm_kmem_size) != 0) 51484344f9fSDag-Erling Smørgrav printf("kern.vm.kmem.size is now called vm.kmem_size!\n"); 51584344f9fSDag-Erling Smørgrav #endif 51684344f9fSDag-Erling Smørgrav TUNABLE_INT_FETCH("vm.kmem_size", &vm_kmem_size); 5178de6e8e1SMike Smith 51827b8623fSDavid Greenman /* 51927b8623fSDavid Greenman * Limit kmem virtual size to twice the physical memory. 52027b8623fSDavid Greenman * This allows for kmem map sparseness, but limits the size 52127b8623fSDavid Greenman * to something sane. Be careful to not overflow the 32bit 52227b8623fSDavid Greenman * ints while doing the check. 52327b8623fSDavid Greenman */ 5241795d0cdSPaul Saab if (((vm_kmem_size / 2) / PAGE_SIZE) > cnt.v_page_count) 52527b8623fSDavid Greenman vm_kmem_size = 2 * cnt.v_page_count * PAGE_SIZE; 5268a58a9f6SJohn Dyson 52708442f8aSBosko Milekic /* 528347194c1SMike Silbersack * Tune settings based on the kernel map's size at this time. 529347194c1SMike Silbersack */ 530347194c1SMike Silbersack init_param3(vm_kmem_size / PAGE_SIZE); 531347194c1SMike Silbersack 532df8bae1dSRodney W. Grimes kmem_map = kmem_suballoc(kernel_map, (vm_offset_t *)&kmembase, 533099a0e58SBosko Milekic (vm_offset_t *)&kmemlimit, vm_kmem_size); 5343075778bSJohn Dyson kmem_map->system_map = 1; 5358355f576SJeff Roberson 536e4eb384bSBosko Milekic #ifdef DEBUG_MEMGUARD 537e4eb384bSBosko Milekic /* 538e4eb384bSBosko Milekic * Initialize MemGuard if support compiled in. MemGuard is a 539e4eb384bSBosko Milekic * replacement allocator used for detecting tamper-after-free 540e4eb384bSBosko Milekic * scenarios as they occur. It is only used for debugging. 541e4eb384bSBosko Milekic */ 542e4eb384bSBosko Milekic vm_memguard_divisor = 10; 543d362c40dSPawel Jakub Dawidek TUNABLE_INT_FETCH("vm.memguard.divisor", &vm_memguard_divisor); 544e4eb384bSBosko Milekic 545e4eb384bSBosko Milekic /* Pick a conservative value if provided value sucks. */ 546e4eb384bSBosko Milekic if ((vm_memguard_divisor <= 0) || 547e4eb384bSBosko Milekic ((vm_kmem_size / vm_memguard_divisor) == 0)) 548e4eb384bSBosko Milekic vm_memguard_divisor = 10; 549e4eb384bSBosko Milekic memguard_init(kmem_map, vm_kmem_size / vm_memguard_divisor); 550e4eb384bSBosko Milekic #endif 551e4eb384bSBosko Milekic 55299571dc3SJeff Roberson uma_startup2(); 5538355f576SJeff Roberson 55463a7e0a3SRobert Watson mt_zone = uma_zcreate("mt_zone", sizeof(struct malloc_type_internal), 55563a7e0a3SRobert Watson #ifdef INVARIANTS 55663a7e0a3SRobert Watson mtrash_ctor, mtrash_dtor, mtrash_init, mtrash_fini, 55763a7e0a3SRobert Watson #else 55863a7e0a3SRobert Watson NULL, NULL, NULL, NULL, 55963a7e0a3SRobert Watson #endif 56063a7e0a3SRobert Watson UMA_ALIGN_PTR, UMA_ZONE_MALLOC); 5616f267175SJeff Roberson for (i = 0, indx = 0; kmemzones[indx].kz_size != 0; indx++) { 5626f267175SJeff Roberson int size = kmemzones[indx].kz_size; 5636f267175SJeff Roberson char *name = kmemzones[indx].kz_name; 5648355f576SJeff Roberson 5658efc4effSJeff Roberson kmemzones[indx].kz_zone = uma_zcreate(name, size, 5668efc4effSJeff Roberson #ifdef INVARIANTS 5678f70816cSJeff Roberson mtrash_ctor, mtrash_dtor, mtrash_init, mtrash_fini, 5688efc4effSJeff Roberson #else 5698efc4effSJeff Roberson NULL, NULL, NULL, NULL, 5708efc4effSJeff Roberson #endif 5718efc4effSJeff Roberson UMA_ALIGN_PTR, UMA_ZONE_MALLOC); 5726f267175SJeff Roberson 5738355f576SJeff Roberson for (;i <= size; i+= KMEM_ZBASE) 5746f267175SJeff Roberson kmemsize[i >> KMEM_ZSHIFT] = indx; 5758355f576SJeff Roberson 576df8bae1dSRodney W. Grimes } 577254c6cb3SPoul-Henning Kamp } 578254c6cb3SPoul-Henning Kamp 579db669378SPeter Wemm void 58087efd4d5SRobert Watson malloc_init(void *data) 581254c6cb3SPoul-Henning Kamp { 58263a7e0a3SRobert Watson struct malloc_type_internal *mtip; 58363a7e0a3SRobert Watson struct malloc_type *mtp; 58463a7e0a3SRobert Watson 58563a7e0a3SRobert Watson KASSERT(cnt.v_page_count != 0, ("malloc_register before vm_init")); 58663a7e0a3SRobert Watson 58763a7e0a3SRobert Watson mtp = data; 58863a7e0a3SRobert Watson mtip = uma_zalloc(mt_zone, M_WAITOK | M_ZERO); 58963a7e0a3SRobert Watson mtp->ks_handle = mtip; 590254c6cb3SPoul-Henning Kamp 5916f267175SJeff Roberson mtx_lock(&malloc_mtx); 59263a7e0a3SRobert Watson mtp->ks_next = kmemstatistics; 59363a7e0a3SRobert Watson kmemstatistics = mtp; 594cd814b26SRobert Watson kmemcount++; 5956f267175SJeff Roberson mtx_unlock(&malloc_mtx); 596df8bae1dSRodney W. Grimes } 597db669378SPeter Wemm 598db669378SPeter Wemm void 59987efd4d5SRobert Watson malloc_uninit(void *data) 600db669378SPeter Wemm { 60163a7e0a3SRobert Watson struct malloc_type_internal *mtip; 6022a143d5bSPawel Jakub Dawidek struct malloc_type_stats *mtsp; 60363a7e0a3SRobert Watson struct malloc_type *mtp, *temp; 6042a143d5bSPawel Jakub Dawidek long temp_allocs, temp_bytes; 6052a143d5bSPawel Jakub Dawidek int i; 606db669378SPeter Wemm 60763a7e0a3SRobert Watson mtp = data; 60863a7e0a3SRobert Watson KASSERT(mtp->ks_handle != NULL, ("malloc_deregister: cookie NULL")); 6096f267175SJeff Roberson mtx_lock(&malloc_mtx); 61063a7e0a3SRobert Watson mtip = mtp->ks_handle; 61163a7e0a3SRobert Watson mtp->ks_handle = NULL; 61263a7e0a3SRobert Watson if (mtp != kmemstatistics) { 61363a7e0a3SRobert Watson for (temp = kmemstatistics; temp != NULL; 61463a7e0a3SRobert Watson temp = temp->ks_next) { 61563a7e0a3SRobert Watson if (temp->ks_next == mtp) 61663a7e0a3SRobert Watson temp->ks_next = mtp->ks_next; 617db669378SPeter Wemm } 61863a7e0a3SRobert Watson } else 61963a7e0a3SRobert Watson kmemstatistics = mtp->ks_next; 620cd814b26SRobert Watson kmemcount--; 6216f267175SJeff Roberson mtx_unlock(&malloc_mtx); 6222a143d5bSPawel Jakub Dawidek 6232a143d5bSPawel Jakub Dawidek /* 6242a143d5bSPawel Jakub Dawidek * Look for memory leaks. 6252a143d5bSPawel Jakub Dawidek */ 6262a143d5bSPawel Jakub Dawidek temp_allocs = temp_bytes = 0; 6272a143d5bSPawel Jakub Dawidek for (i = 0; i < MAXCPU; i++) { 6282a143d5bSPawel Jakub Dawidek mtsp = &mtip->mti_stats[i]; 6292a143d5bSPawel Jakub Dawidek temp_allocs += mtsp->mts_numallocs; 6302a143d5bSPawel Jakub Dawidek temp_allocs -= mtsp->mts_numfrees; 6312a143d5bSPawel Jakub Dawidek temp_bytes += mtsp->mts_memalloced; 6322a143d5bSPawel Jakub Dawidek temp_bytes -= mtsp->mts_memfreed; 6332a143d5bSPawel Jakub Dawidek } 6342a143d5bSPawel Jakub Dawidek if (temp_allocs > 0 || temp_bytes > 0) { 6352a143d5bSPawel Jakub Dawidek printf("Warning: memory type %s leaked memory on destroy " 6362a143d5bSPawel Jakub Dawidek "(%ld allocations, %ld bytes leaked).\n", mtp->ks_shortdesc, 6372a143d5bSPawel Jakub Dawidek temp_allocs, temp_bytes); 6382a143d5bSPawel Jakub Dawidek } 6392a143d5bSPawel Jakub Dawidek 6408c61b219SJoseph Koshy uma_zfree(mt_zone, mtip); 641db669378SPeter Wemm } 6426f267175SJeff Roberson 643d362c40dSPawel Jakub Dawidek struct malloc_type * 644d362c40dSPawel Jakub Dawidek malloc_desc2type(const char *desc) 645d362c40dSPawel Jakub Dawidek { 646d362c40dSPawel Jakub Dawidek struct malloc_type *mtp; 647d362c40dSPawel Jakub Dawidek 648d362c40dSPawel Jakub Dawidek mtx_assert(&malloc_mtx, MA_OWNED); 649d362c40dSPawel Jakub Dawidek for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) { 650d362c40dSPawel Jakub Dawidek if (strcmp(mtp->ks_shortdesc, desc) == 0) 651d362c40dSPawel Jakub Dawidek return (mtp); 652d362c40dSPawel Jakub Dawidek } 653d362c40dSPawel Jakub Dawidek return (NULL); 654d362c40dSPawel Jakub Dawidek } 655d362c40dSPawel Jakub Dawidek 6566f267175SJeff Roberson static int 6576f267175SJeff Roberson sysctl_kern_malloc(SYSCTL_HANDLER_ARGS) 6586f267175SJeff Roberson { 65963a7e0a3SRobert Watson struct malloc_type_stats mts_local, *mtsp; 66063a7e0a3SRobert Watson struct malloc_type_internal *mtip; 66163a7e0a3SRobert Watson struct malloc_type *mtp; 66263a7e0a3SRobert Watson struct sbuf sbuf; 66363a7e0a3SRobert Watson long temp_allocs, temp_bytes; 6646f267175SJeff Roberson int linesize = 128; 6656f267175SJeff Roberson int bufsize; 6666f267175SJeff Roberson int first; 6676f267175SJeff Roberson int error; 6686f267175SJeff Roberson char *buf; 6696f267175SJeff Roberson int cnt; 6706f267175SJeff Roberson int i; 6716f267175SJeff Roberson 6726f267175SJeff Roberson cnt = 0; 6736f267175SJeff Roberson 67463a7e0a3SRobert Watson /* Guess at how much room is needed. */ 6756f267175SJeff Roberson mtx_lock(&malloc_mtx); 676cd814b26SRobert Watson cnt = kmemcount; 6775a34a9f0SJeff Roberson mtx_unlock(&malloc_mtx); 67863a7e0a3SRobert Watson 6796f267175SJeff Roberson bufsize = linesize * (cnt + 1); 68063a7e0a3SRobert Watson buf = malloc(bufsize, M_TEMP, M_WAITOK|M_ZERO); 68163a7e0a3SRobert Watson sbuf_new(&sbuf, buf, bufsize, SBUF_FIXEDLEN); 68263a7e0a3SRobert Watson 6835a34a9f0SJeff Roberson mtx_lock(&malloc_mtx); 68463a7e0a3SRobert Watson sbuf_printf(&sbuf, 6856f267175SJeff Roberson "\n Type InUse MemUse HighUse Requests Size(s)\n"); 68663a7e0a3SRobert Watson for (mtp = kmemstatistics; cnt != 0 && mtp != NULL; 68763a7e0a3SRobert Watson mtp = mtp->ks_next, cnt--) { 68863a7e0a3SRobert Watson mtip = mtp->ks_handle; 68963a7e0a3SRobert Watson bzero(&mts_local, sizeof(mts_local)); 69063a7e0a3SRobert Watson for (i = 0; i < MAXCPU; i++) { 69163a7e0a3SRobert Watson mtsp = &mtip->mti_stats[i]; 69263a7e0a3SRobert Watson mts_local.mts_memalloced += mtsp->mts_memalloced; 69363a7e0a3SRobert Watson mts_local.mts_memfreed += mtsp->mts_memfreed; 69463a7e0a3SRobert Watson mts_local.mts_numallocs += mtsp->mts_numallocs; 69563a7e0a3SRobert Watson mts_local.mts_numfrees += mtsp->mts_numfrees; 69663a7e0a3SRobert Watson mts_local.mts_size |= mtsp->mts_size; 69763a7e0a3SRobert Watson } 69863a7e0a3SRobert Watson if (mts_local.mts_numallocs == 0) 6996f267175SJeff Roberson continue; 7006f267175SJeff Roberson 70163a7e0a3SRobert Watson /* 70263a7e0a3SRobert Watson * Due to races in per-CPU statistics gather, it's possible to 70363a7e0a3SRobert Watson * get a slightly negative number here. If we do, approximate 70463a7e0a3SRobert Watson * with 0. 70563a7e0a3SRobert Watson */ 70663a7e0a3SRobert Watson if (mts_local.mts_numallocs > mts_local.mts_numfrees) 70763a7e0a3SRobert Watson temp_allocs = mts_local.mts_numallocs - 70863a7e0a3SRobert Watson mts_local.mts_numfrees; 70963a7e0a3SRobert Watson else 71063a7e0a3SRobert Watson temp_allocs = 0; 71163a7e0a3SRobert Watson 71263a7e0a3SRobert Watson /* 71363a7e0a3SRobert Watson * Ditto for bytes allocated. 71463a7e0a3SRobert Watson */ 71563a7e0a3SRobert Watson if (mts_local.mts_memalloced > mts_local.mts_memfreed) 71663a7e0a3SRobert Watson temp_bytes = mts_local.mts_memalloced - 71763a7e0a3SRobert Watson mts_local.mts_memfreed; 71863a7e0a3SRobert Watson else 71963a7e0a3SRobert Watson temp_bytes = 0; 72063a7e0a3SRobert Watson 72163a7e0a3SRobert Watson /* 722cd814b26SRobert Watson * High-waterwark is no longer easily available, so we just 723cd814b26SRobert Watson * print '-' for that column. 72463a7e0a3SRobert Watson */ 725cd814b26SRobert Watson sbuf_printf(&sbuf, "%13s%6lu%6luK -%9llu", 72663a7e0a3SRobert Watson mtp->ks_shortdesc, 72763a7e0a3SRobert Watson temp_allocs, 72863a7e0a3SRobert Watson (temp_bytes + 1023) / 1024, 7294f8721d2SRobert Watson (unsigned long long)mts_local.mts_numallocs); 7306f267175SJeff Roberson 7316f267175SJeff Roberson first = 1; 732280759e7SRobert Drehmel for (i = 0; i < sizeof(kmemzones) / sizeof(kmemzones[0]) - 1; 733280759e7SRobert Drehmel i++) { 73463a7e0a3SRobert Watson if (mts_local.mts_size & (1 << i)) { 7356f267175SJeff Roberson if (first) 73663a7e0a3SRobert Watson sbuf_printf(&sbuf, " "); 7376f267175SJeff Roberson else 73863a7e0a3SRobert Watson sbuf_printf(&sbuf, ","); 73963a7e0a3SRobert Watson sbuf_printf(&sbuf, "%s", 74063a7e0a3SRobert Watson kmemzones[i].kz_name); 7416f267175SJeff Roberson first = 0; 7426f267175SJeff Roberson } 743280759e7SRobert Drehmel } 74463a7e0a3SRobert Watson sbuf_printf(&sbuf, "\n"); 7456f267175SJeff Roberson } 74663a7e0a3SRobert Watson sbuf_finish(&sbuf); 7476f267175SJeff Roberson mtx_unlock(&malloc_mtx); 7486f267175SJeff Roberson 74963a7e0a3SRobert Watson error = SYSCTL_OUT(req, sbuf_data(&sbuf), sbuf_len(&sbuf)); 75063a7e0a3SRobert Watson 75163a7e0a3SRobert Watson sbuf_delete(&sbuf); 7526f267175SJeff Roberson free(buf, M_TEMP); 7536f267175SJeff Roberson return (error); 7546f267175SJeff Roberson } 7556f267175SJeff Roberson 7566f267175SJeff Roberson SYSCTL_OID(_kern, OID_AUTO, malloc, CTLTYPE_STRING|CTLFLAG_RD, 7576f267175SJeff Roberson NULL, 0, sysctl_kern_malloc, "A", "Malloc Stats"); 7585e914b96SJeff Roberson 759cd814b26SRobert Watson static int 760cd814b26SRobert Watson sysctl_kern_malloc_stats(SYSCTL_HANDLER_ARGS) 761cd814b26SRobert Watson { 762cd814b26SRobert Watson struct malloc_type_stream_header mtsh; 763cd814b26SRobert Watson struct malloc_type_internal *mtip; 764cd814b26SRobert Watson struct malloc_type_header mth; 765cd814b26SRobert Watson struct malloc_type *mtp; 766cd814b26SRobert Watson int buflen, count, error, i; 767cd814b26SRobert Watson struct sbuf sbuf; 768cd814b26SRobert Watson char *buffer; 769cd814b26SRobert Watson 770cd814b26SRobert Watson mtx_lock(&malloc_mtx); 771cd814b26SRobert Watson restart: 772cd814b26SRobert Watson mtx_assert(&malloc_mtx, MA_OWNED); 773cd814b26SRobert Watson count = kmemcount; 774cd814b26SRobert Watson mtx_unlock(&malloc_mtx); 775cd814b26SRobert Watson buflen = sizeof(mtsh) + count * (sizeof(mth) + 776cd814b26SRobert Watson sizeof(struct malloc_type_stats) * MAXCPU) + 1; 777cd814b26SRobert Watson buffer = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO); 778cd814b26SRobert Watson mtx_lock(&malloc_mtx); 779cd814b26SRobert Watson if (count < kmemcount) { 780cd814b26SRobert Watson free(buffer, M_TEMP); 781cd814b26SRobert Watson goto restart; 782cd814b26SRobert Watson } 783cd814b26SRobert Watson 784cd814b26SRobert Watson sbuf_new(&sbuf, buffer, buflen, SBUF_FIXEDLEN); 785cd814b26SRobert Watson 786cd814b26SRobert Watson /* 787cd814b26SRobert Watson * Insert stream header. 788cd814b26SRobert Watson */ 789cd814b26SRobert Watson bzero(&mtsh, sizeof(mtsh)); 790cd814b26SRobert Watson mtsh.mtsh_version = MALLOC_TYPE_STREAM_VERSION; 791cd814b26SRobert Watson mtsh.mtsh_maxcpus = MAXCPU; 792cd814b26SRobert Watson mtsh.mtsh_count = kmemcount; 793cd814b26SRobert Watson if (sbuf_bcat(&sbuf, &mtsh, sizeof(mtsh)) < 0) { 794cd814b26SRobert Watson mtx_unlock(&malloc_mtx); 795cd814b26SRobert Watson error = ENOMEM; 796cd814b26SRobert Watson goto out; 797cd814b26SRobert Watson } 798cd814b26SRobert Watson 799cd814b26SRobert Watson /* 800cd814b26SRobert Watson * Insert alternating sequence of type headers and type statistics. 801cd814b26SRobert Watson */ 802cd814b26SRobert Watson for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) { 803cd814b26SRobert Watson mtip = (struct malloc_type_internal *)mtp->ks_handle; 804cd814b26SRobert Watson 805cd814b26SRobert Watson /* 806cd814b26SRobert Watson * Insert type header. 807cd814b26SRobert Watson */ 808cd814b26SRobert Watson bzero(&mth, sizeof(mth)); 809cd814b26SRobert Watson strlcpy(mth.mth_name, mtp->ks_shortdesc, MALLOC_MAX_NAME); 810cd814b26SRobert Watson if (sbuf_bcat(&sbuf, &mth, sizeof(mth)) < 0) { 811cd814b26SRobert Watson mtx_unlock(&malloc_mtx); 812cd814b26SRobert Watson error = ENOMEM; 813cd814b26SRobert Watson goto out; 814cd814b26SRobert Watson } 815cd814b26SRobert Watson 816cd814b26SRobert Watson /* 817cd814b26SRobert Watson * Insert type statistics for each CPU. 818cd814b26SRobert Watson */ 819cd814b26SRobert Watson for (i = 0; i < MAXCPU; i++) { 820cd814b26SRobert Watson if (sbuf_bcat(&sbuf, &mtip->mti_stats[i], 821cd814b26SRobert Watson sizeof(mtip->mti_stats[i])) < 0) { 822cd814b26SRobert Watson mtx_unlock(&malloc_mtx); 823cd814b26SRobert Watson error = ENOMEM; 824cd814b26SRobert Watson goto out; 825cd814b26SRobert Watson } 826cd814b26SRobert Watson } 827cd814b26SRobert Watson } 828cd814b26SRobert Watson mtx_unlock(&malloc_mtx); 829cd814b26SRobert Watson sbuf_finish(&sbuf); 830cd814b26SRobert Watson error = SYSCTL_OUT(req, sbuf_data(&sbuf), sbuf_len(&sbuf)); 831cd814b26SRobert Watson out: 832cd814b26SRobert Watson sbuf_delete(&sbuf); 833cd814b26SRobert Watson free(buffer, M_TEMP); 834cd814b26SRobert Watson return (error); 835cd814b26SRobert Watson } 836cd814b26SRobert Watson 837cd814b26SRobert Watson SYSCTL_PROC(_kern, OID_AUTO, malloc_stats, CTLFLAG_RD|CTLTYPE_STRUCT, 838cd814b26SRobert Watson 0, 0, sysctl_kern_malloc_stats, "s,malloc_type_ustats", 839cd814b26SRobert Watson "Return malloc types"); 840cd814b26SRobert Watson 841cd814b26SRobert Watson SYSCTL_INT(_kern, OID_AUTO, malloc_count, CTLFLAG_RD, &kmemcount, 0, 842cd814b26SRobert Watson "Count of kernel malloc types"); 843cd814b26SRobert Watson 844909ed16cSRobert Watson #ifdef DDB 845909ed16cSRobert Watson DB_SHOW_COMMAND(malloc, db_show_malloc) 846909ed16cSRobert Watson { 847909ed16cSRobert Watson struct malloc_type_internal *mtip; 848909ed16cSRobert Watson struct malloc_type *mtp; 849909ed16cSRobert Watson u_int64_t allocs, frees; 850909ed16cSRobert Watson int i; 851909ed16cSRobert Watson 852909ed16cSRobert Watson db_printf("%18s %12s %12s %12s\n", "Type", "Allocs", "Frees", 853909ed16cSRobert Watson "Used"); 854909ed16cSRobert Watson for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) { 855909ed16cSRobert Watson mtip = (struct malloc_type_internal *)mtp->ks_handle; 856909ed16cSRobert Watson allocs = 0; 857909ed16cSRobert Watson frees = 0; 858909ed16cSRobert Watson for (i = 0; i < MAXCPU; i++) { 859909ed16cSRobert Watson allocs += mtip->mti_stats[i].mts_numallocs; 860909ed16cSRobert Watson frees += mtip->mti_stats[i].mts_numfrees; 861909ed16cSRobert Watson } 86264a266f9SRobert Watson db_printf("%18s %12ju %12ju %12ju\n", mtp->ks_shortdesc, 863909ed16cSRobert Watson allocs, frees, allocs - frees); 864909ed16cSRobert Watson } 865909ed16cSRobert Watson } 866909ed16cSRobert Watson #endif 867909ed16cSRobert Watson 8685e914b96SJeff Roberson #ifdef MALLOC_PROFILE 8695e914b96SJeff Roberson 8705e914b96SJeff Roberson static int 8715e914b96SJeff Roberson sysctl_kern_mprof(SYSCTL_HANDLER_ARGS) 8725e914b96SJeff Roberson { 8735e914b96SJeff Roberson int linesize = 64; 87463a7e0a3SRobert Watson struct sbuf sbuf; 8755e914b96SJeff Roberson uint64_t count; 8765e914b96SJeff Roberson uint64_t waste; 8775e914b96SJeff Roberson uint64_t mem; 8785e914b96SJeff Roberson int bufsize; 8795e914b96SJeff Roberson int error; 8805e914b96SJeff Roberson char *buf; 8815e914b96SJeff Roberson int rsize; 8825e914b96SJeff Roberson int size; 8835e914b96SJeff Roberson int i; 8845e914b96SJeff Roberson 8855e914b96SJeff Roberson bufsize = linesize * (KMEM_ZSIZE + 1); 8865e914b96SJeff Roberson bufsize += 128; /* For the stats line */ 8875e914b96SJeff Roberson bufsize += 128; /* For the banner line */ 8885e914b96SJeff Roberson waste = 0; 8895e914b96SJeff Roberson mem = 0; 8905e914b96SJeff Roberson 89163a7e0a3SRobert Watson buf = malloc(bufsize, M_TEMP, M_WAITOK|M_ZERO); 89263a7e0a3SRobert Watson sbuf_new(&sbuf, buf, bufsize, SBUF_FIXEDLEN); 89363a7e0a3SRobert Watson sbuf_printf(&sbuf, 8945e914b96SJeff Roberson "\n Size Requests Real Size\n"); 8955e914b96SJeff Roberson for (i = 0; i < KMEM_ZSIZE; i++) { 8965e914b96SJeff Roberson size = i << KMEM_ZSHIFT; 8975e914b96SJeff Roberson rsize = kmemzones[kmemsize[i]].kz_size; 8985e914b96SJeff Roberson count = (long long unsigned)krequests[i]; 8995e914b96SJeff Roberson 90063a7e0a3SRobert Watson sbuf_printf(&sbuf, "%6d%28llu%11d\n", size, 90163a7e0a3SRobert Watson (unsigned long long)count, rsize); 9025e914b96SJeff Roberson 9035e914b96SJeff Roberson if ((rsize * count) > (size * count)) 9045e914b96SJeff Roberson waste += (rsize * count) - (size * count); 9055e914b96SJeff Roberson mem += (rsize * count); 9065e914b96SJeff Roberson } 90763a7e0a3SRobert Watson sbuf_printf(&sbuf, 9085e914b96SJeff Roberson "\nTotal memory used:\t%30llu\nTotal Memory wasted:\t%30llu\n", 9095e914b96SJeff Roberson (unsigned long long)mem, (unsigned long long)waste); 91063a7e0a3SRobert Watson sbuf_finish(&sbuf); 9115e914b96SJeff Roberson 91263a7e0a3SRobert Watson error = SYSCTL_OUT(req, sbuf_data(&sbuf), sbuf_len(&sbuf)); 9135e914b96SJeff Roberson 91463a7e0a3SRobert Watson sbuf_delete(&sbuf); 9155e914b96SJeff Roberson free(buf, M_TEMP); 9165e914b96SJeff Roberson return (error); 9175e914b96SJeff Roberson } 9185e914b96SJeff Roberson 9195e914b96SJeff Roberson SYSCTL_OID(_kern, OID_AUTO, mprof, CTLTYPE_STRING|CTLFLAG_RD, 9205e914b96SJeff Roberson NULL, 0, sysctl_kern_mprof, "A", "Malloc Profiling"); 9215e914b96SJeff Roberson #endif /* MALLOC_PROFILE */ 922