Remove MALLOC_DEBUG and MALLOCLOG, which is dead code after malloc(9) move

to kmem(9).  Note: kmem(9) has debugging facilities under DEBUG/DIAGNOSTIC.
However, expensive kmguard and debug_freecheck have to be enabled manually.
This commit is contained in:
rmind 2012-04-28 23:03:39 +00:00
parent bc2122f164
commit 4b760398c3
9 changed files with 52 additions and 666 deletions

View File

@ -1,4 +1,4 @@
# $NetBSD: GENERIC,v 1.4 2010/05/17 11:41:28 kiyohara Exp $
# $NetBSD: GENERIC,v 1.5 2012/04/28 23:03:40 rmind Exp $
#
# GENERIC machine description file
#
@ -22,7 +22,7 @@ include "arch/ia64/conf/std.ia64"
options INCLUDE_CONFIG_FILE # embed config file in kernel binary
#ident "GENERIC-$Revision: 1.4 $"
#ident "GENERIC-$Revision: 1.5 $"
maxusers 32 # estimated number of users
@ -45,8 +45,6 @@ options DDB_ONPANIC=1 # see also sysctl(8): `ddb.onpanic'
options DDB_HISTORY_SIZE=512 # enable history editing in DDB
#options KGDB # remote debugger
#options KGDB_DEVNAME="\"com\"",KGDB_DEVADDR=0x3f8,KGDB_DEVRATE=9600
#options MALLOC_DEBUG
#options MALLOCLOG
makeoptions DEBUG="-g" # compile full symbol table
# File systems

View File

@ -1,4 +1,4 @@
# $NetBSD: GENERIC.SKI,v 1.7 2010/11/23 11:14:00 hannken Exp $
# $NetBSD: GENERIC.SKI,v 1.8 2012/04/28 23:03:40 rmind Exp $
#
# GENERIC machine description file
#
@ -22,7 +22,7 @@ include "arch/ia64/conf/std.ia64"
options INCLUDE_CONFIG_FILE # embed config file in kernel binary
#ident "GENERIC-$Revision: 1.7 $"
#ident "GENERIC-$Revision: 1.8 $"
maxusers 32 # estimated number of users
@ -44,8 +44,6 @@ options DDB # in-kernel debugger
#options DDB_HISTORY_SIZE=512 # enable history editing in DDB
#options KGDB # remote debugger
#options KGDB_DEVNAME="\"com\"",KGDB_DEVADDR=0x3f8,KGDB_DEVRATE=9600
#options MALLOC_DEBUG
#options MALLOCLOG
makeoptions DEBUG="-g" # compile full symbol table
# File systems

View File

@ -1,4 +1,4 @@
# $NetBSD: files,v 1.1047 2012/04/07 05:36:49 christos Exp $
# $NetBSD: files,v 1.1048 2012/04/28 23:03:39 rmind Exp $
# @(#)files.newconf 7.5 (Berkeley) 5/10/93
version 20100430
@ -69,9 +69,6 @@ defflag opt_ntp.h PPS_SYNC NTP
defflag opt_ptm.h NO_DEV_PTM COMPAT_BSDPTY
defparam opt_kmempages.h NKMEMPAGES NKMEMPAGES_MIN NKMEMPAGES_MAX
defflag opt_malloclog.h MALLOCLOG
defparam opt_malloclog.h MALLOCLOGSIZE
defflag opt_malloc_debug.h MALLOC_DEBUG
defflag opt_pool.h POOL_DIAGNOSTIC
defparam opt_poollog.h POOL_LOGSIZE
@ -1490,8 +1487,6 @@ file kern/kern_ksyms.c ksyms | ddb | modular needs-flag
file kern/kern_lock.c
file kern/kern_lwp.c
file kern/kern_malloc.c
file kern/kern_malloc_stdtype.c
file kern/kern_malloc_debug.c malloc_debug
file kern/kern_module.c
file kern/kern_module_vfs.c
file kern/kern_mutex.c

View File

