]> CyberLeo.Net >> Repos - FreeBSD/releng/8.2.git/blob - sys/kern/kern_malloc.c
MFC 216679:
[FreeBSD/releng/8.2.git] / sys / kern / kern_malloc.c
1 /*-
2  * Copyright (c) 1987, 1991, 1993
3  *      The Regents of the University of California.
4  * Copyright (c) 2005-2009 Robert N. M. Watson
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 4. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *      @(#)kern_malloc.c       8.3 (Berkeley) 1/4/94
32  */
33
34 /*
35  * Kernel malloc(9) implementation -- general purpose kernel memory allocator
36  * based on memory types.  Back end is implemented using the UMA(9) zone
37  * allocator.  A set of fixed-size buckets are used for smaller allocations,
38  * and a special UMA allocation interface is used for larger allocations.
39  * Callers declare memory types, and statistics are maintained independently
40  * for each memory type.  Statistics are maintained per-CPU for performance
41  * reasons.  See malloc(9) and comments in malloc.h for a detailed
42  * description.
43  */
44
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
47
48 #include "opt_ddb.h"
49 #include "opt_kdtrace.h"
50 #include "opt_vm.h"
51
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/kdb.h>
55 #include <sys/kernel.h>
56 #include <sys/lock.h>
57 #include <sys/malloc.h>
58 #include <sys/mbuf.h>
59 #include <sys/mutex.h>
60 #include <sys/vmmeter.h>
61 #include <sys/proc.h>
62 #include <sys/sbuf.h>
63 #include <sys/sysctl.h>
64 #include <sys/time.h>
65
66 #include <vm/vm.h>
67 #include <vm/pmap.h>
68 #include <vm/vm_param.h>
69 #include <vm/vm_kern.h>
70 #include <vm/vm_extern.h>
71 #include <vm/vm_map.h>
72 #include <vm/vm_page.h>
73 #include <vm/uma.h>
74 #include <vm/uma_int.h>
75 #include <vm/uma_dbg.h>
76
77 #ifdef DEBUG_MEMGUARD
78 #include <vm/memguard.h>
79 #endif
80 #ifdef DEBUG_REDZONE
81 #include <vm/redzone.h>
82 #endif
83
84 #if defined(INVARIANTS) && defined(__i386__)
85 #include <machine/cpu.h>
86 #endif
87
88 #include <ddb/ddb.h>
89
90 #ifdef KDTRACE_HOOKS
91 #include <sys/dtrace_bsd.h>
92
93 dtrace_malloc_probe_func_t      dtrace_malloc_probe;
94 #endif
95
96 /*
97  * When realloc() is called, if the new size is sufficiently smaller than
98  * the old size, realloc() will allocate a new, smaller block to avoid
99  * wasting memory. 'Sufficiently smaller' is defined as: newsize <=
100  * oldsize / 2^n, where REALLOC_FRACTION defines the value of 'n'.
101  */
102 #ifndef REALLOC_FRACTION
103 #define REALLOC_FRACTION        1       /* new block if <= half the size */
104 #endif
105
106 /*
107  * Centrally define some common malloc types.
108  */
109 MALLOC_DEFINE(M_CACHE, "cache", "Various Dynamically allocated caches");
110 MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory");
111 MALLOC_DEFINE(M_TEMP, "temp", "misc temporary data buffers");
112
113 MALLOC_DEFINE(M_IP6OPT, "ip6opt", "IPv6 options");
114 MALLOC_DEFINE(M_IP6NDP, "ip6ndp", "IPv6 Neighbor Discovery");
115
116 static void kmeminit(void *);
117 SYSINIT(kmem, SI_SUB_KMEM, SI_ORDER_FIRST, kmeminit, NULL);
118
119 static MALLOC_DEFINE(M_FREE, "free", "should be on free list");
120
121 static struct malloc_type *kmemstatistics;
122 static vm_offset_t kmembase;
123 static vm_offset_t kmemlimit;
124 static int kmemcount;
125
126 #define KMEM_ZSHIFT     4
127 #define KMEM_ZBASE      16
128 #define KMEM_ZMASK      (KMEM_ZBASE - 1)
129
130 #define KMEM_ZMAX       PAGE_SIZE
131 #define KMEM_ZSIZE      (KMEM_ZMAX >> KMEM_ZSHIFT)
132 static u_int8_t kmemsize[KMEM_ZSIZE + 1];
133
134 /*
135  * Small malloc(9) memory allocations are allocated from a set of UMA buckets
136  * of various sizes.
137  *
138  * XXX: The comment here used to read "These won't be powers of two for
139  * long."  It's possible that a significant amount of wasted memory could be
140  * recovered by tuning the sizes of these buckets.
141  */
142 struct {
143         int kz_size;
144         char *kz_name;
145         uma_zone_t kz_zone;
146 } kmemzones[] = {
147         {16, "16", NULL},
148         {32, "32", NULL},
149         {64, "64", NULL},
150         {128, "128", NULL},
151         {256, "256", NULL},
152         {512, "512", NULL},
153         {1024, "1024", NULL},
154         {2048, "2048", NULL},
155         {4096, "4096", NULL},
156 #if PAGE_SIZE > 4096
157         {8192, "8192", NULL},
158 #if PAGE_SIZE > 8192
159         {16384, "16384", NULL},
160 #if PAGE_SIZE > 16384
161         {32768, "32768", NULL},
162 #if PAGE_SIZE > 32768
163         {65536, "65536", NULL},
164 #if PAGE_SIZE > 65536
165 #error  "Unsupported PAGE_SIZE"
166 #endif  /* 65536 */
167 #endif  /* 32768 */
168 #endif  /* 16384 */
169 #endif  /* 8192 */
170 #endif  /* 4096 */
171         {0, NULL},
172 };
173
174 /*
175  * Zone to allocate malloc type descriptions from.  For ABI reasons, memory
176  * types are described by a data structure passed by the declaring code, but
177  * the malloc(9) implementation has its own data structure describing the
178  * type and statistics.  This permits the malloc(9)-internal data structures
179  * to be modified without breaking binary-compiled kernel modules that
180  * declare malloc types.
181  */
182 static uma_zone_t mt_zone;
183
184 u_long vm_kmem_size;
185 SYSCTL_ULONG(_vm, OID_AUTO, kmem_size, CTLFLAG_RDTUN, &vm_kmem_size, 0,
186     "Size of kernel memory");
187
188 static u_long vm_kmem_size_min;
189 SYSCTL_ULONG(_vm, OID_AUTO, kmem_size_min, CTLFLAG_RDTUN, &vm_kmem_size_min, 0,
190     "Minimum size of kernel memory");
191
192 static u_long vm_kmem_size_max;
193 SYSCTL_ULONG(_vm, OID_AUTO, kmem_size_max, CTLFLAG_RDTUN, &vm_kmem_size_max, 0,
194     "Maximum size of kernel memory");
195
196 static u_int vm_kmem_size_scale;
197 SYSCTL_UINT(_vm, OID_AUTO, kmem_size_scale, CTLFLAG_RDTUN, &vm_kmem_size_scale, 0,
198     "Scale factor for kernel memory size");
199
200 static int sysctl_kmem_map_size(SYSCTL_HANDLER_ARGS);
201 SYSCTL_PROC(_vm, OID_AUTO, kmem_map_size,
202     CTLFLAG_RD | CTLTYPE_ULONG | CTLFLAG_MPSAFE, NULL, 0,
203     sysctl_kmem_map_size, "LU", "Current kmem_map allocation size");
204
205 static int sysctl_kmem_map_free(SYSCTL_HANDLER_ARGS);
206 SYSCTL_PROC(_vm, OID_AUTO, kmem_map_free,
207     CTLFLAG_RD | CTLTYPE_ULONG | CTLFLAG_MPSAFE, NULL, 0,
208     sysctl_kmem_map_free, "LU", "Largest contiguous free range in kmem_map");
209
210 /*
211  * The malloc_mtx protects the kmemstatistics linked list.
212  */
213 struct mtx malloc_mtx;
214
215 #ifdef MALLOC_PROFILE
216 uint64_t krequests[KMEM_ZSIZE + 1];
217
218 static int sysctl_kern_mprof(SYSCTL_HANDLER_ARGS);
219 #endif
220
221 static int sysctl_kern_malloc_stats(SYSCTL_HANDLER_ARGS);
222
223 /*
224  * time_uptime of the last malloc(9) failure (induced or real).
225  */
226 static time_t t_malloc_fail;
227
228 /*
229  * malloc(9) fault injection -- cause malloc failures every (n) mallocs when
230  * the caller specifies M_NOWAIT.  If set to 0, no failures are caused.
231  */
232 #ifdef MALLOC_MAKE_FAILURES
233 SYSCTL_NODE(_debug, OID_AUTO, malloc, CTLFLAG_RD, 0,
234     "Kernel malloc debugging options");
235
236 static int malloc_failure_rate;
237 static int malloc_nowait_count;
238 static int malloc_failure_count;
239 SYSCTL_INT(_debug_malloc, OID_AUTO, failure_rate, CTLFLAG_RW,
240     &malloc_failure_rate, 0, "Every (n) mallocs with M_NOWAIT will fail");
241 TUNABLE_INT("debug.malloc.failure_rate", &malloc_failure_rate);
242 SYSCTL_INT(_debug_malloc, OID_AUTO, failure_count, CTLFLAG_RD,
243     &malloc_failure_count, 0, "Number of imposed M_NOWAIT malloc failures");
244 #endif
245
246 static int
247 sysctl_kmem_map_size(SYSCTL_HANDLER_ARGS)
248 {
249         u_long size;
250
251         size = kmem_map->size;
252         return (sysctl_handle_long(oidp, &size, 0, req));
253 }
254
255 static int
256 sysctl_kmem_map_free(SYSCTL_HANDLER_ARGS)
257 {
258         u_long size;
259
260         vm_map_lock_read(kmem_map);
261         size = kmem_map->root != NULL ?
262             kmem_map->root->max_free : kmem_map->size;
263         vm_map_unlock_read(kmem_map);
264         return (sysctl_handle_long(oidp, &size, 0, req));
265 }
266
267 int
268 malloc_last_fail(void)
269 {
270
271         return (time_uptime - t_malloc_fail);
272 }
273
274 /*
275  * An allocation has succeeded -- update malloc type statistics for the
276  * amount of bucket size.  Occurs within a critical section so that the
277  * thread isn't preempted and doesn't migrate while updating per-PCU
278  * statistics.
279  */
280 static void
281 malloc_type_zone_allocated(struct malloc_type *mtp, unsigned long size,
282     int zindx)
283 {
284         struct malloc_type_internal *mtip;
285         struct malloc_type_stats *mtsp;
286
287         critical_enter();
288         mtip = mtp->ks_handle;
289         mtsp = &mtip->mti_stats[curcpu];
290         if (size > 0) {
291                 mtsp->mts_memalloced += size;
292                 mtsp->mts_numallocs++;
293         }
294         if (zindx != -1)
295                 mtsp->mts_size |= 1 << zindx;
296
297 #ifdef KDTRACE_HOOKS
298         if (dtrace_malloc_probe != NULL) {
299                 uint32_t probe_id = mtip->mti_probes[DTMALLOC_PROBE_MALLOC];
300                 if (probe_id != 0)
301                         (dtrace_malloc_probe)(probe_id,
302                             (uintptr_t) mtp, (uintptr_t) mtip,
303                             (uintptr_t) mtsp, size, zindx);
304         }
305 #endif
306
307         critical_exit();
308 }
309
310 void
311 malloc_type_allocated(struct malloc_type *mtp, unsigned long size)
312 {
313
314         if (size > 0)
315                 malloc_type_zone_allocated(mtp, size, -1);
316 }
317
318 /*
319  * A free operation has occurred -- update malloc type statistics for the
320  * amount of the bucket size.  Occurs within a critical section so that the
321  * thread isn't preempted and doesn't migrate while updating per-CPU
322  * statistics.
323  */
324 void
325 malloc_type_freed(struct malloc_type *mtp, unsigned long size)
326 {
327         struct malloc_type_internal *mtip;
328         struct malloc_type_stats *mtsp;
329
330         critical_enter();
331         mtip = mtp->ks_handle;
332         mtsp = &mtip->mti_stats[curcpu];
333         mtsp->mts_memfreed += size;
334         mtsp->mts_numfrees++;
335
336 #ifdef KDTRACE_HOOKS
337         if (dtrace_malloc_probe != NULL) {
338                 uint32_t probe_id = mtip->mti_probes[DTMALLOC_PROBE_FREE];
339                 if (probe_id != 0)
340                         (dtrace_malloc_probe)(probe_id,
341                             (uintptr_t) mtp, (uintptr_t) mtip,
342                             (uintptr_t) mtsp, size, 0);
343         }
344 #endif
345
346         critical_exit();
347 }
348
349 /*
350  *      malloc:
351  *
352  *      Allocate a block of memory.
353  *
354  *      If M_NOWAIT is set, this routine will not block and return NULL if
355  *      the allocation fails.
356  */
357 void *
358 malloc(unsigned long size, struct malloc_type *mtp, int flags)
359 {
360         int indx;
361         caddr_t va;
362         uma_zone_t zone;
363 #if defined(DIAGNOSTIC) || defined(DEBUG_REDZONE)
364         unsigned long osize = size;
365 #endif
366
367 #ifdef INVARIANTS
368         KASSERT(mtp->ks_magic == M_MAGIC, ("malloc: bad malloc type magic"));
369         /*
370          * Check that exactly one of M_WAITOK or M_NOWAIT is specified.
371          */
372         indx = flags & (M_WAITOK | M_NOWAIT);
373         if (indx != M_NOWAIT && indx != M_WAITOK) {
374                 static  struct timeval lasterr;
375                 static  int curerr, once;
376                 if (once == 0 && ppsratecheck(&lasterr, &curerr, 1)) {
377                         printf("Bad malloc flags: %x\n", indx);
378                         kdb_backtrace();
379                         flags |= M_WAITOK;
380                         once++;
381                 }
382         }
383 #endif
384 #ifdef MALLOC_MAKE_FAILURES
385         if ((flags & M_NOWAIT) && (malloc_failure_rate != 0)) {
386                 atomic_add_int(&malloc_nowait_count, 1);
387                 if ((malloc_nowait_count % malloc_failure_rate) == 0) {
388                         atomic_add_int(&malloc_failure_count, 1);
389                         t_malloc_fail = time_uptime;
390                         return (NULL);
391                 }
392         }
393 #endif
394         if (flags & M_WAITOK)
395                 KASSERT(curthread->td_intr_nesting_level == 0,
396                    ("malloc(M_WAITOK) in interrupt context"));
397
398 #ifdef DEBUG_MEMGUARD
399         if (memguard_cmp(mtp, size)) {
400                 va = memguard_alloc(size, flags);
401                 if (va != NULL)
402                         return (va);
403                 /* This is unfortunate but should not be fatal. */
404         }
405 #endif
406
407 #ifdef DEBUG_REDZONE
408         size = redzone_size_ntor(size);
409 #endif
410
411         if (size <= KMEM_ZMAX) {
412                 if (size & KMEM_ZMASK)
413                         size = (size & ~KMEM_ZMASK) + KMEM_ZBASE;
414                 indx = kmemsize[size >> KMEM_ZSHIFT];
415                 zone = kmemzones[indx].kz_zone;
416 #ifdef MALLOC_PROFILE
417                 krequests[size >> KMEM_ZSHIFT]++;
418 #endif
419                 va = uma_zalloc(zone, flags);
420                 if (va != NULL)
421                         size = zone->uz_size;
422                 malloc_type_zone_allocated(mtp, va == NULL ? 0 : size, indx);
423         } else {
424                 size = roundup(size, PAGE_SIZE);
425                 zone = NULL;
426                 va = uma_large_malloc(size, flags);
427                 malloc_type_allocated(mtp, va == NULL ? 0 : size);
428         }
429         if (flags & M_WAITOK)
430                 KASSERT(va != NULL, ("malloc(M_WAITOK) returned NULL"));
431         else if (va == NULL)
432                 t_malloc_fail = time_uptime;
433 #ifdef DIAGNOSTIC
434         if (va != NULL && !(flags & M_ZERO)) {
435                 memset(va, 0x70, osize);
436         }
437 #endif
438 #ifdef DEBUG_REDZONE
439         if (va != NULL)
440                 va = redzone_setup(va, osize);
441 #endif
442         return ((void *) va);
443 }
444
445 /*
446  *      free:
447  *
448  *      Free a block of memory allocated by malloc.
449  *
450  *      This routine may not block.
451  */
452 void
453 free(void *addr, struct malloc_type *mtp)
454 {
455         uma_slab_t slab;
456         u_long size;
457
458         KASSERT(mtp->ks_magic == M_MAGIC, ("free: bad malloc type magic"));
459
460         /* free(NULL, ...) does nothing */
461         if (addr == NULL)
462                 return;
463
464 #ifdef DEBUG_MEMGUARD
465         if (is_memguard_addr(addr)) {
466                 memguard_free(addr);
467                 return;
468         }
469 #endif
470
471 #ifdef DEBUG_REDZONE
472         redzone_check(addr);
473         addr = redzone_addr_ntor(addr);
474 #endif
475
476         slab = vtoslab((vm_offset_t)addr & (~UMA_SLAB_MASK));
477
478         if (slab == NULL)
479                 panic("free: address %p(%p) has not been allocated.\n",
480                     addr, (void *)((u_long)addr & (~UMA_SLAB_MASK)));
481
482
483         if (!(slab->us_flags & UMA_SLAB_MALLOC)) {
484 #ifdef INVARIANTS
485                 struct malloc_type **mtpp = addr;
486 #endif
487                 size = slab->us_keg->uk_size;
488 #ifdef INVARIANTS
489                 /*
490                  * Cache a pointer to the malloc_type that most recently freed
491                  * this memory here.  This way we know who is most likely to
492                  * have stepped on it later.
493                  *
494                  * This code assumes that size is a multiple of 8 bytes for
495                  * 64 bit machines
496                  */
497                 mtpp = (struct malloc_type **)
498                     ((unsigned long)mtpp & ~UMA_ALIGN_PTR);
499                 mtpp += (size - sizeof(struct malloc_type *)) /
500                     sizeof(struct malloc_type *);
501                 *mtpp = mtp;
502 #endif
503                 uma_zfree_arg(LIST_FIRST(&slab->us_keg->uk_zones), addr, slab);
504         } else {
505                 size = slab->us_size;
506                 uma_large_free(slab);
507         }
508         malloc_type_freed(mtp, size);
509 }
510
511 /*
512  *      realloc: change the size of a memory block
513  */
514 void *
515 realloc(void *addr, unsigned long size, struct malloc_type *mtp, int flags)
516 {
517         uma_slab_t slab;
518         unsigned long alloc;
519         void *newaddr;
520
521         KASSERT(mtp->ks_magic == M_MAGIC,
522             ("realloc: bad malloc type magic"));
523
524         /* realloc(NULL, ...) is equivalent to malloc(...) */
525         if (addr == NULL)
526                 return (malloc(size, mtp, flags));
527
528         /*
529          * XXX: Should report free of old memory and alloc of new memory to
530          * per-CPU stats.
531          */
532
533 #ifdef DEBUG_MEMGUARD
534         if (is_memguard_addr(addr))
535                 return (memguard_realloc(addr, size, mtp, flags));
536 #endif
537
538 #ifdef DEBUG_REDZONE
539         slab = NULL;
540         alloc = redzone_get_size(addr);
541 #else
542         slab = vtoslab((vm_offset_t)addr & ~(UMA_SLAB_MASK));
543
544         /* Sanity check */
545         KASSERT(slab != NULL,
546             ("realloc: address %p out of range", (void *)addr));
547
548         /* Get the size of the original block */
549         if (!(slab->us_flags & UMA_SLAB_MALLOC))
550                 alloc = slab->us_keg->uk_size;
551         else
552                 alloc = slab->us_size;
553
554         /* Reuse the original block if appropriate */
555         if (size <= alloc
556             && (size > (alloc >> REALLOC_FRACTION) || alloc == MINALLOCSIZE))
557                 return (addr);
558 #endif /* !DEBUG_REDZONE */
559
560         /* Allocate a new, bigger (or smaller) block */
561         if ((newaddr = malloc(size, mtp, flags)) == NULL)
562                 return (NULL);
563
564         /* Copy over original contents */
565         bcopy(addr, newaddr, min(size, alloc));
566         free(addr, mtp);
567         return (newaddr);
568 }
569
570 /*
571  *      reallocf: same as realloc() but free memory on failure.
572  */
573 void *
574 reallocf(void *addr, unsigned long size, struct malloc_type *mtp, int flags)
575 {
576         void *mem;
577
578         if ((mem = realloc(addr, size, mtp, flags)) == NULL)
579                 free(addr, mtp);
580         return (mem);
581 }
582
583 /*
584  * Initialize the kernel memory allocator
585  */
586 /* ARGSUSED*/
587 static void
588 kmeminit(void *dummy)
589 {
590         u_int8_t indx;
591         u_long mem_size, tmp;
592         int i;
593  
594         mtx_init(&malloc_mtx, "malloc", NULL, MTX_DEF);
595
596         /*
597          * Try to auto-tune the kernel memory size, so that it is
598          * more applicable for a wider range of machine sizes.
599          * On an X86, a VM_KMEM_SIZE_SCALE value of 4 is good, while
600          * a VM_KMEM_SIZE of 12MB is a fair compromise.  The
601          * VM_KMEM_SIZE_MAX is dependent on the maximum KVA space
602          * available, and on an X86 with a total KVA space of 256MB,
603          * try to keep VM_KMEM_SIZE_MAX at 80MB or below.
604          *
605          * Note that the kmem_map is also used by the zone allocator,
606          * so make sure that there is enough space.
607          */
608         vm_kmem_size = VM_KMEM_SIZE + nmbclusters * PAGE_SIZE;
609         mem_size = cnt.v_page_count;
610
611 #if defined(VM_KMEM_SIZE_SCALE)
612         vm_kmem_size_scale = VM_KMEM_SIZE_SCALE;
613 #endif
614         TUNABLE_INT_FETCH("vm.kmem_size_scale", &vm_kmem_size_scale);
615         if (vm_kmem_size_scale > 0 &&
616             (mem_size / vm_kmem_size_scale) > (vm_kmem_size / PAGE_SIZE))
617                 vm_kmem_size = (mem_size / vm_kmem_size_scale) * PAGE_SIZE;
618
619 #if defined(VM_KMEM_SIZE_MIN)
620         vm_kmem_size_min = VM_KMEM_SIZE_MIN;
621 #endif
622         TUNABLE_ULONG_FETCH("vm.kmem_size_min", &vm_kmem_size_min);
623         if (vm_kmem_size_min > 0 && vm_kmem_size < vm_kmem_size_min) {
624                 vm_kmem_size = vm_kmem_size_min;
625         }
626
627 #if defined(VM_KMEM_SIZE_MAX)
628         vm_kmem_size_max = VM_KMEM_SIZE_MAX;
629 #endif
630         TUNABLE_ULONG_FETCH("vm.kmem_size_max", &vm_kmem_size_max);
631         if (vm_kmem_size_max > 0 && vm_kmem_size >= vm_kmem_size_max)
632                 vm_kmem_size = vm_kmem_size_max;
633
634         /* Allow final override from the kernel environment */
635         TUNABLE_ULONG_FETCH("vm.kmem_size", &vm_kmem_size);
636
637         /*
638          * Limit kmem virtual size to twice the physical memory.
639          * This allows for kmem map sparseness, but limits the size
640          * to something sane. Be careful to not overflow the 32bit
641          * ints while doing the check.
642          */
643         if (((vm_kmem_size / 2) / PAGE_SIZE) > cnt.v_page_count)
644                 vm_kmem_size = 2 * cnt.v_page_count * PAGE_SIZE;
645
646         /*
647          * Tune settings based on the kmem map's size at this time.
648          */
649         init_param3(vm_kmem_size / PAGE_SIZE);
650
651 #ifdef DEBUG_MEMGUARD
652         tmp = memguard_fudge(vm_kmem_size, vm_kmem_size_max);
653 #else
654         tmp = vm_kmem_size;
655 #endif
656         kmem_map = kmem_suballoc(kernel_map, &kmembase, &kmemlimit,
657             tmp, TRUE);
658         kmem_map->system_map = 1;
659
660 #ifdef DEBUG_MEMGUARD
661         /*
662          * Initialize MemGuard if support compiled in.  MemGuard is a
663          * replacement allocator used for detecting tamper-after-free
664          * scenarios as they occur.  It is only used for debugging.
665          */
666         memguard_init(kmem_map);
667 #endif
668
669         uma_startup2();
670
671         mt_zone = uma_zcreate("mt_zone", sizeof(struct malloc_type_internal),
672 #ifdef INVARIANTS
673             mtrash_ctor, mtrash_dtor, mtrash_init, mtrash_fini,
674 #else
675             NULL, NULL, NULL, NULL,
676 #endif
677             UMA_ALIGN_PTR, UMA_ZONE_MALLOC);
678         for (i = 0, indx = 0; kmemzones[indx].kz_size != 0; indx++) {
679                 int size = kmemzones[indx].kz_size;
680                 char *name = kmemzones[indx].kz_name;
681
682                 kmemzones[indx].kz_zone = uma_zcreate(name, size,
683 #ifdef INVARIANTS
684                     mtrash_ctor, mtrash_dtor, mtrash_init, mtrash_fini,
685 #else
686                     NULL, NULL, NULL, NULL,
687 #endif
688                     UMA_ALIGN_PTR, UMA_ZONE_MALLOC);
689                     
690                 for (;i <= size; i+= KMEM_ZBASE)
691                         kmemsize[i >> KMEM_ZSHIFT] = indx;
692                 
693         }
694 }
695
696 void
697 malloc_init(void *data)
698 {
699         struct malloc_type_internal *mtip;
700         struct malloc_type *mtp;
701
702         KASSERT(cnt.v_page_count != 0, ("malloc_register before vm_init"));
703
704         mtp = data;
705         if (mtp->ks_magic != M_MAGIC)
706                 panic("malloc_init: bad malloc type magic");
707
708         mtip = uma_zalloc(mt_zone, M_WAITOK | M_ZERO);
709         mtp->ks_handle = mtip;
710
711         mtx_lock(&malloc_mtx);
712         mtp->ks_next = kmemstatistics;
713         kmemstatistics = mtp;
714         kmemcount++;
715         mtx_unlock(&malloc_mtx);
716 }
717
718 void
719 malloc_uninit(void *data)
720 {
721         struct malloc_type_internal *mtip;
722         struct malloc_type_stats *mtsp;
723         struct malloc_type *mtp, *temp;
724         uma_slab_t slab;
725         long temp_allocs, temp_bytes;
726         int i;
727
728         mtp = data;
729         KASSERT(mtp->ks_magic == M_MAGIC,
730             ("malloc_uninit: bad malloc type magic"));
731         KASSERT(mtp->ks_handle != NULL, ("malloc_deregister: cookie NULL"));
732
733         mtx_lock(&malloc_mtx);
734         mtip = mtp->ks_handle;
735         mtp->ks_handle = NULL;
736         if (mtp != kmemstatistics) {
737                 for (temp = kmemstatistics; temp != NULL;
738                     temp = temp->ks_next) {
739                         if (temp->ks_next == mtp) {
740                                 temp->ks_next = mtp->ks_next;
741                                 break;
742                         }
743                 }
744                 KASSERT(temp,
745                     ("malloc_uninit: type '%s' not found", mtp->ks_shortdesc));
746         } else
747                 kmemstatistics = mtp->ks_next;
748         kmemcount--;
749         mtx_unlock(&malloc_mtx);
750
751         /*
752          * Look for memory leaks.
753          */
754         temp_allocs = temp_bytes = 0;
755         for (i = 0; i < MAXCPU; i++) {
756                 mtsp = &mtip->mti_stats[i];
757                 temp_allocs += mtsp->mts_numallocs;
758                 temp_allocs -= mtsp->mts_numfrees;
759                 temp_bytes += mtsp->mts_memalloced;
760                 temp_bytes -= mtsp->mts_memfreed;
761         }
762         if (temp_allocs > 0 || temp_bytes > 0) {
763                 printf("Warning: memory type %s leaked memory on destroy "
764                     "(%ld allocations, %ld bytes leaked).\n", mtp->ks_shortdesc,
765                     temp_allocs, temp_bytes);
766         }
767
768         slab = vtoslab((vm_offset_t) mtip & (~UMA_SLAB_MASK));
769         uma_zfree_arg(mt_zone, mtip, slab);
770 }
771
772 struct malloc_type *
773 malloc_desc2type(const char *desc)
774 {
775         struct malloc_type *mtp;
776
777         mtx_assert(&malloc_mtx, MA_OWNED);
778         for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) {
779                 if (strcmp(mtp->ks_shortdesc, desc) == 0)
780                         return (mtp);
781         }
782         return (NULL);
783 }
784
785 static int
786 sysctl_kern_malloc_stats(SYSCTL_HANDLER_ARGS)
787 {
788         struct malloc_type_stream_header mtsh;
789         struct malloc_type_internal *mtip;
790         struct malloc_type_header mth;
791         struct malloc_type *mtp;
792         int buflen, count, error, i;
793         struct sbuf sbuf;
794         char *buffer;
795
796         mtx_lock(&malloc_mtx);
797 restart:
798         mtx_assert(&malloc_mtx, MA_OWNED);
799         count = kmemcount;
800         mtx_unlock(&malloc_mtx);
801         buflen = sizeof(mtsh) + count * (sizeof(mth) +
802             sizeof(struct malloc_type_stats) * MAXCPU) + 1;
803         buffer = malloc(buflen, M_TEMP, M_WAITOK | M_ZERO);
804         mtx_lock(&malloc_mtx);
805         if (count < kmemcount) {
806                 free(buffer, M_TEMP);
807                 goto restart;
808         }
809
810         sbuf_new(&sbuf, buffer, buflen, SBUF_FIXEDLEN);
811
812         /*
813          * Insert stream header.
814          */
815         bzero(&mtsh, sizeof(mtsh));
816         mtsh.mtsh_version = MALLOC_TYPE_STREAM_VERSION;
817         mtsh.mtsh_maxcpus = MAXCPU;
818         mtsh.mtsh_count = kmemcount;
819         if (sbuf_bcat(&sbuf, &mtsh, sizeof(mtsh)) < 0) {
820                 mtx_unlock(&malloc_mtx);
821                 error = ENOMEM;
822                 goto out;
823         }
824
825         /*
826          * Insert alternating sequence of type headers and type statistics.
827          */
828         for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) {
829                 mtip = (struct malloc_type_internal *)mtp->ks_handle;
830
831                 /*
832                  * Insert type header.
833                  */
834                 bzero(&mth, sizeof(mth));
835                 strlcpy(mth.mth_name, mtp->ks_shortdesc, MALLOC_MAX_NAME);
836                 if (sbuf_bcat(&sbuf, &mth, sizeof(mth)) < 0) {
837                         mtx_unlock(&malloc_mtx);
838                         error = ENOMEM;
839                         goto out;
840                 }
841
842                 /*
843                  * Insert type statistics for each CPU.
844                  */
845                 for (i = 0; i < MAXCPU; i++) {
846                         if (sbuf_bcat(&sbuf, &mtip->mti_stats[i],
847                             sizeof(mtip->mti_stats[i])) < 0) {
848                                 mtx_unlock(&malloc_mtx);
849                                 error = ENOMEM;
850                                 goto out;
851                         }
852                 }
853         }
854         mtx_unlock(&malloc_mtx);
855         sbuf_finish(&sbuf);
856         error = SYSCTL_OUT(req, sbuf_data(&sbuf), sbuf_len(&sbuf));
857 out:
858         sbuf_delete(&sbuf);
859         free(buffer, M_TEMP);
860         return (error);
861 }
862
863 SYSCTL_PROC(_kern, OID_AUTO, malloc_stats, CTLFLAG_RD|CTLTYPE_STRUCT,
864     0, 0, sysctl_kern_malloc_stats, "s,malloc_type_ustats",
865     "Return malloc types");
866
867 SYSCTL_INT(_kern, OID_AUTO, malloc_count, CTLFLAG_RD, &kmemcount, 0,
868     "Count of kernel malloc types");
869
870 void
871 malloc_type_list(malloc_type_list_func_t *func, void *arg)
872 {
873         struct malloc_type *mtp, **bufmtp;
874         int count, i;
875         size_t buflen;
876
877         mtx_lock(&malloc_mtx);
878 restart:
879         mtx_assert(&malloc_mtx, MA_OWNED);
880         count = kmemcount;
881         mtx_unlock(&malloc_mtx);
882
883         buflen = sizeof(struct malloc_type *) * count;
884         bufmtp = malloc(buflen, M_TEMP, M_WAITOK);
885
886         mtx_lock(&malloc_mtx);
887
888         if (count < kmemcount) {
889                 free(bufmtp, M_TEMP);
890                 goto restart;
891         }
892
893         for (mtp = kmemstatistics, i = 0; mtp != NULL; mtp = mtp->ks_next, i++)
894                 bufmtp[i] = mtp;
895
896         mtx_unlock(&malloc_mtx);
897
898         for (i = 0; i < count; i++)
899                 (func)(bufmtp[i], arg);
900
901         free(bufmtp, M_TEMP);
902 }
903
904 #ifdef DDB
905 DB_SHOW_COMMAND(malloc, db_show_malloc)
906 {
907         struct malloc_type_internal *mtip;
908         struct malloc_type *mtp;
909         u_int64_t allocs, frees;
910         u_int64_t alloced, freed;
911         int i;
912
913         db_printf("%18s %12s  %12s %12s\n", "Type", "InUse", "MemUse",
914             "Requests");
915         for (mtp = kmemstatistics; mtp != NULL; mtp = mtp->ks_next) {
916                 mtip = (struct malloc_type_internal *)mtp->ks_handle;
917                 allocs = 0;
918                 frees = 0;
919                 alloced = 0;
920                 freed = 0;
921                 for (i = 0; i < MAXCPU; i++) {
922                         allocs += mtip->mti_stats[i].mts_numallocs;
923                         frees += mtip->mti_stats[i].mts_numfrees;
924                         alloced += mtip->mti_stats[i].mts_memalloced;
925                         freed += mtip->mti_stats[i].mts_memfreed;
926                 }
927                 db_printf("%18s %12ju %12juK %12ju\n",
928                     mtp->ks_shortdesc, allocs - frees,
929                     (alloced - freed + 1023) / 1024, allocs);
930         }
931 }
932 #endif
933
934 #ifdef MALLOC_PROFILE
935
936 static int
937 sysctl_kern_mprof(SYSCTL_HANDLER_ARGS)
938 {
939         int linesize = 64;
940         struct sbuf sbuf;
941         uint64_t count;
942         uint64_t waste;
943         uint64_t mem;
944         int bufsize;
945         int error;
946         char *buf;
947         int rsize;
948         int size;
949         int i;
950
951         bufsize = linesize * (KMEM_ZSIZE + 1);
952         bufsize += 128;         /* For the stats line */
953         bufsize += 128;         /* For the banner line */
954         waste = 0;
955         mem = 0;
956
957         buf = malloc(bufsize, M_TEMP, M_WAITOK|M_ZERO);
958         sbuf_new(&sbuf, buf, bufsize, SBUF_FIXEDLEN);
959         sbuf_printf(&sbuf, 
960             "\n  Size                    Requests  Real Size\n");
961         for (i = 0; i < KMEM_ZSIZE; i++) {
962                 size = i << KMEM_ZSHIFT;
963                 rsize = kmemzones[kmemsize[i]].kz_size;
964                 count = (long long unsigned)krequests[i];
965
966                 sbuf_printf(&sbuf, "%6d%28llu%11d\n", size,
967                     (unsigned long long)count, rsize);
968
969                 if ((rsize * count) > (size * count))
970                         waste += (rsize * count) - (size * count);
971                 mem += (rsize * count);
972         }
973         sbuf_printf(&sbuf,
974             "\nTotal memory used:\t%30llu\nTotal Memory wasted:\t%30llu\n",
975             (unsigned long long)mem, (unsigned long long)waste);
976         sbuf_finish(&sbuf);
977
978         error = SYSCTL_OUT(req, sbuf_data(&sbuf), sbuf_len(&sbuf));
979
980         sbuf_delete(&sbuf);
981         free(buf, M_TEMP);
982         return (error);
983 }
984
985 SYSCTL_OID(_kern, OID_AUTO, mprof, CTLTYPE_STRING|CTLFLAG_RD,
986     NULL, 0, sysctl_kern_mprof, "A", "Malloc Profiling");
987 #endif /* MALLOC_PROFILE */