Home | History | Annotate | Line # | Download | only in kern
kern_malloc.c revision 1.121.2.1
      1  1.121.2.1     skrll /*	$NetBSD: kern_malloc.c,v 1.121.2.1 2009/01/19 13:19:38 skrll Exp $	*/
      2        1.9       cgd 
      3        1.1       cgd /*
      4        1.8       cgd  * Copyright (c) 1987, 1991, 1993
      5        1.8       cgd  *	The Regents of the University of California.  All rights reserved.
      6        1.1       cgd  *
      7        1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8        1.1       cgd  * modification, are permitted provided that the following conditions
      9        1.1       cgd  * are met:
     10        1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11        1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12        1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13        1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14        1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15       1.81       agc  * 3. Neither the name of the University nor the names of its contributors
     16       1.81       agc  *    may be used to endorse or promote products derived from this software
     17       1.81       agc  *    without specific prior written permission.
     18       1.81       agc  *
     19       1.81       agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20       1.81       agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21       1.81       agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22       1.81       agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23       1.81       agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24       1.81       agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25       1.81       agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26       1.81       agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27       1.81       agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28       1.81       agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29       1.81       agc  * SUCH DAMAGE.
     30       1.81       agc  *
     31       1.81       agc  *	@(#)kern_malloc.c	8.4 (Berkeley) 5/20/95
     32       1.81       agc  */
     33       1.81       agc 
     34       1.81       agc /*
     35       1.81       agc  * Copyright (c) 1996 Christopher G. Demetriou.  All rights reserved.
     36       1.81       agc  *
     37       1.81       agc  * Redistribution and use in source and binary forms, with or without
     38       1.81       agc  * modification, are permitted provided that the following conditions
     39       1.81       agc  * are met:
     40       1.81       agc  * 1. Redistributions of source code must retain the above copyright
     41       1.81       agc  *    notice, this list of conditions and the following disclaimer.
     42       1.81       agc  * 2. Redistributions in binary form must reproduce the above copyright
     43       1.81       agc  *    notice, this list of conditions and the following disclaimer in the
     44       1.81       agc  *    documentation and/or other materials provided with the distribution.
     45        1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     46        1.1       cgd  *    must display the following acknowledgement:
     47        1.1       cgd  *	This product includes software developed by the University of
     48        1.1       cgd  *	California, Berkeley and its contributors.
     49        1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     50        1.1       cgd  *    may be used to endorse or promote products derived from this software
     51        1.1       cgd  *    without specific prior written permission.
     52        1.1       cgd  *
     53        1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     54        1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     55        1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     56        1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     57        1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     58        1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     59        1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     60        1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     61        1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     62        1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     63        1.1       cgd  * SUCH DAMAGE.
     64        1.1       cgd  *
     65       1.32      fvdl  *	@(#)kern_malloc.c	8.4 (Berkeley) 5/20/95
     66        1.1       cgd  */
     67       1.64     lukem 
     68       1.64     lukem #include <sys/cdefs.h>
     69  1.121.2.1     skrll __KERNEL_RCSID(0, "$NetBSD: kern_malloc.c,v 1.121.2.1 2009/01/19 13:19:38 skrll Exp $");
     70        1.1       cgd 
     71        1.7   mycroft #include <sys/param.h>
     72        1.7   mycroft #include <sys/proc.h>
     73        1.7   mycroft #include <sys/kernel.h>
     74        1.7   mycroft #include <sys/malloc.h>
     75       1.12  christos #include <sys/systm.h>
     76      1.106        ad #include <sys/debug.h>
     77      1.109        ad #include <sys/mutex.h>
     78      1.113        ad #include <sys/lockdebug.h>
     79       1.24   thorpej 
     80       1.28       mrg #include <uvm/uvm_extern.h>
     81       1.28       mrg 
     82       1.92      yamt static struct vm_map_kernel kmem_map_store;
     83       1.58       chs struct vm_map *kmem_map = NULL;
     84       1.28       mrg 
     85       1.49   thorpej #include "opt_kmempages.h"
     86       1.49   thorpej 
     87       1.49   thorpej #ifdef NKMEMCLUSTERS
     88       1.52  sommerfe #error NKMEMCLUSTERS is obsolete; remove it from your kernel config file and use NKMEMPAGES instead or let the kernel auto-size
     89       1.49   thorpej #endif
     90       1.49   thorpej 
     91       1.49   thorpej /*
     92       1.49   thorpej  * Default number of pages in kmem_map.  We attempt to calculate this
     93       1.49   thorpej  * at run-time, but allow it to be either patched or set in the kernel
     94       1.49   thorpej  * config file.
     95       1.49   thorpej  */
     96       1.49   thorpej #ifndef NKMEMPAGES
     97       1.49   thorpej #define	NKMEMPAGES	0
     98       1.49   thorpej #endif
     99       1.49   thorpej int	nkmempages = NKMEMPAGES;
    100       1.49   thorpej 
    101       1.49   thorpej /*
    102       1.49   thorpej  * Defaults for lower- and upper-bounds for the kmem_map page count.
    103       1.49   thorpej  * Can be overridden by kernel config options.
    104       1.49   thorpej  */
    105       1.49   thorpej #ifndef	NKMEMPAGES_MIN
    106       1.49   thorpej #define	NKMEMPAGES_MIN	NKMEMPAGES_MIN_DEFAULT
    107       1.49   thorpej #endif
    108       1.49   thorpej 
    109       1.49   thorpej #ifndef NKMEMPAGES_MAX
    110       1.49   thorpej #define	NKMEMPAGES_MAX	NKMEMPAGES_MAX_DEFAULT
    111       1.49   thorpej #endif
    112       1.49   thorpej 
    113       1.24   thorpej #include "opt_kmemstats.h"
    114       1.27   thorpej #include "opt_malloclog.h"
    115       1.71      fvdl #include "opt_malloc_debug.h"
    116       1.12  christos 
    117      1.103       chs #define	MINALLOCSIZE	(1 << MINBUCKET)
    118      1.103       chs #define	BUCKETINDX(size) \
    119      1.103       chs 	((size) <= (MINALLOCSIZE * 128) \
    120      1.103       chs 		? (size) <= (MINALLOCSIZE * 8) \
    121      1.103       chs 			? (size) <= (MINALLOCSIZE * 2) \
    122      1.103       chs 				? (size) <= (MINALLOCSIZE * 1) \
    123      1.103       chs 					? (MINBUCKET + 0) \
    124      1.103       chs 					: (MINBUCKET + 1) \
    125      1.103       chs 				: (size) <= (MINALLOCSIZE * 4) \
    126      1.103       chs 					? (MINBUCKET + 2) \
    127      1.103       chs 					: (MINBUCKET + 3) \
    128      1.103       chs 			: (size) <= (MINALLOCSIZE* 32) \
    129      1.103       chs 				? (size) <= (MINALLOCSIZE * 16) \
    130      1.103       chs 					? (MINBUCKET + 4) \
    131      1.103       chs 					: (MINBUCKET + 5) \
    132      1.103       chs 				: (size) <= (MINALLOCSIZE * 64) \
    133      1.103       chs 					? (MINBUCKET + 6) \
    134      1.103       chs 					: (MINBUCKET + 7) \
    135      1.103       chs 		: (size) <= (MINALLOCSIZE * 2048) \
    136      1.103       chs 			? (size) <= (MINALLOCSIZE * 512) \
    137      1.103       chs 				? (size) <= (MINALLOCSIZE * 256) \
    138      1.103       chs 					? (MINBUCKET + 8) \
    139      1.103       chs 					: (MINBUCKET + 9) \
    140      1.103       chs 				: (size) <= (MINALLOCSIZE * 1024) \
    141      1.103       chs 					? (MINBUCKET + 10) \
    142      1.103       chs 					: (MINBUCKET + 11) \
    143      1.103       chs 			: (size) <= (MINALLOCSIZE * 8192) \
    144      1.103       chs 				? (size) <= (MINALLOCSIZE * 4096) \
    145      1.103       chs 					? (MINBUCKET + 12) \
    146      1.103       chs 					: (MINBUCKET + 13) \
    147      1.103       chs 				: (size) <= (MINALLOCSIZE * 16384) \
    148      1.103       chs 					? (MINBUCKET + 14) \
    149      1.103       chs 					: (MINBUCKET + 15))
    150      1.103       chs 
    151      1.103       chs /*
    152      1.103       chs  * Array of descriptors that describe the contents of each page
    153      1.103       chs  */
    154      1.103       chs struct kmemusage {
    155      1.103       chs 	short ku_indx;		/* bucket index */
    156      1.103       chs 	union {
    157      1.103       chs 		u_short freecnt;/* for small allocations, free pieces in page */
    158      1.103       chs 		u_short pagecnt;/* for large allocations, pages alloced */
    159      1.103       chs 	} ku_un;
    160      1.103       chs };
    161      1.103       chs #define	ku_freecnt ku_un.freecnt
    162      1.103       chs #define	ku_pagecnt ku_un.pagecnt
    163      1.103       chs 
    164       1.99       chs struct kmembuckets kmembuckets[MINBUCKET + 16];
    165        1.1       cgd struct kmemusage *kmemusage;
    166        1.1       cgd char *kmembase, *kmemlimit;
    167       1.77   thorpej 
    168      1.106        ad #ifdef DEBUG
    169      1.106        ad static void *malloc_freecheck;
    170      1.106        ad #endif
    171      1.106        ad 
    172      1.103       chs /*
    173      1.103       chs  * Turn virtual addresses into kmem map indicies
    174      1.103       chs  */
    175      1.108  christos #define	btokup(addr)	(&kmemusage[((char *)(addr) - kmembase) >> PGSHIFT])
    176      1.103       chs 
    177       1.77   thorpej struct malloc_type *kmemstatistics;
    178        1.1       cgd 
    179       1.27   thorpej #ifdef MALLOCLOG
    180       1.27   thorpej #ifndef MALLOCLOGSIZE
    181       1.27   thorpej #define	MALLOCLOGSIZE	100000
    182       1.27   thorpej #endif
    183       1.27   thorpej 
    184       1.27   thorpej struct malloclog {
    185       1.27   thorpej 	void *addr;
    186       1.27   thorpej 	long size;
    187       1.77   thorpej 	struct malloc_type *type;
    188       1.27   thorpej 	int action;
    189       1.27   thorpej 	const char *file;
    190       1.27   thorpej 	long line;
    191       1.27   thorpej } malloclog[MALLOCLOGSIZE];
    192       1.27   thorpej 
    193       1.27   thorpej long	malloclogptr;
    194       1.27   thorpej 
    195      1.121     blymn /*
    196      1.121     blymn  * Fuzz factor for neighbour address match this must be a mask of the lower
    197      1.121     blymn  * bits we wish to ignore when comparing addresses
    198      1.121     blymn  */
    199      1.121     blymn __uintptr_t malloclog_fuzz = 0x7FL;
    200      1.121     blymn 
    201      1.121     blymn 
    202       1.27   thorpej static void
    203       1.77   thorpej domlog(void *a, long size, struct malloc_type *type, int action,
    204       1.77   thorpej     const char *file, long line)
    205       1.27   thorpej {
    206       1.27   thorpej 
    207       1.27   thorpej 	malloclog[malloclogptr].addr = a;
    208       1.27   thorpej 	malloclog[malloclogptr].size = size;
    209       1.27   thorpej 	malloclog[malloclogptr].type = type;
    210       1.27   thorpej 	malloclog[malloclogptr].action = action;
    211       1.27   thorpej 	malloclog[malloclogptr].file = file;
    212       1.27   thorpej 	malloclog[malloclogptr].line = line;
    213       1.27   thorpej 	malloclogptr++;
    214       1.27   thorpej 	if (malloclogptr >= MALLOCLOGSIZE)
    215       1.27   thorpej 		malloclogptr = 0;
    216       1.27   thorpej }
    217       1.27   thorpej 
    218       1.27   thorpej static void
    219       1.69     enami hitmlog(void *a)
    220       1.27   thorpej {
    221       1.27   thorpej 	struct malloclog *lp;
    222       1.27   thorpej 	long l;
    223       1.27   thorpej 
    224       1.69     enami #define	PRT do { \
    225       1.88   mycroft 	lp = &malloclog[l]; \
    226       1.88   mycroft 	if (lp->addr == a && lp->action) { \
    227       1.27   thorpej 		printf("malloc log entry %ld:\n", l); \
    228       1.27   thorpej 		printf("\taddr = %p\n", lp->addr); \
    229       1.27   thorpej 		printf("\tsize = %ld\n", lp->size); \
    230       1.77   thorpej 		printf("\ttype = %s\n", lp->type->ks_shortdesc); \
    231       1.27   thorpej 		printf("\taction = %s\n", lp->action == 1 ? "alloc" : "free"); \
    232       1.27   thorpej 		printf("\tfile = %s\n", lp->file); \
    233       1.27   thorpej 		printf("\tline = %ld\n", lp->line); \
    234       1.69     enami 	} \
    235       1.69     enami } while (/* CONSTCOND */0)
    236       1.27   thorpej 
    237      1.121     blymn /*
    238      1.121     blymn  * Print fuzzy matched "neighbour" - look for the memory block that has
    239      1.121     blymn  * been allocated below the address we are interested in.  We look for a
    240      1.121     blymn  * base address + size that is within malloclog_fuzz of our target
    241      1.121     blymn  * address. If the base address and target address are the same then it is
    242      1.121     blymn  * likely we have found a free (size is 0 in this case) so we won't report
    243      1.121     blymn  * those, they will get reported by PRT anyway.
    244      1.121     blymn  */
    245      1.121     blymn #define	NPRT do { \
    246      1.121     blymn 	__uintptr_t fuzz_mask = ~(malloclog_fuzz); \
    247      1.121     blymn 	lp = &malloclog[l]; \
    248      1.121     blymn 	if ((__uintptr_t)lp->addr != (__uintptr_t)a && \
    249      1.121     blymn 	    (((__uintptr_t)lp->addr + lp->size + malloclog_fuzz) & fuzz_mask) \
    250      1.121     blymn 	    == ((__uintptr_t)a & fuzz_mask) && lp->action) {		\
    251      1.121     blymn 		printf("neighbour malloc log entry %ld:\n", l); \
    252      1.121     blymn 		printf("\taddr = %p\n", lp->addr); \
    253      1.121     blymn 		printf("\tsize = %ld\n", lp->size); \
    254      1.121     blymn 		printf("\ttype = %s\n", lp->type->ks_shortdesc); \
    255      1.121     blymn 		printf("\taction = %s\n", lp->action == 1 ? "alloc" : "free"); \
    256      1.121     blymn 		printf("\tfile = %s\n", lp->file); \
    257      1.121     blymn 		printf("\tline = %ld\n", lp->line); \
    258      1.121     blymn 	} \
    259      1.121     blymn } while (/* CONSTCOND */0)
    260      1.121     blymn 
    261      1.121     blymn 	for (l = malloclogptr; l < MALLOCLOGSIZE; l++) {
    262       1.69     enami 		PRT;
    263      1.121     blymn 		NPRT;
    264      1.121     blymn 	}
    265      1.121     blymn 
    266       1.27   thorpej 
    267      1.121     blymn 	for (l = 0; l < malloclogptr; l++) {
    268       1.69     enami 		PRT;
    269      1.121     blymn 		NPRT;
    270      1.121     blymn 	}
    271      1.121     blymn 
    272       1.88   mycroft #undef PRT
    273       1.27   thorpej }
    274       1.27   thorpej #endif /* MALLOCLOG */
    275       1.27   thorpej 
    276        1.8       cgd #ifdef DIAGNOSTIC
    277        1.8       cgd /*
    278        1.8       cgd  * This structure provides a set of masks to catch unaligned frees.
    279        1.8       cgd  */
    280       1.57  jdolecek const long addrmask[] = { 0,
    281        1.8       cgd 	0x00000001, 0x00000003, 0x00000007, 0x0000000f,
    282        1.8       cgd 	0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff,
    283        1.8       cgd 	0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff,
    284        1.8       cgd 	0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff,
    285        1.8       cgd };
    286        1.8       cgd 
    287        1.8       cgd /*
    288        1.8       cgd  * The WEIRD_ADDR is used as known text to copy into free objects so
    289        1.8       cgd  * that modifications after frees can be detected.
    290        1.8       cgd  */
    291       1.76   thorpej #define	WEIRD_ADDR	((uint32_t) 0xdeadbeef)
    292       1.55       chs #ifdef DEBUG
    293       1.69     enami #define	MAX_COPY	PAGE_SIZE
    294       1.55       chs #else
    295       1.69     enami #define	MAX_COPY	32
    296       1.55       chs #endif
    297        1.8       cgd 
    298        1.8       cgd /*
    299       1.11       cgd  * Normally the freelist structure is used only to hold the list pointer
    300       1.11       cgd  * for free objects.  However, when running with diagnostics, the first
    301       1.77   thorpej  * 8/16 bytes of the structure is unused except for diagnostic information,
    302       1.77   thorpej  * and the free list pointer is at offset 8/16 in the structure.  Since the
    303       1.11       cgd  * first 8 bytes is the portion of the structure most often modified, this
    304       1.11       cgd  * helps to detect memory reuse problems and avoid free list corruption.
    305        1.8       cgd  */
    306        1.8       cgd struct freelist {
    307       1.76   thorpej 	uint32_t spare0;
    308       1.77   thorpej #ifdef _LP64
    309       1.77   thorpej 	uint32_t spare1;		/* explicit padding */
    310       1.77   thorpej #endif
    311       1.77   thorpej 	struct malloc_type *type;
    312      1.108  christos 	void *	next;
    313        1.8       cgd };
    314        1.8       cgd #else /* !DIAGNOSTIC */
    315        1.8       cgd struct freelist {
    316      1.108  christos 	void *	next;
    317        1.8       cgd };
    318        1.8       cgd #endif /* DIAGNOSTIC */
    319        1.8       cgd 
    320      1.109        ad kmutex_t malloc_lock;
    321       1.78        pk 
    322       1.77   thorpej /*
    323        1.1       cgd  * Allocate a block of memory
    324        1.1       cgd  */
    325       1.27   thorpej #ifdef MALLOCLOG
    326       1.27   thorpej void *
    327  1.121.2.1     skrll _kern_malloc(unsigned long size, struct malloc_type *ksp, int flags,
    328       1.77   thorpej     const char *file, long line)
    329       1.27   thorpej #else
    330        1.1       cgd void *
    331  1.121.2.1     skrll kern_malloc(unsigned long size, struct malloc_type *ksp, int flags)
    332       1.27   thorpej #endif /* MALLOCLOG */
    333        1.1       cgd {
    334       1.50  augustss 	struct kmembuckets *kbp;
    335       1.50  augustss 	struct kmemusage *kup;
    336       1.50  augustss 	struct freelist *freep;
    337        1.5    andrew 	long indx, npg, allocsize;
    338      1.108  christos 	char *va, *cp, *savedlist;
    339        1.8       cgd #ifdef DIAGNOSTIC
    340       1.76   thorpej 	uint32_t *end, *lp;
    341        1.8       cgd 	int copysize;
    342        1.8       cgd #endif
    343        1.1       cgd 
    344       1.59   thorpej #ifdef LOCKDEBUG
    345      1.119        ad 	if ((flags & M_NOWAIT) == 0) {
    346      1.118      yamt 		ASSERT_SLEEPABLE();
    347      1.119        ad 	}
    348       1.59   thorpej #endif
    349       1.62   thorpej #ifdef MALLOC_DEBUG
    350      1.106        ad 	if (debug_malloc(size, ksp, flags, (void *) &va)) {
    351  1.121.2.1     skrll 		if (va != 0) {
    352      1.106        ad 			FREECHECK_OUT(&malloc_freecheck, (void *)va);
    353  1.121.2.1     skrll 		}
    354       1.62   thorpej 		return ((void *) va);
    355      1.106        ad 	}
    356       1.62   thorpej #endif
    357        1.1       cgd 	indx = BUCKETINDX(size);
    358       1.99       chs 	kbp = &kmembuckets[indx];
    359      1.113        ad 	mutex_spin_enter(&malloc_lock);
    360        1.1       cgd #ifdef KMEMSTATS
    361        1.1       cgd 	while (ksp->ks_memuse >= ksp->ks_limit) {
    362        1.1       cgd 		if (flags & M_NOWAIT) {
    363      1.113        ad 			mutex_spin_exit(&malloc_lock);
    364        1.1       cgd 			return ((void *) NULL);
    365        1.1       cgd 		}
    366        1.1       cgd 		if (ksp->ks_limblocks < 65535)
    367        1.1       cgd 			ksp->ks_limblocks++;
    368      1.109        ad 		mtsleep((void *)ksp, PSWP+2, ksp->ks_shortdesc, 0,
    369      1.109        ad 			&malloc_lock);
    370        1.1       cgd 	}
    371        1.8       cgd 	ksp->ks_size |= 1 << indx;
    372        1.8       cgd #endif
    373        1.8       cgd #ifdef DIAGNOSTIC
    374        1.8       cgd 	copysize = 1 << indx < MAX_COPY ? 1 << indx : MAX_COPY;
    375        1.1       cgd #endif
    376        1.1       cgd 	if (kbp->kb_next == NULL) {
    377      1.111      yamt 		int s;
    378        1.8       cgd 		kbp->kb_last = NULL;
    379        1.1       cgd 		if (size > MAXALLOCSAVE)
    380       1.66     enami 			allocsize = round_page(size);
    381        1.1       cgd 		else
    382        1.1       cgd 			allocsize = 1 << indx;
    383       1.47     ragge 		npg = btoc(allocsize);
    384      1.113        ad 		mutex_spin_exit(&malloc_lock);
    385      1.111      yamt 		s = splvm();
    386      1.108  christos 		va = (void *) uvm_km_alloc(kmem_map,
    387       1.97      yamt 		    (vsize_t)ctob(npg), 0,
    388       1.73       chs 		    ((flags & M_NOWAIT) ? UVM_KMF_NOWAIT : 0) |
    389       1.97      yamt 		    ((flags & M_CANFAIL) ? UVM_KMF_CANFAIL : 0) |
    390       1.97      yamt 		    UVM_KMF_WIRED);
    391      1.111      yamt 		splx(s);
    392       1.51   thorpej 		if (__predict_false(va == NULL)) {
    393       1.17       cgd 			/*
    394       1.17       cgd 			 * Kmem_malloc() can return NULL, even if it can
    395       1.91    simonb 			 * wait, if there is no map space available, because
    396       1.17       cgd 			 * it can't fix that problem.  Neither can we,
    397       1.17       cgd 			 * right now.  (We should release pages which
    398       1.99       chs 			 * are completely free and which are in kmembuckets
    399       1.17       cgd 			 * with too many free elements.)
    400       1.17       cgd 			 */
    401       1.68  jdolecek 			if ((flags & (M_NOWAIT|M_CANFAIL)) == 0)
    402       1.17       cgd 				panic("malloc: out of space in kmem_map");
    403       1.73       chs 			return (NULL);
    404        1.1       cgd 		}
    405      1.113        ad 		mutex_spin_enter(&malloc_lock);
    406        1.1       cgd #ifdef KMEMSTATS
    407        1.1       cgd 		kbp->kb_total += kbp->kb_elmpercl;
    408        1.1       cgd #endif
    409        1.1       cgd 		kup = btokup(va);
    410        1.1       cgd 		kup->ku_indx = indx;
    411        1.1       cgd 		if (allocsize > MAXALLOCSAVE) {
    412        1.1       cgd 			if (npg > 65535)
    413        1.1       cgd 				panic("malloc: allocation too large");
    414        1.1       cgd 			kup->ku_pagecnt = npg;
    415        1.1       cgd #ifdef KMEMSTATS
    416        1.1       cgd 			ksp->ks_memuse += allocsize;
    417        1.1       cgd #endif
    418        1.1       cgd 			goto out;
    419        1.1       cgd 		}
    420        1.1       cgd #ifdef KMEMSTATS
    421        1.1       cgd 		kup->ku_freecnt = kbp->kb_elmpercl;
    422        1.1       cgd 		kbp->kb_totalfree += kbp->kb_elmpercl;
    423        1.1       cgd #endif
    424        1.1       cgd 		/*
    425        1.1       cgd 		 * Just in case we blocked while allocating memory,
    426        1.1       cgd 		 * and someone else also allocated memory for this
    427       1.99       chs 		 * kmembucket, don't assume the list is still empty.
    428        1.1       cgd 		 */
    429        1.1       cgd 		savedlist = kbp->kb_next;
    430       1.49   thorpej 		kbp->kb_next = cp = va + (npg << PAGE_SHIFT) - allocsize;
    431        1.8       cgd 		for (;;) {
    432        1.8       cgd 			freep = (struct freelist *)cp;
    433        1.8       cgd #ifdef DIAGNOSTIC
    434        1.8       cgd 			/*
    435        1.8       cgd 			 * Copy in known text to detect modification
    436        1.8       cgd 			 * after freeing.
    437        1.8       cgd 			 */
    438       1.86     ragge 			end = (uint32_t *)&cp[copysize];
    439       1.86     ragge 			for (lp = (uint32_t *)cp; lp < end; lp++)
    440        1.8       cgd 				*lp = WEIRD_ADDR;
    441        1.8       cgd 			freep->type = M_FREE;
    442        1.8       cgd #endif /* DIAGNOSTIC */
    443        1.8       cgd 			if (cp <= va)
    444        1.8       cgd 				break;
    445        1.8       cgd 			cp -= allocsize;
    446        1.8       cgd 			freep->next = cp;
    447        1.8       cgd 		}
    448        1.8       cgd 		freep->next = savedlist;
    449      1.117      yamt 		if (savedlist == NULL)
    450      1.108  christos 			kbp->kb_last = (void *)freep;
    451        1.1       cgd 	}
    452        1.1       cgd 	va = kbp->kb_next;
    453        1.8       cgd 	kbp->kb_next = ((struct freelist *)va)->next;
    454        1.8       cgd #ifdef DIAGNOSTIC
    455        1.8       cgd 	freep = (struct freelist *)va;
    456       1.77   thorpej 	/* XXX potential to get garbage pointer here. */
    457       1.29       chs 	if (kbp->kb_next) {
    458       1.29       chs 		int rv;
    459       1.35       eeh 		vaddr_t addr = (vaddr_t)kbp->kb_next;
    460       1.29       chs 
    461       1.43   thorpej 		vm_map_lock(kmem_map);
    462       1.29       chs 		rv = uvm_map_checkprot(kmem_map, addr,
    463       1.69     enami 		    addr + sizeof(struct freelist), VM_PROT_WRITE);
    464       1.43   thorpej 		vm_map_unlock(kmem_map);
    465       1.29       chs 
    466       1.51   thorpej 		if (__predict_false(rv == 0)) {
    467       1.69     enami 			printf("Data modified on freelist: "
    468       1.69     enami 			    "word %ld of object %p size %ld previous type %s "
    469       1.69     enami 			    "(invalid addr %p)\n",
    470       1.41       mrg 			    (long)((int32_t *)&kbp->kb_next - (int32_t *)kbp),
    471       1.80      manu 			    va, size, "foo", kbp->kb_next);
    472       1.27   thorpej #ifdef MALLOCLOG
    473       1.41       mrg 			hitmlog(va);
    474       1.27   thorpej #endif
    475       1.41       mrg 			kbp->kb_next = NULL;
    476       1.29       chs 		}
    477        1.8       cgd 	}
    478       1.11       cgd 
    479       1.11       cgd 	/* Fill the fields that we've used with WEIRD_ADDR */
    480       1.77   thorpej #ifdef _LP64
    481       1.77   thorpej 	freep->type = (struct malloc_type *)
    482       1.77   thorpej 	    (WEIRD_ADDR | (((u_long) WEIRD_ADDR) << 32));
    483       1.77   thorpej #else
    484       1.77   thorpej 	freep->type = (struct malloc_type *) WEIRD_ADDR;
    485        1.8       cgd #endif
    486       1.86     ragge 	end = (uint32_t *)&freep->next +
    487       1.11       cgd 	    (sizeof(freep->next) / sizeof(int32_t));
    488       1.86     ragge 	for (lp = (uint32_t *)&freep->next; lp < end; lp++)
    489       1.11       cgd 		*lp = WEIRD_ADDR;
    490       1.11       cgd 
    491       1.11       cgd 	/* and check that the data hasn't been modified. */
    492       1.76   thorpej 	end = (uint32_t *)&va[copysize];
    493       1.86     ragge 	for (lp = (uint32_t *)va; lp < end; lp++) {
    494       1.51   thorpej 		if (__predict_true(*lp == WEIRD_ADDR))
    495        1.8       cgd 			continue;
    496       1.69     enami 		printf("Data modified on freelist: "
    497       1.69     enami 		    "word %ld of object %p size %ld previous type %s "
    498       1.69     enami 		    "(0x%x != 0x%x)\n",
    499       1.76   thorpej 		    (long)(lp - (uint32_t *)va), va, size,
    500       1.80      manu 		    "bar", *lp, WEIRD_ADDR);
    501       1.27   thorpej #ifdef MALLOCLOG
    502       1.27   thorpej 		hitmlog(va);
    503       1.27   thorpej #endif
    504        1.8       cgd 		break;
    505        1.8       cgd 	}
    506       1.11       cgd 
    507        1.8       cgd 	freep->spare0 = 0;
    508        1.8       cgd #endif /* DIAGNOSTIC */
    509        1.1       cgd #ifdef KMEMSTATS
    510        1.1       cgd 	kup = btokup(va);
    511        1.1       cgd 	if (kup->ku_indx != indx)
    512        1.1       cgd 		panic("malloc: wrong bucket");
    513        1.1       cgd 	if (kup->ku_freecnt == 0)
    514        1.1       cgd 		panic("malloc: lost data");
    515        1.1       cgd 	kup->ku_freecnt--;
    516        1.1       cgd 	kbp->kb_totalfree--;
    517        1.1       cgd 	ksp->ks_memuse += 1 << indx;
    518        1.1       cgd out:
    519        1.1       cgd 	kbp->kb_calls++;
    520        1.1       cgd 	ksp->ks_inuse++;
    521        1.1       cgd 	ksp->ks_calls++;
    522        1.1       cgd 	if (ksp->ks_memuse > ksp->ks_maxused)
    523        1.1       cgd 		ksp->ks_maxused = ksp->ks_memuse;
    524        1.1       cgd #else
    525        1.1       cgd out:
    526        1.1       cgd #endif
    527       1.27   thorpej #ifdef MALLOCLOG
    528       1.80      manu 	domlog(va, size, ksp, 1, file, line);
    529       1.27   thorpej #endif
    530      1.113        ad 	mutex_spin_exit(&malloc_lock);
    531       1.67     enami 	if ((flags & M_ZERO) != 0)
    532       1.65     lukem 		memset(va, 0, size);
    533      1.106        ad 	FREECHECK_OUT(&malloc_freecheck, (void *)va);
    534        1.1       cgd 	return ((void *) va);
    535        1.1       cgd }
    536        1.1       cgd 
    537        1.1       cgd /*
    538        1.1       cgd  * Free a block of memory allocated by malloc.
    539        1.1       cgd  */
    540       1.27   thorpej #ifdef MALLOCLOG
    541       1.27   thorpej void
    542  1.121.2.1     skrll _kern_free(void *addr, struct malloc_type *ksp, const char *file, long line)
    543       1.27   thorpej #else
    544        1.1       cgd void
    545  1.121.2.1     skrll kern_free(void *addr, struct malloc_type *ksp)
    546       1.27   thorpej #endif /* MALLOCLOG */
    547        1.1       cgd {
    548       1.50  augustss 	struct kmembuckets *kbp;
    549       1.50  augustss 	struct kmemusage *kup;
    550       1.50  augustss 	struct freelist *freep;
    551        1.8       cgd 	long size;
    552        1.5    andrew #ifdef DIAGNOSTIC
    553      1.108  christos 	void *cp;
    554       1.11       cgd 	int32_t *end, *lp;
    555       1.11       cgd 	long alloc, copysize;
    556        1.5    andrew #endif
    557       1.48   thorpej 
    558      1.106        ad 	FREECHECK_IN(&malloc_freecheck, addr);
    559       1.62   thorpej #ifdef MALLOC_DEBUG
    560       1.77   thorpej 	if (debug_free(addr, ksp))
    561       1.62   thorpej 		return;
    562       1.62   thorpej #endif
    563       1.62   thorpej 
    564       1.48   thorpej #ifdef DIAGNOSTIC
    565       1.48   thorpej 	/*
    566       1.48   thorpej 	 * Ensure that we're free'ing something that we could
    567       1.48   thorpej 	 * have allocated in the first place.  That is, check
    568       1.48   thorpej 	 * to see that the address is within kmem_map.
    569       1.48   thorpej 	 */
    570       1.83     enami 	if (__predict_false((vaddr_t)addr < vm_map_min(kmem_map) ||
    571       1.83     enami 	    (vaddr_t)addr >= vm_map_max(kmem_map)))
    572       1.48   thorpej 		panic("free: addr %p not within kmem_map", addr);
    573        1.1       cgd #endif
    574        1.1       cgd 
    575        1.1       cgd 	kup = btokup(addr);
    576        1.1       cgd 	size = 1 << kup->ku_indx;
    577       1.99       chs 	kbp = &kmembuckets[kup->ku_indx];
    578      1.113        ad 
    579      1.115      yamt 	LOCKDEBUG_MEM_CHECK(addr,
    580      1.115      yamt 	    size <= MAXALLOCSAVE ? size : ctob(kup->ku_pagecnt));
    581      1.113        ad 
    582      1.113        ad 	mutex_spin_enter(&malloc_lock);
    583       1.27   thorpej #ifdef MALLOCLOG
    584       1.80      manu 	domlog(addr, 0, ksp, 2, file, line);
    585       1.27   thorpej #endif
    586        1.1       cgd #ifdef DIAGNOSTIC
    587        1.8       cgd 	/*
    588        1.8       cgd 	 * Check for returns of data that do not point to the
    589        1.8       cgd 	 * beginning of the allocation.
    590        1.8       cgd 	 */
    591       1.49   thorpej 	if (size > PAGE_SIZE)
    592       1.49   thorpej 		alloc = addrmask[BUCKETINDX(PAGE_SIZE)];
    593        1.1       cgd 	else
    594        1.1       cgd 		alloc = addrmask[kup->ku_indx];
    595        1.8       cgd 	if (((u_long)addr & alloc) != 0)
    596       1.75    provos 		panic("free: unaligned addr %p, size %ld, type %s, mask %ld",
    597       1.77   thorpej 		    addr, size, ksp->ks_shortdesc, alloc);
    598        1.1       cgd #endif /* DIAGNOSTIC */
    599        1.1       cgd 	if (size > MAXALLOCSAVE) {
    600       1.97      yamt 		uvm_km_free(kmem_map, (vaddr_t)addr, ctob(kup->ku_pagecnt),
    601       1.97      yamt 		    UVM_KMF_WIRED);
    602        1.1       cgd #ifdef KMEMSTATS
    603        1.1       cgd 		size = kup->ku_pagecnt << PGSHIFT;
    604        1.1       cgd 		ksp->ks_memuse -= size;
    605        1.1       cgd 		kup->ku_indx = 0;
    606        1.1       cgd 		kup->ku_pagecnt = 0;
    607        1.1       cgd 		if (ksp->ks_memuse + size >= ksp->ks_limit &&
    608        1.1       cgd 		    ksp->ks_memuse < ksp->ks_limit)
    609      1.108  christos 			wakeup((void *)ksp);
    610       1.79      fvdl #ifdef DIAGNOSTIC
    611       1.79      fvdl 		if (ksp->ks_inuse == 0)
    612       1.79      fvdl 			panic("free 1: inuse 0, probable double free");
    613       1.79      fvdl #endif
    614        1.1       cgd 		ksp->ks_inuse--;
    615        1.1       cgd 		kbp->kb_total -= 1;
    616        1.1       cgd #endif
    617      1.113        ad 		mutex_spin_exit(&malloc_lock);
    618        1.1       cgd 		return;
    619        1.1       cgd 	}
    620        1.8       cgd 	freep = (struct freelist *)addr;
    621        1.8       cgd #ifdef DIAGNOSTIC
    622        1.8       cgd 	/*
    623        1.8       cgd 	 * Check for multiple frees. Use a quick check to see if
    624        1.8       cgd 	 * it looks free before laboriously searching the freelist.
    625        1.8       cgd 	 */
    626       1.51   thorpej 	if (__predict_false(freep->spare0 == WEIRD_ADDR)) {
    627       1.16       cgd 		for (cp = kbp->kb_next; cp;
    628       1.16       cgd 		    cp = ((struct freelist *)cp)->next) {
    629        1.8       cgd 			if (addr != cp)
    630        1.8       cgd 				continue;
    631       1.22  christos 			printf("multiply freed item %p\n", addr);
    632       1.27   thorpej #ifdef MALLOCLOG
    633       1.27   thorpej 			hitmlog(addr);
    634       1.27   thorpej #endif
    635        1.8       cgd 			panic("free: duplicated free");
    636        1.8       cgd 		}
    637        1.8       cgd 	}
    638      1.112        ad 
    639        1.8       cgd 	/*
    640        1.8       cgd 	 * Copy in known text to detect modification after freeing
    641        1.8       cgd 	 * and to make it look free. Also, save the type being freed
    642        1.8       cgd 	 * so we can list likely culprit if modification is detected
    643        1.8       cgd 	 * when the object is reallocated.
    644        1.8       cgd 	 */
    645        1.8       cgd 	copysize = size < MAX_COPY ? size : MAX_COPY;
    646      1.108  christos 	end = (int32_t *)&((char *)addr)[copysize];
    647       1.11       cgd 	for (lp = (int32_t *)addr; lp < end; lp++)
    648        1.8       cgd 		*lp = WEIRD_ADDR;
    649       1.77   thorpej 	freep->type = ksp;
    650        1.8       cgd #endif /* DIAGNOSTIC */
    651        1.1       cgd #ifdef KMEMSTATS
    652        1.1       cgd 	kup->ku_freecnt++;
    653       1.36   thorpej 	if (kup->ku_freecnt >= kbp->kb_elmpercl) {
    654        1.1       cgd 		if (kup->ku_freecnt > kbp->kb_elmpercl)
    655        1.1       cgd 			panic("free: multiple frees");
    656        1.1       cgd 		else if (kbp->kb_totalfree > kbp->kb_highwat)
    657        1.1       cgd 			kbp->kb_couldfree++;
    658       1.36   thorpej 	}
    659        1.1       cgd 	kbp->kb_totalfree++;
    660        1.1       cgd 	ksp->ks_memuse -= size;
    661        1.1       cgd 	if (ksp->ks_memuse + size >= ksp->ks_limit &&
    662        1.1       cgd 	    ksp->ks_memuse < ksp->ks_limit)
    663      1.108  christos 		wakeup((void *)ksp);
    664       1.79      fvdl #ifdef DIAGNOSTIC
    665       1.79      fvdl 	if (ksp->ks_inuse == 0)
    666       1.79      fvdl 		panic("free 2: inuse 0, probable double free");
    667       1.79      fvdl #endif
    668        1.1       cgd 	ksp->ks_inuse--;
    669        1.1       cgd #endif
    670        1.8       cgd 	if (kbp->kb_next == NULL)
    671        1.8       cgd 		kbp->kb_next = addr;
    672        1.8       cgd 	else
    673        1.8       cgd 		((struct freelist *)kbp->kb_last)->next = addr;
    674        1.8       cgd 	freep->next = NULL;
    675        1.8       cgd 	kbp->kb_last = addr;
    676      1.113        ad 	mutex_spin_exit(&malloc_lock);
    677       1.20       cgd }
    678       1.20       cgd 
    679       1.20       cgd /*
    680       1.20       cgd  * Change the size of a block of memory.
    681       1.20       cgd  */
    682       1.20       cgd void *
    683  1.121.2.1     skrll kern_realloc(void *curaddr, unsigned long newsize, struct malloc_type *ksp,
    684       1.77   thorpej     int flags)
    685       1.20       cgd {
    686       1.50  augustss 	struct kmemusage *kup;
    687       1.72   thorpej 	unsigned long cursize;
    688       1.20       cgd 	void *newaddr;
    689       1.20       cgd #ifdef DIAGNOSTIC
    690       1.20       cgd 	long alloc;
    691       1.20       cgd #endif
    692       1.20       cgd 
    693       1.20       cgd 	/*
    694       1.69     enami 	 * realloc() with a NULL pointer is the same as malloc().
    695       1.20       cgd 	 */
    696       1.20       cgd 	if (curaddr == NULL)
    697       1.77   thorpej 		return (malloc(newsize, ksp, flags));
    698       1.20       cgd 
    699       1.20       cgd 	/*
    700       1.69     enami 	 * realloc() with zero size is the same as free().
    701       1.20       cgd 	 */
    702       1.20       cgd 	if (newsize == 0) {
    703       1.77   thorpej 		free(curaddr, ksp);
    704       1.20       cgd 		return (NULL);
    705       1.20       cgd 	}
    706       1.59   thorpej 
    707       1.59   thorpej #ifdef LOCKDEBUG
    708      1.119        ad 	if ((flags & M_NOWAIT) == 0) {
    709      1.118      yamt 		ASSERT_SLEEPABLE();
    710      1.119        ad 	}
    711       1.59   thorpej #endif
    712       1.20       cgd 
    713       1.20       cgd 	/*
    714       1.20       cgd 	 * Find out how large the old allocation was (and do some
    715       1.20       cgd 	 * sanity checking).
    716       1.20       cgd 	 */
    717       1.20       cgd 	kup = btokup(curaddr);
    718       1.20       cgd 	cursize = 1 << kup->ku_indx;
    719       1.20       cgd 
    720       1.20       cgd #ifdef DIAGNOSTIC
    721       1.20       cgd 	/*
    722       1.20       cgd 	 * Check for returns of data that do not point to the
    723       1.20       cgd 	 * beginning of the allocation.
    724       1.20       cgd 	 */
    725       1.49   thorpej 	if (cursize > PAGE_SIZE)
    726       1.49   thorpej 		alloc = addrmask[BUCKETINDX(PAGE_SIZE)];
    727       1.20       cgd 	else
    728       1.20       cgd 		alloc = addrmask[kup->ku_indx];
    729       1.20       cgd 	if (((u_long)curaddr & alloc) != 0)
    730       1.69     enami 		panic("realloc: "
    731       1.69     enami 		    "unaligned addr %p, size %ld, type %s, mask %ld\n",
    732       1.77   thorpej 		    curaddr, cursize, ksp->ks_shortdesc, alloc);
    733       1.20       cgd #endif /* DIAGNOSTIC */
    734       1.20       cgd 
    735       1.20       cgd 	if (cursize > MAXALLOCSAVE)
    736       1.20       cgd 		cursize = ctob(kup->ku_pagecnt);
    737       1.20       cgd 
    738       1.20       cgd 	/*
    739       1.20       cgd 	 * If we already actually have as much as they want, we're done.
    740       1.20       cgd 	 */
    741       1.20       cgd 	if (newsize <= cursize)
    742       1.20       cgd 		return (curaddr);
    743       1.20       cgd 
    744       1.20       cgd 	/*
    745       1.20       cgd 	 * Can't satisfy the allocation with the existing block.
    746       1.20       cgd 	 * Allocate a new one and copy the data.
    747       1.20       cgd 	 */
    748       1.77   thorpej 	newaddr = malloc(newsize, ksp, flags);
    749       1.51   thorpej 	if (__predict_false(newaddr == NULL)) {
    750       1.20       cgd 		/*
    751       1.69     enami 		 * malloc() failed, because flags included M_NOWAIT.
    752       1.20       cgd 		 * Return NULL to indicate that failure.  The old
    753       1.20       cgd 		 * pointer is still valid.
    754       1.20       cgd 		 */
    755       1.69     enami 		return (NULL);
    756       1.20       cgd 	}
    757       1.34     perry 	memcpy(newaddr, curaddr, cursize);
    758       1.20       cgd 
    759       1.20       cgd 	/*
    760       1.20       cgd 	 * We were successful: free the old allocation and return
    761       1.20       cgd 	 * the new one.
    762       1.20       cgd 	 */
    763       1.77   thorpej 	free(curaddr, ksp);
    764       1.20       cgd 	return (newaddr);
    765       1.70     enami }
    766       1.70     enami 
    767       1.70     enami /*
    768       1.70     enami  * Roundup size to the actual allocation size.
    769       1.70     enami  */
    770       1.70     enami unsigned long
    771       1.70     enami malloc_roundup(unsigned long size)
    772       1.70     enami {
    773       1.70     enami 
    774       1.70     enami 	if (size > MAXALLOCSAVE)
    775       1.70     enami 		return (roundup(size, PAGE_SIZE));
    776       1.70     enami 	else
    777       1.70     enami 		return (1 << BUCKETINDX(size));
    778        1.1       cgd }
    779        1.1       cgd 
    780        1.1       cgd /*
    781       1.77   thorpej  * Add a malloc type to the system.
    782       1.77   thorpej  */
    783       1.77   thorpej void
    784       1.77   thorpej malloc_type_attach(struct malloc_type *type)
    785       1.77   thorpej {
    786       1.77   thorpej 
    787       1.77   thorpej 	if (nkmempages == 0)
    788       1.77   thorpej 		panic("malloc_type_attach: nkmempages == 0");
    789       1.77   thorpej 
    790       1.77   thorpej 	if (type->ks_magic != M_MAGIC)
    791       1.77   thorpej 		panic("malloc_type_attach: bad magic");
    792       1.77   thorpej 
    793       1.77   thorpej #ifdef DIAGNOSTIC
    794       1.77   thorpej 	{
    795       1.77   thorpej 		struct malloc_type *ksp;
    796       1.77   thorpej 		for (ksp = kmemstatistics; ksp != NULL; ksp = ksp->ks_next) {
    797       1.77   thorpej 			if (ksp == type)
    798       1.77   thorpej 				panic("malloc_type_attach: already on list");
    799       1.77   thorpej 		}
    800       1.77   thorpej 	}
    801       1.77   thorpej #endif
    802       1.77   thorpej 
    803       1.77   thorpej #ifdef KMEMSTATS
    804       1.77   thorpej 	if (type->ks_limit == 0)
    805       1.77   thorpej 		type->ks_limit = ((u_long)nkmempages << PAGE_SHIFT) * 6U / 10U;
    806       1.77   thorpej #else
    807       1.77   thorpej 	type->ks_limit = 0;
    808       1.77   thorpej #endif
    809       1.77   thorpej 
    810       1.77   thorpej 	type->ks_next = kmemstatistics;
    811       1.77   thorpej 	kmemstatistics = type;
    812       1.77   thorpej }
    813       1.77   thorpej 
    814       1.77   thorpej /*
    815       1.77   thorpej  * Remove a malloc type from the system..
    816       1.77   thorpej  */
    817       1.77   thorpej void
    818       1.77   thorpej malloc_type_detach(struct malloc_type *type)
    819       1.77   thorpej {
    820       1.77   thorpej 	struct malloc_type *ksp;
    821       1.77   thorpej 
    822       1.77   thorpej #ifdef DIAGNOSTIC
    823       1.77   thorpej 	if (type->ks_magic != M_MAGIC)
    824       1.77   thorpej 		panic("malloc_type_detach: bad magic");
    825       1.77   thorpej #endif
    826       1.77   thorpej 
    827       1.77   thorpej 	if (type == kmemstatistics)
    828       1.77   thorpej 		kmemstatistics = type->ks_next;
    829       1.77   thorpej 	else {
    830       1.77   thorpej 		for (ksp = kmemstatistics; ksp->ks_next != NULL;
    831       1.77   thorpej 		     ksp = ksp->ks_next) {
    832       1.77   thorpej 			if (ksp->ks_next == type) {
    833       1.77   thorpej 				ksp->ks_next = type->ks_next;
    834       1.77   thorpej 				break;
    835       1.77   thorpej 			}
    836       1.77   thorpej 		}
    837       1.77   thorpej #ifdef DIAGNOSTIC
    838       1.77   thorpej 		if (ksp->ks_next == NULL)
    839       1.77   thorpej 			panic("malloc_type_detach: not on list");
    840       1.77   thorpej #endif
    841       1.77   thorpej 	}
    842       1.77   thorpej 	type->ks_next = NULL;
    843       1.77   thorpej }
    844       1.77   thorpej 
    845       1.77   thorpej /*
    846       1.77   thorpej  * Set the limit on a malloc type.
    847       1.77   thorpej  */
    848       1.77   thorpej void
    849      1.105      yamt malloc_type_setlimit(struct malloc_type *type, u_long limit)
    850       1.77   thorpej {
    851       1.77   thorpej #ifdef KMEMSTATS
    852      1.113        ad 	mutex_spin_enter(&malloc_lock);
    853       1.77   thorpej 	type->ks_limit = limit;
    854      1.113        ad 	mutex_spin_exit(&malloc_lock);
    855       1.77   thorpej #endif
    856       1.77   thorpej }
    857       1.77   thorpej 
    858       1.77   thorpej /*
    859       1.49   thorpej  * Compute the number of pages that kmem_map will map, that is,
    860       1.49   thorpej  * the size of the kernel malloc arena.
    861       1.49   thorpej  */
    862       1.49   thorpej void
    863       1.69     enami kmeminit_nkmempages(void)
    864       1.49   thorpej {
    865       1.49   thorpej 	int npages;
    866       1.49   thorpej 
    867       1.49   thorpej 	if (nkmempages != 0) {
    868       1.49   thorpej 		/*
    869       1.49   thorpej 		 * It's already been set (by us being here before, or
    870       1.49   thorpej 		 * by patching or kernel config options), bail out now.
    871       1.49   thorpej 		 */
    872       1.49   thorpej 		return;
    873       1.49   thorpej 	}
    874       1.49   thorpej 
    875       1.94      yamt 	npages = physmem;
    876       1.49   thorpej 
    877       1.49   thorpej 	if (npages > NKMEMPAGES_MAX)
    878       1.49   thorpej 		npages = NKMEMPAGES_MAX;
    879       1.49   thorpej 
    880       1.49   thorpej 	if (npages < NKMEMPAGES_MIN)
    881       1.49   thorpej 		npages = NKMEMPAGES_MIN;
    882       1.49   thorpej 
    883       1.49   thorpej 	nkmempages = npages;
    884       1.49   thorpej }
    885       1.49   thorpej 
    886       1.49   thorpej /*
    887        1.1       cgd  * Initialize the kernel memory allocator
    888        1.1       cgd  */
    889       1.12  christos void
    890       1.69     enami kmeminit(void)
    891        1.1       cgd {
    892       1.77   thorpej 	__link_set_decl(malloc_types, struct malloc_type);
    893       1.77   thorpej 	struct malloc_type * const *ksp;
    894       1.84     ragge 	vaddr_t kmb, kml;
    895       1.23       tls #ifdef KMEMSTATS
    896       1.50  augustss 	long indx;
    897       1.23       tls #endif
    898        1.1       cgd 
    899        1.1       cgd #if	((MAXALLOCSAVE & (MAXALLOCSAVE - 1)) != 0)
    900        1.1       cgd 		ERROR!_kmeminit:_MAXALLOCSAVE_not_power_of_2
    901        1.1       cgd #endif
    902        1.1       cgd #if	(MAXALLOCSAVE > MINALLOCSIZE * 32768)
    903        1.1       cgd 		ERROR!_kmeminit:_MAXALLOCSAVE_too_big
    904        1.1       cgd #endif
    905       1.47     ragge #if	(MAXALLOCSAVE < NBPG)
    906        1.1       cgd 		ERROR!_kmeminit:_MAXALLOCSAVE_too_small
    907        1.1       cgd #endif
    908       1.11       cgd 
    909       1.11       cgd 	if (sizeof(struct freelist) > (1 << MINBUCKET))
    910       1.11       cgd 		panic("minbucket too small/struct freelist too big");
    911       1.11       cgd 
    912      1.116        ad 	mutex_init(&malloc_lock, MUTEX_DEFAULT, IPL_VM);
    913      1.109        ad 
    914       1.49   thorpej 	/*
    915       1.49   thorpej 	 * Compute the number of kmem_map pages, if we have not
    916       1.49   thorpej 	 * done so already.
    917       1.49   thorpej 	 */
    918       1.49   thorpej 	kmeminit_nkmempages();
    919       1.49   thorpej 
    920       1.97      yamt 	kmemusage = (struct kmemusage *) uvm_km_alloc(kernel_map,
    921       1.97      yamt 	    (vsize_t)(nkmempages * sizeof(struct kmemusage)), 0,
    922       1.97      yamt 	    UVM_KMF_WIRED|UVM_KMF_ZERO);
    923       1.85      fvdl 	kmb = 0;
    924       1.84     ragge 	kmem_map = uvm_km_suballoc(kernel_map, &kmb,
    925       1.96     perry 	    &kml, ((vsize_t)nkmempages << PAGE_SHIFT),
    926      1.107   thorpej 	    VM_MAP_INTRSAFE, false, &kmem_map_store);
    927       1.93      yamt 	uvm_km_vacache_init(kmem_map, "kvakmem", 0);
    928       1.84     ragge 	kmembase = (char *)kmb;
    929       1.84     ragge 	kmemlimit = (char *)kml;
    930        1.1       cgd #ifdef KMEMSTATS
    931        1.1       cgd 	for (indx = 0; indx < MINBUCKET + 16; indx++) {
    932       1.49   thorpej 		if (1 << indx >= PAGE_SIZE)
    933       1.99       chs 			kmembuckets[indx].kb_elmpercl = 1;
    934        1.1       cgd 		else
    935       1.99       chs 			kmembuckets[indx].kb_elmpercl = PAGE_SIZE / (1 << indx);
    936       1.99       chs 		kmembuckets[indx].kb_highwat =
    937       1.99       chs 			5 * kmembuckets[indx].kb_elmpercl;
    938        1.1       cgd 	}
    939       1.62   thorpej #endif
    940       1.77   thorpej 
    941       1.77   thorpej 	/* Attach all of the statically-linked malloc types. */
    942       1.77   thorpej 	__link_set_foreach(ksp, malloc_types)
    943       1.77   thorpej 		malloc_type_attach(*ksp);
    944        1.1       cgd }
    945       1.39   thorpej 
    946       1.39   thorpej #ifdef DDB
    947       1.39   thorpej #include <ddb/db_output.h>
    948       1.39   thorpej 
    949       1.39   thorpej /*
    950       1.39   thorpej  * Dump kmem statistics from ddb.
    951       1.39   thorpej  *
    952       1.39   thorpej  * usage: call dump_kmemstats
    953       1.39   thorpej  */
    954       1.69     enami void	dump_kmemstats(void);
    955       1.39   thorpej 
    956       1.39   thorpej void
    957       1.69     enami dump_kmemstats(void)
    958       1.39   thorpej {
    959       1.39   thorpej #ifdef KMEMSTATS
    960       1.77   thorpej 	struct malloc_type *ksp;
    961       1.39   thorpej 
    962       1.77   thorpej 	for (ksp = kmemstatistics; ksp != NULL; ksp = ksp->ks_next) {
    963       1.77   thorpej 		if (ksp->ks_memuse == 0)
    964       1.77   thorpej 			continue;
    965       1.77   thorpej 		db_printf("%s%.*s %ld\n", ksp->ks_shortdesc,
    966       1.77   thorpej 		    (int)(20 - strlen(ksp->ks_shortdesc)),
    967       1.77   thorpej 		    "                    ",
    968       1.77   thorpej 		    ksp->ks_memuse);
    969       1.39   thorpej 	}
    970       1.39   thorpej #else
    971       1.39   thorpej 	db_printf("Kmem stats are not being collected.\n");
    972       1.39   thorpej #endif /* KMEMSTATS */
    973       1.39   thorpej }
    974       1.39   thorpej #endif /* DDB */
    975       1.82      manu 
    976       1.82      manu 
    977       1.82      manu #if 0
    978       1.96     perry /*
    979       1.82      manu  * Diagnostic messages about "Data modified on
    980       1.82      manu  * freelist" indicate a memory corruption, but
    981       1.82      manu  * they do not help tracking it down.
    982       1.96     perry  * This function can be called at various places
    983       1.82      manu  * to sanity check malloc's freelist and discover
    984       1.82      manu  * where does the corruption take place.
    985       1.82      manu  */
    986       1.82      manu int
    987       1.82      manu freelist_sanitycheck(void) {
    988       1.82      manu 	int i,j;
    989       1.82      manu 	struct kmembuckets *kbp;
    990       1.82      manu 	struct freelist *freep;
    991       1.82      manu 	int rv = 0;
    992       1.96     perry 
    993       1.82      manu 	for (i = MINBUCKET; i <= MINBUCKET + 15; i++) {
    994       1.99       chs 		kbp = &kmembuckets[i];
    995       1.82      manu 		freep = (struct freelist *)kbp->kb_next;
    996       1.82      manu 		j = 0;
    997       1.82      manu 		while(freep) {
    998       1.82      manu 			vm_map_lock(kmem_map);
    999       1.82      manu 			rv = uvm_map_checkprot(kmem_map, (vaddr_t)freep,
   1000       1.96     perry 			    (vaddr_t)freep + sizeof(struct freelist),
   1001       1.82      manu 			    VM_PROT_WRITE);
   1002       1.82      manu 			vm_map_unlock(kmem_map);
   1003       1.82      manu 
   1004       1.82      manu 			if ((rv == 0) || (*(int *)freep != WEIRD_ADDR)) {
   1005       1.82      manu 				printf("bucket %i, chunck %d at %p modified\n",
   1006       1.82      manu 				    i, j, freep);
   1007       1.82      manu 				return 1;
   1008       1.82      manu 			}
   1009       1.82      manu 			freep = (struct freelist *)freep->next;
   1010       1.82      manu 			j++;
   1011       1.82      manu 		}
   1012       1.82      manu 	}
   1013       1.82      manu 
   1014       1.82      manu 	return 0;
   1015       1.82      manu }
   1016       1.82      manu #endif
   1017