@ -1,4 +1,4 @@
/* $NetBSD: db_command.c,v 1.137 2011/11/13 03:21:51 mrg Exp $ */
/* $NetBSD: db_command.c,v 1.138 2012/04/28 23:03:39 rmind Exp $ */
/*
* Copyright (c) 1996, 1997, 1998, 1999, 2002, 2009 The NetBSD Foundation, Inc.
@ -60,7 +60,7 @@
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: db_command.c,v 1.137 2011/11/13 03:21:51 mrg Exp $");
__KERNEL_RCSID(0, "$NetBSD: db_command.c,v 1.138 2012/04/28 23:03:39 rmind Exp $");
#ifdef _KERNEL_OPT
#include "opt_aio.h"
@ -79,7 +79,6 @@ __KERNEL_RCSID(0, "$NetBSD: db_command.c,v 1.137 2011/11/13 03:21:51 mrg Exp $")
#include <sys/reboot.h>
#include <sys/device.h>
#include <sys/lwp.h>
#include <sys/malloc.h>
#include <sys/mbuf.h>
#include <sys/namei.h>
#include <sys/pool.h>
@ -192,7 +191,6 @@ static void db_help_print_cmd(db_expr_t, bool, db_expr_t, const char *);
static void db_lock_print_cmd(db_expr_t, bool, db_expr_t, const char *);
static void db_mount_print_cmd(db_expr_t, bool, db_expr_t, const char *);
static void db_mbuf_print_cmd(db_expr_t, bool, db_expr_t, const char *);
static void db_malloc_print_cmd(db_expr_t, bool, db_expr_t, const char *);
static void db_map_print_cmd(db_expr_t, bool, db_expr_t, const char *);
static void db_namecache_print_cmd(db_expr_t, bool, db_expr_t,
const char *);
@ -248,7 +246,6 @@ static const struct db_command db_show_cmds[] = {
"Print the files open by process at address",
"[/f] address", NULL) },
{ DDB_ADD_CMD("lock", db_lock_print_cmd, 0,NULL,NULL,NULL) },
{ DDB_ADD_CMD("malloc", db_malloc_print_cmd,0,NULL,NULL,NULL) },
{ DDB_ADD_CMD("map", db_map_print_cmd, 0,
"Print the vm_map at address.", "[/f] address",NULL) },
{ DDB_ADD_CMD("module", db_show_module_cmd, 0,
@ -983,22 +980,6 @@ db_map_print_cmd(db_expr_t addr, bool have_addr, db_expr_t count,
#endif /* XXX CRASH(8) */
}
/*ARGSUSED*/
static void
db_malloc_print_cmd(db_expr_t addr, bool have_addr,
db_expr_t count, const char *modif)
{
#ifdef MALLOC_DEBUG
if (!have_addr)
addr = 0;
debug_malloc_printit(db_printf, (vaddr_t) addr);
#else
db_printf("The kernel is not built with the MALLOC_DEBUG option.\n");
#endif /* MALLOC_DEBUG */
}
/*ARGSUSED*/
static void
db_object_print_cmd(db_expr_t addr, bool have_addr,

View File

@ -1,4 +1,4 @@
/* $NetBSD: kern_malloc.c,v 1.138 2012/02/06 12:13:44 drochner Exp $ */
/* $NetBSD: kern_malloc.c,v 1.139 2012/04/28 23:03:40 rmind Exp $ */
/*
* Copyright (c) 1987, 1991, 1993
@ -65,47 +65,45 @@
* @(#)kern_malloc.c 8.4 (Berkeley) 5/20/95
*/
/*
* Wrapper interface for obsolete malloc(9).
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: kern_malloc.c,v 1.138 2012/02/06 12:13:44 drochner Exp $");
__KERNEL_RCSID(0, "$NetBSD: kern_malloc.c,v 1.139 2012/04/28 23:03:40 rmind Exp $");
#include <sys/param.h>
#include <sys/proc.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/kmem.h>
#include <sys/systm.h>
#include <sys/debug.h>
#include <sys/mutex.h>
#include <sys/lockdebug.h>
#include <uvm/uvm_extern.h>
#include "opt_kmemstats.h"
#include "opt_malloclog.h"
#include "opt_malloc_debug.h"
struct kmembuckets kmembuckets[MINBUCKET + 16];
struct kmemusage *kmemusage;
struct malloc_type *kmemstatistics;
kmutex_t malloc_lock;
struct malloc_header {
/* Total size, include the header. */
size_t mh_size;
} __aligned(ALIGNBYTES+1);
/*
* Built-in malloc types. Note: ought to be removed.
*/
MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory");
MALLOC_DEFINE(M_DMAMAP, "DMA map", "bus_dma(9) structures");
MALLOC_DEFINE(M_FREE, "free", "should be on free list");
MALLOC_DEFINE(M_PCB, "pcb", "protocol control block");
MALLOC_DEFINE(M_TEMP, "temp", "misc. temporary data buffers");
MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
MALLOC_DEFINE(M_FTABLE, "fragtbl", "fragment reassembly header");
MALLOC_DEFINE(M_UFSMNT, "UFS mount", "UFS mount structure");
MALLOC_DEFINE(M_NETADDR, "Export Host", "Export host address structure");
MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "internet multicast options");
MALLOC_DEFINE(M_IPMADDR, "in_multi", "internet multicast address");
MALLOC_DEFINE(M_MRTABLE, "mrt", "multicast routing tables");
MALLOC_DEFINE(M_BWMETER, "bwmeter", "multicast upcall bw meters");
/*
* Allocate a block of memory
* Header contains total size, including the header itself.
*/
#ifdef MALLOCLOG
void *
_kern_malloc(unsigned long size, struct malloc_type *ksp, int flags,
const char *file, long line)
#else
struct malloc_header {
size_t mh_size;
} __aligned(ALIGNBYTES + 1);
void *
kern_malloc(unsigned long size, struct malloc_type *ksp, int flags)
#endif /* MALLOCLOG */
{
const int kmflags = (flags & M_NOWAIT) ? KM_NOSLEEP : KM_SLEEP;
size_t allocsize, hdroffset;
@ -133,16 +131,8 @@ kern_malloc(unsigned long size, struct malloc_type *ksp, int flags)
return mh + 1;
}
/*
* Free a block of memory allocated by malloc.
*/
#ifdef MALLOCLOG
void
_kern_free(void *addr, struct malloc_type *ksp, const char *file, long line)
#else
void
kern_free(void *addr, struct malloc_type *ksp)
#endif /* MALLOCLOG */
{
struct malloc_header *mh;
@ -156,9 +146,6 @@ kern_free(void *addr, struct malloc_type *ksp)
kmem_intr_free(mh, mh->mh_size);
}
/*
* Change the size of a block of memory.
*/
void *
kern_realloc(void *curaddr, unsigned long newsize, struct malloc_type *ksp,
int flags)
@ -171,21 +158,19 @@ kern_realloc(void *curaddr, unsigned long newsize, struct malloc_type *ksp,
* realloc() with a NULL pointer is the same as malloc().
*/
if (curaddr == NULL)
return (malloc(newsize, ksp, flags));
return malloc(newsize, ksp, flags);
/*
* realloc() with zero size is the same as free().
*/
if (newsize == 0) {
free(curaddr, ksp);
return (NULL);
return NULL;
}
#ifdef LOCKDEBUG
if ((flags & M_NOWAIT) == 0) {
ASSERT_SLEEPABLE();
}
#endif
mh = curaddr;
mh--;
@ -196,7 +181,7 @@ kern_realloc(void *curaddr, unsigned long newsize, struct malloc_type *ksp,
* If we already actually have as much as they want, we're done.
*/
if (newsize <= cursize)
return (curaddr);
return curaddr;
/*
* Can't satisfy the allocation with the existing block.
@ -209,7 +194,7 @@ kern_realloc(void *curaddr, unsigned long newsize, struct malloc_type *ksp,
* Return NULL to indicate that failure. The old
* pointer is still valid.
*/
return (NULL);
return NULL;
}
memcpy(newaddr, curaddr, cursize);
@ -218,81 +203,19 @@ kern_realloc(void *curaddr, unsigned long newsize, struct malloc_type *ksp,
* the new one.
*/
free(curaddr, ksp);
return (newaddr);
return newaddr;
}
/*
* Add a malloc type to the system.
*/
void
malloc_type_attach(struct malloc_type *type)
{
if (type->ks_magic != M_MAGIC)
panic("malloc_type_attach: bad magic");
#ifdef DIAGNOSTIC
{
struct malloc_type *ksp;
for (ksp = kmemstatistics; ksp != NULL; ksp = ksp->ks_next) {
if (ksp == type)
panic("%s: `%s' already on list", __func__,
type->ks_shortdesc);
}
}
#endif
#ifdef KMEMSTATS
#else
type->ks_limit = 0;
#endif
type->ks_next = kmemstatistics;
kmemstatistics = type;
KASSERT(type->ks_magic == M_MAGIC);
}
/*
* Remove a malloc type from the system..
*/
void
malloc_type_detach(struct malloc_type *type)
{
struct malloc_type *ksp;
#ifdef DIAGNOSTIC
if (type->ks_magic != M_MAGIC)
panic("malloc_type_detach: bad magic");
#endif
if (type == kmemstatistics)
kmemstatistics = type->ks_next;
else {
for (ksp = kmemstatistics; ksp->ks_next != NULL;
ksp = ksp->ks_next) {
if (ksp->ks_next == type) {
ksp->ks_next = type->ks_next;
break;
}
}
#ifdef DIAGNOSTIC
if (ksp->ks_next == NULL)
panic("malloc_type_detach: not on list");
#endif
}
type->ks_next = NULL;
}
/*
* Set the limit on a malloc type.
*/
void
malloc_type_setlimit(struct malloc_type *type, u_long limit)
{
#ifdef KMEMSTATS
mutex_spin_enter(&malloc_lock);
type->ks_limit = limit;
mutex_spin_exit(&malloc_lock);
#endif
KASSERT(type->ks_magic == M_MAGIC);
}
/*
@ -303,100 +226,8 @@ kmeminit(void)
{
__link_set_decl(malloc_types, struct malloc_type);
struct malloc_type * const *ksp;
#ifdef KMEMSTATS
long indx;
#endif
mutex_init(&malloc_lock, MUTEX_DEFAULT, IPL_VM);
#ifdef KMEMSTATS
for (indx = 0; indx < MINBUCKET + 16; indx++) {
if (1 << indx >= PAGE_SIZE)
kmembuckets[indx].kb_elmpercl = 1;
else
kmembuckets[indx].kb_elmpercl = PAGE_SIZE / (1 << indx);
kmembuckets[indx].kb_highwat =
5 * kmembuckets[indx].kb_elmpercl;
}
#endif
/* Attach all of the statically-linked malloc types. */
__link_set_foreach(ksp, malloc_types)
malloc_type_attach(*ksp);
#ifdef MALLOC_DEBUG
debug_malloc_init();
#endif
}
#ifdef DDB
#include <ddb/db_output.h>
/*
* Dump kmem statistics from ddb.
*
* usage: call dump_kmemstats
*/
void dump_kmemstats(void);
void
dump_kmemstats(void)
{
#ifdef KMEMSTATS
struct malloc_type *ksp;
for (ksp = kmemstatistics; ksp != NULL; ksp = ksp->ks_next) {
if (ksp->ks_memuse == 0)
continue;
db_printf("%s%.*s %ld\n", ksp->ks_shortdesc,
(int)(20 - strlen(ksp->ks_shortdesc)),
" ",
ksp->ks_memuse);
}
#else
db_printf("Kmem stats are not being collected.\n");
#endif /* KMEMSTATS */
}
#endif /* DDB */
#if 0
/*
* Diagnostic messages about "Data modified on
* freelist" indicate a memory corruption, but
* they do not help tracking it down.
* This function can be called at various places
* to sanity check malloc's freelist and discover
* where does the corruption take place.
*/
int
freelist_sanitycheck(void) {
int i,j;
struct kmembuckets *kbp;
struct freelist *freep;
int rv = 0;
for (i = MINBUCKET; i <= MINBUCKET + 15; i++) {
kbp = &kmembuckets[i];
freep = (struct freelist *)kbp->kb_next;
j = 0;
while(freep) {
vm_map_lock(kmem_map);
rv = uvm_map_checkprot(kmem_map, (vaddr_t)freep,
(vaddr_t)freep + sizeof(struct freelist),
VM_PROT_WRITE);
vm_map_unlock(kmem_map);
if ((rv == 0) || (*(int *)freep != WEIRD_ADDR)) {
printf("bucket %i, chunck %d at %p modified\n",
i, j, freep);
return 1;
}
freep = (struct freelist *)freep->next;
j++;
}
}
return 0;
}
#endif

