Home | History | Annotate | Line # | Download | only in kern
kern_malloc.c revision 1.136
      1  1.136     rmind /*	$NetBSD: kern_malloc.c,v 1.136 2012/01/30 01:56:47 rmind 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.136     rmind __KERNEL_RCSID(0, "$NetBSD: kern_malloc.c,v 1.136 2012/01/30 01:56:47 rmind 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.134      para #include <sys/kmem.h>
     76   1.12  christos #include <sys/systm.h>
     77  1.106        ad #include <sys/debug.h>
     78  1.109        ad #include <sys/mutex.h>
     79  1.113        ad #include <sys/lockdebug.h>
     80   1.24   thorpej 
     81   1.28       mrg #include <uvm/uvm_extern.h>
     82   1.28       mrg 
     83   1.24   thorpej #include "opt_kmemstats.h"
     84   1.27   thorpej #include "opt_malloclog.h"
     85   1.71      fvdl #include "opt_malloc_debug.h"
     86   1.12  christos 
     87   1.99       chs struct kmembuckets kmembuckets[MINBUCKET + 16];
     88    1.1       cgd struct kmemusage *kmemusage;
     89   1.77   thorpej struct malloc_type *kmemstatistics;
     90    1.1       cgd 
     91  1.134      para kmutex_t malloc_lock;
     92   1.27   thorpej 
     93  1.134      para struct malloc_header {
     94  1.136     rmind 	/* Total size, include the header. */
     95  1.136     rmind 	size_t		mh_size;
     96    1.8       cgd };
     97   1.78        pk 
     98   1.77   thorpej /*
     99    1.1       cgd  * Allocate a block of memory
    100    1.1       cgd  */
    101   1.27   thorpej #ifdef MALLOCLOG
    102   1.27   thorpej void *
    103  1.125     pooka _kern_malloc(unsigned long size, struct malloc_type *ksp, int flags,
    104   1.77   thorpej     const char *file, long line)
    105   1.27   thorpej #else
    106    1.1       cgd void *
    107  1.125     pooka kern_malloc(unsigned long size, struct malloc_type *ksp, int flags)
    108   1.27   thorpej #endif /* MALLOCLOG */
    109    1.1       cgd {
    110  1.136     rmind 	const int kmflags = (flags & M_NOWAIT) ? KM_NOSLEEP : KM_SLEEP;
    111  1.136     rmind 	const size_t allocsize = sizeof(struct malloc_header) + size;
    112  1.134      para 	struct malloc_header *mh;
    113  1.134      para 	void *p;
    114  1.134      para 
    115  1.134      para 	p = kmem_intr_alloc(allocsize, kmflags);
    116  1.134      para 	if (p == NULL)
    117  1.134      para 		return NULL;
    118    1.1       cgd 
    119  1.134      para 	if ((flags & M_ZERO) != 0) {
    120  1.134      para 		memset(p, 0, allocsize);
    121    1.8       cgd 	}
    122  1.134      para 	mh = (void *)p;
    123  1.134      para 	mh->mh_size = allocsize;
    124   1.11       cgd 
    125  1.134      para 	return mh + 1;
    126    1.1       cgd }
    127    1.1       cgd 
    128    1.1       cgd /*
    129    1.1       cgd  * Free a block of memory allocated by malloc.
    130    1.1       cgd  */
    131   1.27   thorpej #ifdef MALLOCLOG
    132   1.27   thorpej void
    133  1.125     pooka _kern_free(void *addr, struct malloc_type *ksp, const char *file, long line)
    134   1.27   thorpej #else
    135    1.1       cgd void
    136  1.125     pooka kern_free(void *addr, struct malloc_type *ksp)
    137   1.27   thorpej #endif /* MALLOCLOG */
    138    1.1       cgd {
    139  1.134      para 	struct malloc_header *mh;
    140   1.48   thorpej 
    141  1.134      para 	mh = addr;
    142  1.134      para 	mh--;
    143   1.62   thorpej 
    144  1.134      para 	kmem_intr_free(mh, mh->mh_size);
    145   1.20       cgd }
    146   1.20       cgd 
    147   1.20       cgd /*
    148   1.20       cgd  * Change the size of a block of memory.
    149   1.20       cgd  */
    150   1.20       cgd void *
    151  1.126     pooka kern_realloc(void *curaddr, unsigned long newsize, struct malloc_type *ksp,
    152   1.77   thorpej     int flags)
    153   1.20       cgd {
    154  1.134      para 	struct malloc_header *mh;
    155   1.72   thorpej 	unsigned long cursize;
    156   1.20       cgd 	void *newaddr;
    157   1.20       cgd 
    158   1.20       cgd 	/*
    159   1.69     enami 	 * realloc() with a NULL pointer is the same as malloc().
    160   1.20       cgd 	 */
    161   1.20       cgd 	if (curaddr == NULL)
    162   1.77   thorpej 		return (malloc(newsize, ksp, flags));
    163   1.20       cgd 
    164   1.20       cgd 	/*
    165   1.69     enami 	 * realloc() with zero size is the same as free().
    166   1.20       cgd 	 */
    167   1.20       cgd 	if (newsize == 0) {
    168   1.77   thorpej 		free(curaddr, ksp);
    169   1.20       cgd 		return (NULL);
    170   1.20       cgd 	}
    171   1.59   thorpej 
    172   1.59   thorpej #ifdef LOCKDEBUG
    173  1.119        ad 	if ((flags & M_NOWAIT) == 0) {
    174  1.118      yamt 		ASSERT_SLEEPABLE();
    175  1.119        ad 	}
    176   1.59   thorpej #endif
    177   1.20       cgd 
    178  1.134      para 	mh = curaddr;
    179  1.134      para 	mh--;
    180   1.20       cgd 
    181  1.136     rmind 	cursize = mh->mh_size - sizeof(struct malloc_header);
    182   1.20       cgd 
    183   1.20       cgd 	/*
    184   1.20       cgd 	 * If we already actually have as much as they want, we're done.
    185   1.20       cgd 	 */
    186   1.20       cgd 	if (newsize <= cursize)
    187   1.20       cgd 		return (curaddr);
    188   1.20       cgd 
    189   1.20       cgd 	/*
    190   1.20       cgd 	 * Can't satisfy the allocation with the existing block.
    191   1.20       cgd 	 * Allocate a new one and copy the data.
    192   1.20       cgd 	 */
    193   1.77   thorpej 	newaddr = malloc(newsize, ksp, flags);
    194   1.51   thorpej 	if (__predict_false(newaddr == NULL)) {
    195   1.20       cgd 		/*
    196   1.69     enami 		 * malloc() failed, because flags included M_NOWAIT.
    197   1.20       cgd 		 * Return NULL to indicate that failure.  The old
    198   1.20       cgd 		 * pointer is still valid.
    199   1.20       cgd 		 */
    200   1.69     enami 		return (NULL);
    201   1.20       cgd 	}
    202   1.34     perry 	memcpy(newaddr, curaddr, cursize);
    203   1.20       cgd 
    204   1.20       cgd 	/*
    205   1.20       cgd 	 * We were successful: free the old allocation and return
    206   1.20       cgd 	 * the new one.
    207   1.20       cgd 	 */
    208   1.77   thorpej 	free(curaddr, ksp);
    209   1.20       cgd 	return (newaddr);
    210   1.70     enami }
    211   1.70     enami 
    212   1.70     enami /*
    213   1.77   thorpej  * Add a malloc type to the system.
    214   1.77   thorpej  */
    215   1.77   thorpej void
    216   1.77   thorpej malloc_type_attach(struct malloc_type *type)
    217   1.77   thorpej {
    218   1.77   thorpej 
    219   1.77   thorpej 	if (type->ks_magic != M_MAGIC)
    220   1.77   thorpej 		panic("malloc_type_attach: bad magic");
    221   1.77   thorpej 
    222   1.77   thorpej #ifdef DIAGNOSTIC
    223   1.77   thorpej 	{
    224   1.77   thorpej 		struct malloc_type *ksp;
    225   1.77   thorpej 		for (ksp = kmemstatistics; ksp != NULL; ksp = ksp->ks_next) {
    226   1.77   thorpej 			if (ksp == type)
    227  1.131  christos 				panic("%s: `%s' already on list", __func__,
    228  1.131  christos 				    type->ks_shortdesc);
    229   1.77   thorpej 		}
    230   1.77   thorpej 	}
    231   1.77   thorpej #endif
    232   1.77   thorpej 
    233   1.77   thorpej #ifdef KMEMSTATS
    234   1.77   thorpej #else
    235   1.77   thorpej 	type->ks_limit = 0;
    236   1.77   thorpej #endif
    237   1.77   thorpej 
    238   1.77   thorpej 	type->ks_next = kmemstatistics;
    239   1.77   thorpej 	kmemstatistics = type;
    240   1.77   thorpej }
    241   1.77   thorpej 
    242   1.77   thorpej /*
    243   1.77   thorpej  * Remove a malloc type from the system..
    244   1.77   thorpej  */
    245   1.77   thorpej void
    246   1.77   thorpej malloc_type_detach(struct malloc_type *type)
    247   1.77   thorpej {
    248   1.77   thorpej 	struct malloc_type *ksp;
    249   1.77   thorpej 
    250   1.77   thorpej #ifdef DIAGNOSTIC
    251   1.77   thorpej 	if (type->ks_magic != M_MAGIC)
    252   1.77   thorpej 		panic("malloc_type_detach: bad magic");
    253   1.77   thorpej #endif
    254   1.77   thorpej 
    255   1.77   thorpej 	if (type == kmemstatistics)
    256   1.77   thorpej 		kmemstatistics = type->ks_next;
    257   1.77   thorpej 	else {
    258   1.77   thorpej 		for (ksp = kmemstatistics; ksp->ks_next != NULL;
    259   1.77   thorpej 		     ksp = ksp->ks_next) {
    260   1.77   thorpej 			if (ksp->ks_next == type) {
    261   1.77   thorpej 				ksp->ks_next = type->ks_next;
    262   1.77   thorpej 				break;
    263   1.77   thorpej 			}
    264   1.77   thorpej 		}
    265   1.77   thorpej #ifdef DIAGNOSTIC
    266   1.77   thorpej 		if (ksp->ks_next == NULL)
    267   1.77   thorpej 			panic("malloc_type_detach: not on list");
    268   1.77   thorpej #endif
    269   1.77   thorpej 	}
    270   1.77   thorpej 	type->ks_next = NULL;
    271   1.77   thorpej }
    272   1.77   thorpej 
    273   1.77   thorpej /*
    274   1.77   thorpej  * Set the limit on a malloc type.
    275   1.77   thorpej  */
    276   1.77   thorpej void
    277  1.105      yamt malloc_type_setlimit(struct malloc_type *type, u_long limit)
    278   1.77   thorpej {
    279   1.77   thorpej #ifdef KMEMSTATS
    280  1.113        ad 	mutex_spin_enter(&malloc_lock);
    281   1.77   thorpej 	type->ks_limit = limit;
    282  1.113        ad 	mutex_spin_exit(&malloc_lock);
    283   1.77   thorpej #endif
    284   1.77   thorpej }
    285   1.77   thorpej 
    286   1.77   thorpej /*
    287    1.1       cgd  * Initialize the kernel memory allocator
    288    1.1       cgd  */
    289   1.12  christos void
    290   1.69     enami kmeminit(void)
    291    1.1       cgd {
    292   1.77   thorpej 	__link_set_decl(malloc_types, struct malloc_type);
    293   1.77   thorpej 	struct malloc_type * const *ksp;
    294   1.23       tls #ifdef KMEMSTATS
    295   1.50  augustss 	long indx;
    296   1.23       tls #endif
    297    1.1       cgd 
    298  1.116        ad 	mutex_init(&malloc_lock, MUTEX_DEFAULT, IPL_VM);
    299  1.109        ad 
    300    1.1       cgd #ifdef KMEMSTATS
    301    1.1       cgd 	for (indx = 0; indx < MINBUCKET + 16; indx++) {
    302   1.49   thorpej 		if (1 << indx >= PAGE_SIZE)
    303   1.99       chs 			kmembuckets[indx].kb_elmpercl = 1;
    304    1.1       cgd 		else
    305   1.99       chs 			kmembuckets[indx].kb_elmpercl = PAGE_SIZE / (1 << indx);
    306   1.99       chs 		kmembuckets[indx].kb_highwat =
    307   1.99       chs 			5 * kmembuckets[indx].kb_elmpercl;
    308    1.1       cgd 	}
    309   1.62   thorpej #endif
    310   1.77   thorpej 
    311   1.77   thorpej 	/* Attach all of the statically-linked malloc types. */
    312   1.77   thorpej 	__link_set_foreach(ksp, malloc_types)
    313   1.77   thorpej 		malloc_type_attach(*ksp);
    314  1.127     pooka 
    315  1.127     pooka #ifdef MALLOC_DEBUG
    316  1.127     pooka 	debug_malloc_init();
    317  1.127     pooka #endif
    318    1.1       cgd }
    319   1.39   thorpej 
    320   1.39   thorpej #ifdef DDB
    321   1.39   thorpej #include <ddb/db_output.h>
    322   1.39   thorpej 
    323   1.39   thorpej /*
    324   1.39   thorpej  * Dump kmem statistics from ddb.
    325   1.39   thorpej  *
    326   1.39   thorpej  * usage: call dump_kmemstats
    327   1.39   thorpej  */
    328   1.69     enami void	dump_kmemstats(void);
    329   1.39   thorpej 
    330   1.39   thorpej void
    331   1.69     enami dump_kmemstats(void)
    332   1.39   thorpej {
    333   1.39   thorpej #ifdef KMEMSTATS
    334   1.77   thorpej 	struct malloc_type *ksp;
    335   1.39   thorpej 
    336   1.77   thorpej 	for (ksp = kmemstatistics; ksp != NULL; ksp = ksp->ks_next) {
    337   1.77   thorpej 		if (ksp->ks_memuse == 0)
    338   1.77   thorpej 			continue;
    339   1.77   thorpej 		db_printf("%s%.*s %ld\n", ksp->ks_shortdesc,
    340   1.77   thorpej 		    (int)(20 - strlen(ksp->ks_shortdesc)),
    341   1.77   thorpej 		    "                    ",
    342   1.77   thorpej 		    ksp->ks_memuse);
    343   1.39   thorpej 	}
    344   1.39   thorpej #else
    345   1.39   thorpej 	db_printf("Kmem stats are not being collected.\n");
    346   1.39   thorpej #endif /* KMEMSTATS */
    347   1.39   thorpej }
    348   1.39   thorpej #endif /* DDB */
    349   1.82      manu 
    350   1.82      manu 
    351   1.82      manu #if 0
    352   1.96     perry /*
    353   1.82      manu  * Diagnostic messages about "Data modified on
    354   1.82      manu  * freelist" indicate a memory corruption, but
    355   1.82      manu  * they do not help tracking it down.
    356   1.96     perry  * This function can be called at various places
    357   1.82      manu  * to sanity check malloc's freelist and discover
    358   1.82      manu  * where does the corruption take place.
    359   1.82      manu  */
    360   1.82      manu int
    361   1.82      manu freelist_sanitycheck(void) {
    362   1.82      manu 	int i,j;
    363   1.82      manu 	struct kmembuckets *kbp;
    364   1.82      manu 	struct freelist *freep;
    365   1.82      manu 	int rv = 0;
    366   1.96     perry 
    367   1.82      manu 	for (i = MINBUCKET; i <= MINBUCKET + 15; i++) {
    368   1.99       chs 		kbp = &kmembuckets[i];
    369   1.82      manu 		freep = (struct freelist *)kbp->kb_next;
    370   1.82      manu 		j = 0;
    371   1.82      manu 		while(freep) {
    372   1.82      manu 			vm_map_lock(kmem_map);
    373   1.82      manu 			rv = uvm_map_checkprot(kmem_map, (vaddr_t)freep,
    374   1.96     perry 			    (vaddr_t)freep + sizeof(struct freelist),
    375   1.82      manu 			    VM_PROT_WRITE);
    376   1.82      manu 			vm_map_unlock(kmem_map);
    377   1.82      manu 
    378   1.82      manu 			if ((rv == 0) || (*(int *)freep != WEIRD_ADDR)) {
    379   1.82      manu 				printf("bucket %i, chunck %d at %p modified\n",
    380   1.82      manu 				    i, j, freep);
    381   1.82      manu 				return 1;
    382   1.82      manu 			}
    383   1.82      manu 			freep = (struct freelist *)freep->next;
    384   1.82      manu 			j++;
    385   1.82      manu 		}
    386   1.82      manu 	}
    387   1.82      manu 
    388   1.82      manu 	return 0;
    389   1.82      manu }
    390   1.82      manu #endif
    391