View File

@ -1,327 +0,0 @@
/* $NetBSD: kern_malloc_debug.c,v 1.26 2011/09/01 18:29:29 matt Exp $ */
/*
* Copyright (c) 1999, 2000 Artur Grabowski <art@openbsd.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* OpenBSD: kern_malloc_debug.c,v 1.10 2001/07/26 13:33:52 art Exp
*/
/*
* This really belongs in kern/kern_malloc.c, but it was too much pollution.
*/
/*
* It's only possible to debug one type/size at a time. The question is
* if this is a limitation or a feature. We never want to run this as the
* default malloc because we'll run out of memory really fast. Adding
* more types will also add to the complexity of the code.
*
* This is really simple. Every malloc() allocates two virtual pages,
* the second page is left unmapped, and the value returned is aligned
* so that it ends at (or very close to) the page boundary to catch overflows.
* Every free() changes the protection of the first page to VM_PROT_NONE so
* that we can catch any dangling writes to it.
* To minimize the risk of writes to recycled chunks we keep an LRU of latest
* freed chunks. The length of it is controlled by MALLOC_DEBUG_CHUNKS.
*
* Don't expect any performance.
*
* TODO:
* - support for size >= PAGE_SIZE
* - add support to the fault handler to give better diagnostics if we fail.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: kern_malloc_debug.c,v 1.26 2011/09/01 18:29:29 matt Exp $");
#include <sys/param.h>
#include <sys/proc.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/systm.h>
#include <sys/pool.h>
#include <uvm/uvm.h>
/*
* debug_malloc_type and debug_malloc_size define the type and size of
* memory to be debugged. Use 0 for a wildcard. debug_malloc_size_lo
* is the lower limit and debug_malloc_size_hi the upper limit of sizes
* being debugged; 0 will not work as a wildcard for the upper limit.
* For any debugging to take place, type must be != NULL, size must be >= 0,
* and if the limits are being used, size must be set to 0.
* See /usr/src/sys/sys/malloc.h and malloc(9) for a list of types.
*
* Although those are variables, it's a really bad idea to change the type
* if any memory chunks of this type are used. It's ok to change the size
* in runtime.
*/
struct malloc_type *debug_malloc_type = NULL;
int debug_malloc_size = -1;
int debug_malloc_size_lo = -1;
int debug_malloc_size_hi = -1;
/*
* MALLOC_DEBUG_CHUNKS is the number of memory chunks we require on the
* freelist before we reuse them.
*/
#define MALLOC_DEBUG_CHUNKS 16
void debug_malloc_allocate_free(int);
struct debug_malloc_entry {
TAILQ_ENTRY(debug_malloc_entry) md_list;
vaddr_t md_va;
paddr_t md_pa;
size_t md_size;
struct malloc_type *md_type;
};
TAILQ_HEAD(,debug_malloc_entry) debug_malloc_freelist =
TAILQ_HEAD_INITIALIZER(debug_malloc_freelist);
TAILQ_HEAD(,debug_malloc_entry) debug_malloc_usedlist =
TAILQ_HEAD_INITIALIZER(debug_malloc_usedlist);
int debug_malloc_allocs;
int debug_malloc_frees;
int debug_malloc_pages;
int debug_malloc_chunks_on_freelist;
static struct pool debug_malloc_pool;
void
debug_malloc_init(void)
{
pool_init(&debug_malloc_pool, sizeof(struct debug_malloc_entry),
0, 0, 0, "mdbepl", NULL, IPL_VM);
}
int
debug_malloc(unsigned long size, struct malloc_type *type, int flags,
void **addr)
{
struct debug_malloc_entry *md = NULL;
int s, wait = !(flags & M_NOWAIT);
/* Careful not to compare unsigned long to int -1 */
if ((type != debug_malloc_type && debug_malloc_type != 0) ||
(size != debug_malloc_size && debug_malloc_size != 0) ||
(debug_malloc_size_lo != -1 && size < debug_malloc_size_lo) ||
(debug_malloc_size_hi != -1 && size > debug_malloc_size_hi))
return (0);
/* XXX - fix later */
if (size > PAGE_SIZE)
return (0);
s = splvm();
if (debug_malloc_chunks_on_freelist < MALLOC_DEBUG_CHUNKS)
debug_malloc_allocate_free(wait);
md = TAILQ_FIRST(&debug_malloc_freelist);
if (md == NULL) {
splx(s);
return (0);
}
TAILQ_REMOVE(&debug_malloc_freelist, md, md_list);
debug_malloc_chunks_on_freelist--;
TAILQ_INSERT_HEAD(&debug_malloc_usedlist, md, md_list);
debug_malloc_allocs++;
splx(s);
pmap_kenter_pa(md->md_va, md->md_pa,
VM_PROT_READ|VM_PROT_WRITE, PMAP_KMPAGE);
pmap_update(pmap_kernel());
md->md_size = size;
md->md_type = type;
/*
* Align the returned addr so that it ends where the first page
* ends. roundup to get decent alignment.
*/
*addr = (void *)(md->md_va + PAGE_SIZE - roundup(size, sizeof(long)));
if (*addr != NULL && (flags & M_ZERO))
memset(*addr, 0, size);
return (1);
}
int
debug_free(void *addr, struct malloc_type *type)
{
struct debug_malloc_entry *md;
vaddr_t va;
int s;
if (type != debug_malloc_type && debug_malloc_type != 0)
return (0);
/*
* trunc_page to get the address of the page.
*/
va = trunc_page((vaddr_t)addr);
s = splvm();
TAILQ_FOREACH(md, &debug_malloc_usedlist, md_list)
if (md->md_va == va)
break;
/*
* If we are not responsible for this entry, let the normal free
* handle it
*/
if (md == NULL) {
/*
* sanity check. Check for multiple frees.
*/
TAILQ_FOREACH(md, &debug_malloc_freelist, md_list)
if (md->md_va == va)
panic("debug_free: already free");
splx(s);
return (0);
}
debug_malloc_frees++;
TAILQ_REMOVE(&debug_malloc_usedlist, md, md_list);
TAILQ_INSERT_TAIL(&debug_malloc_freelist, md, md_list);
debug_malloc_chunks_on_freelist++;
/*
* unmap the page.
*/
pmap_kremove(md->md_va, PAGE_SIZE);
pmap_update(pmap_kernel());
splx(s);
return (1);
}
/*
* Add one chunk to the freelist.
*
* called at splvm.
*/
void
debug_malloc_allocate_free(int wait)
{
vaddr_t va, offset;
struct vm_page *pg;
struct debug_malloc_entry *md;
md = pool_get(&debug_malloc_pool, wait ? PR_WAITOK : PR_NOWAIT);
if (md == NULL)
return;
va = uvm_km_alloc(kmem_map, PAGE_SIZE * 2, 0,
UVM_KMF_VAONLY | (wait ? UVM_KMF_NOWAIT : 0));
if (va == 0) {
pool_put(&debug_malloc_pool, md);
return;
}
offset = va - vm_map_min(kernel_map);
for (;;) {
pg = uvm_pagealloc(NULL, offset, NULL, UVM_FLAG_COLORMATCH);
if (pg) {
pg->flags &= ~PG_BUSY; /* new page */
UVM_PAGE_OWN(pg, NULL);
}
if (pg)
break;
if (wait == 0) {
uvm_km_free(kmem_map, va, va + PAGE_SIZE * 2,
UVM_KMF_VAONLY);
pool_put(&debug_malloc_pool, md);
return;
}
uvm_wait("debug_malloc");
}
md->md_va = va;
md->md_pa = VM_PAGE_TO_PHYS(pg);
debug_malloc_pages++;
TAILQ_INSERT_HEAD(&debug_malloc_freelist, md, md_list);
debug_malloc_chunks_on_freelist++;
}
void
debug_malloc_print(void)
{
debug_malloc_printit(printf, 0);
}
void
debug_malloc_printit(void (*pr)(const char *, ...), vaddr_t addr)
{
struct debug_malloc_entry *md;
if (addr) {
TAILQ_FOREACH(md, &debug_malloc_freelist, md_list) {
if (addr >= md->md_va &&
addr < md->md_va + 2 * PAGE_SIZE) {
(*pr)("Memory at address 0x%x is in a freed "
"area. type %s, size: %d\n ",
addr, md->md_type->ks_shortdesc,
md->md_size);
return;
}
}
TAILQ_FOREACH(md, &debug_malloc_usedlist, md_list) {
if (addr >= md->md_va + PAGE_SIZE &&
addr < md->md_va + 2 * PAGE_SIZE) {
(*pr)("Memory at address 0x%x is just outside "
"an allocated area. type %s, size: %d\n",
addr, md->md_type->ks_shortdesc,
md->md_size);
return;
}
}
(*pr)("Memory at address 0x%x is outside debugged malloc.\n");
return;
}
(*pr)("allocs: %d\n", debug_malloc_allocs);
(*pr)("frees: %d\n", debug_malloc_frees);
(*pr)("pages used: %d\n", debug_malloc_pages);
(*pr)("chunks on freelist: %d\n", debug_malloc_chunks_on_freelist);
(*pr)("\taddr:\tsize:\n");
(*pr)("free chunks:\n");
TAILQ_FOREACH(md, &debug_malloc_freelist, md_list)
(*pr)("\t0x%x\t0x%x\t%s\n", md->md_va, md->md_size,
md->md_type->ks_shortdesc);
(*pr)("used chunks:\n");
TAILQ_FOREACH(md, &debug_malloc_usedlist, md_list)
(*pr)("\t0x%x\t0x%x\t%s\n", md->md_va, md->md_size,
md->md_type->ks_shortdesc);
}

View File

@ -1,58 +0,0 @@
/* $NetBSD: kern_malloc_stdtype.c,v 1.4 2010/04/23 19:09:15 rmind Exp $ */
/*
* Copyright (c) 1987, 1991, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* @(#)kern_malloc.c 8.4 (Berkeley) 5/20/95
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: kern_malloc_stdtype.c,v 1.4 2010/04/23 19:09:15 rmind Exp $");
#include <sys/param.h>
#include <sys/malloc.h>
/*
* The following are standard, built-in malloc types and are not
* specific to any subsystem.
*/
MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory");
MALLOC_DEFINE(M_DMAMAP, "DMA map", "bus_dma(9) structures");
MALLOC_DEFINE(M_FREE, "free", "should be on free list");
MALLOC_DEFINE(M_PCB, "pcb", "protocol control block");
MALLOC_DEFINE(M_TEMP, "temp", "misc. temporary data buffers");
/* XXX These should all be elsewhere. */
MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
MALLOC_DEFINE(M_FTABLE, "fragtbl", "fragment reassembly header");
MALLOC_DEFINE(M_UFSMNT, "UFS mount", "UFS mount structure");
MALLOC_DEFINE(M_NETADDR, "Export Host", "Export host address structure");
MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "internet multicast options");
MALLOC_DEFINE(M_IPMADDR, "in_multi", "internet multicast address");
MALLOC_DEFINE(M_MRTABLE, "mrt", "multicast routing tables");
MALLOC_DEFINE(M_BWMETER, "bwmeter", "multicast upcall bw meters");

View File

@ -1,4 +1,4 @@
/* $NetBSD: malloc.h,v 1.110 2011/11/24 16:16:49 jakllsch Exp $ */
/* $NetBSD: malloc.h,v 1.111 2012/04/28 23:03:40 rmind Exp $ */
/*
* Copyright (c) 1987, 1993
@ -34,13 +34,6 @@
#ifndef _SYS_MALLOC_H_
#define _SYS_MALLOC_H_
#if defined(_KERNEL_OPT)
#include "opt_kmemstats.h"
#include "opt_malloclog.h"
#include "opt_malloc_debug.h"
#endif
#ifdef _KERNEL
/*
@ -61,8 +54,6 @@ MALLOC_DECLARE(M_DMAMAP);
MALLOC_DECLARE(M_FREE);
MALLOC_DECLARE(M_PCB);
MALLOC_DECLARE(M_TEMP);
/* XXX These should all be declared elsewhere. */
MALLOC_DECLARE(M_RTABLE);
MALLOC_DECLARE(M_FTABLE);
MALLOC_DECLARE(M_UFSMNT);
@ -71,6 +62,15 @@ MALLOC_DECLARE(M_IPMOPTS);
MALLOC_DECLARE(M_IPMADDR);
MALLOC_DECLARE(M_MRTABLE);
MALLOC_DECLARE(M_BWMETER);
void *kern_malloc(unsigned long, struct malloc_type *, int);
void *kern_realloc(void *, unsigned long, struct malloc_type *, int);
void kern_free(void *, struct malloc_type *);
#define malloc(size, type, flags) kern_malloc(size, type, flags)
#define free(addr, type) kern_free(addr, type)
#define realloc(ptr, size, type, flags) kern_realloc(ptr, size, type, flags)
#endif /* _KERNEL */
/*
@ -87,35 +87,4 @@ struct kmembuckets {
long kb_couldfree; /* over high water mark and could free */
};
#ifdef _KERNEL
#ifdef MALLOCLOG
void *_kern_malloc(unsigned long, struct malloc_type *, int, const char *, long);
void _kern_free(void *, struct malloc_type *, const char *, long);
#define malloc(size, type, flags) \
_kern_malloc((size), (type), (flags), __FILE__, __LINE__)
#define free(addr, type) \
_kern_free((addr), (type), __FILE__, __LINE__)
#else
void *kern_malloc(unsigned long, struct malloc_type *, int);
void kern_free(void *, struct malloc_type *);
#define malloc(size, type, flags) kern_malloc(size, type, flags)
#define free(addr, type) kern_free(addr, type)
#endif /* MALLOCLOG */
#ifdef MALLOC_DEBUG
void debug_malloc_init(void);
int debug_malloc(unsigned long, struct malloc_type *, int, void **);
int debug_free(void *, struct malloc_type *);
void debug_malloc_print(void);
void debug_malloc_printit(void (*)(const char *, ...)
__printflike(1, 2), vaddr_t);
#endif /* MALLOC_DEBUG */
void *kern_realloc(void *, unsigned long, struct malloc_type *, int);
#define realloc(ptr, size, type, flags) \
kern_realloc(ptr, size, type, flags)
unsigned long
malloc_roundup(unsigned long);
#endif /* _KERNEL */
#endif /* !_SYS_MALLOC_H_ */

View File

@ -1,4 +1,4 @@
/* $NetBSD: mallocvar.h,v 1.10 2011/10/15 21:14:57 christos Exp $ */
/* $NetBSD: mallocvar.h,v 1.11 2012/04/28 23:03:40 rmind Exp $ */
/*
* Copyright (c) 1987, 1993
@ -86,7 +86,6 @@ struct malloc_type type[1] = { \
void malloc_type_attach(struct malloc_type *);
void malloc_type_detach(struct malloc_type *);
void malloc_type_setlimit(struct malloc_type *, u_long);
#endif /* _KERNEL */
#endif /* _SYS_MALLOCVAR_H_ */