1994-05-24 10:09:53 +00:00
|
|
|
/*
|
|
|
|
* 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. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the University of
|
|
|
|
* California, Berkeley and its contributors.
|
|
|
|
* 4. 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.3 (Berkeley) 1/4/94
|
1999-08-28 01:08:13 +00:00
|
|
|
* $FreeBSD$
|
1994-05-24 10:09:53 +00:00
|
|
|
*/
|
|
|
|
|
1998-02-23 07:41:23 +00:00
|
|
|
#include "opt_vm.h"
|
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
#include <sys/param.h>
|
1994-05-25 09:21:21 +00:00
|
|
|
#include <sys/systm.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/malloc.h>
|
1996-01-29 09:58:34 +00:00
|
|
|
#include <sys/mbuf.h>
|
2000-10-20 07:29:16 +00:00
|
|
|
#include <sys/mutex.h>
|
1995-12-07 12:48:31 +00:00
|
|
|
#include <sys/vmmeter.h>
|
1997-08-05 00:02:08 +00:00
|
|
|
#include <sys/lock.h>
|
2000-09-23 00:01:37 +00:00
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
#include <vm/vm.h>
|
1995-12-07 12:48:31 +00:00
|
|
|
#include <vm/vm_param.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
#include <vm/vm_kern.h>
|
1995-12-07 12:48:31 +00:00
|
|
|
#include <vm/vm_extern.h>
|
1997-08-05 00:02:08 +00:00
|
|
|
#include <vm/pmap.h>
|
|
|
|
#include <vm/vm_map.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
|
1999-09-19 08:40:11 +00:00
|
|
|
#if defined(INVARIANTS) && defined(__i386__)
|
|
|
|
#include <machine/cpu.h>
|
|
|
|
#endif
|
|
|
|
|
1999-10-03 12:18:29 +00:00
|
|
|
MALLOC_DEFINE(M_CACHE, "cache", "Various Dynamically allocated caches");
|
1999-09-11 16:41:39 +00:00
|
|
|
MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory");
|
|
|
|
MALLOC_DEFINE(M_TEMP, "temp", "misc temporary data buffers");
|
|
|
|
|
1999-11-22 02:45:11 +00:00
|
|
|
MALLOC_DEFINE(M_IP6OPT, "ip6opt", "IPv6 options");
|
|
|
|
MALLOC_DEFINE(M_IP6NDP, "ip6ndp", "IPv6 Neighbor Discovery");
|
|
|
|
|
1995-09-09 18:10:37 +00:00
|
|
|
static void kmeminit __P((void *));
|
1995-08-28 09:19:25 +00:00
|
|
|
SYSINIT(kmem, SI_SUB_KMEM, SI_ORDER_FIRST, kmeminit, NULL)
|
|
|
|
|
1997-10-12 20:26:33 +00:00
|
|
|
static MALLOC_DEFINE(M_FREE, "free", "should be on free list");
|
|
|
|
|
1998-11-10 08:46:24 +00:00
|
|
|
static struct malloc_type *kmemstatistics;
|
1997-10-10 14:06:34 +00:00
|
|
|
static struct kmembuckets bucket[MINBUCKET + 16];
|
|
|
|
static struct kmemusage *kmemusage;
|
|
|
|
static char *kmembase;
|
1997-09-16 13:52:04 +00:00
|
|
|
static char *kmemlimit;
|
2000-02-16 21:11:33 +00:00
|
|
|
|
2000-10-20 07:29:16 +00:00
|
|
|
MUTEX_DECLARE(static, malloc_mtx);
|
2000-09-11 02:32:30 +00:00
|
|
|
|
2000-02-16 21:11:33 +00:00
|
|
|
u_int vm_kmem_size;
|
1994-05-24 10:09:53 +00:00
|
|
|
|
1999-01-10 01:58:29 +00:00
|
|
|
#ifdef INVARIANTS
|
1994-05-24 10:09:53 +00:00
|
|
|
/*
|
|
|
|
* This structure provides a set of masks to catch unaligned frees.
|
|
|
|
*/
|
1995-12-14 08:32:45 +00:00
|
|
|
static long addrmask[] = { 0,
|
1994-05-24 10:09:53 +00:00
|
|
|
0x00000001, 0x00000003, 0x00000007, 0x0000000f,
|
|
|
|
0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff,
|
|
|
|
0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff,
|
|
|
|
0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff,
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The WEIRD_ADDR is used as known text to copy into free objects so
|
|
|
|
* that modifications after frees can be detected.
|
|
|
|
*/
|
1995-04-16 11:25:15 +00:00
|
|
|
#define WEIRD_ADDR 0xdeadc0de
|
|
|
|
#define MAX_COPY 64
|
1994-05-24 10:09:53 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Normally the first word of the structure is used to hold the list
|
|
|
|
* pointer for free objects. However, when running with diagnostics,
|
|
|
|
* we use the third and fourth fields, so as to catch modifications
|
|
|
|
* in the most commonly trashed first two words.
|
|
|
|
*/
|
|
|
|
struct freelist {
|
|
|
|
long spare0;
|
1997-10-10 18:14:23 +00:00
|
|
|
struct malloc_type *type;
|
1994-05-24 10:09:53 +00:00
|
|
|
long spare1;
|
|
|
|
caddr_t next;
|
|
|
|
};
|
1999-01-08 17:31:30 +00:00
|
|
|
#else /* !INVARIANTS */
|
1994-05-24 10:09:53 +00:00
|
|
|
struct freelist {
|
|
|
|
caddr_t next;
|
|
|
|
};
|
1999-01-08 17:31:30 +00:00
|
|
|
#endif /* INVARIANTS */
|
1994-05-24 10:09:53 +00:00
|
|
|
|
|
|
|
/*
|
1999-01-21 08:29:12 +00:00
|
|
|
* malloc:
|
|
|
|
*
|
|
|
|
* Allocate a block of memory.
|
|
|
|
*
|
|
|
|
* If M_NOWAIT is set, this routine will not block and return NULL if
|
|
|
|
* the allocation fails.
|
|
|
|
*
|
|
|
|
* If M_ASLEEP is set (M_NOWAIT must also be set), this routine
|
|
|
|
* will have the side effect of calling asleep() if it returns NULL,
|
|
|
|
* allowing the parent to await() at some future time.
|
1994-05-24 10:09:53 +00:00
|
|
|
*/
|
|
|
|
void *
|
|
|
|
malloc(size, type, flags)
|
|
|
|
unsigned long size;
|
1997-10-10 18:14:23 +00:00
|
|
|
struct malloc_type *type;
|
1997-10-10 14:06:34 +00:00
|
|
|
int flags;
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
|
|
|
register struct kmembuckets *kbp;
|
|
|
|
register struct kmemusage *kup;
|
|
|
|
register struct freelist *freep;
|
|
|
|
long indx, npg, allocsize;
|
|
|
|
int s;
|
|
|
|
caddr_t va, cp, savedlist;
|
1999-01-08 17:31:30 +00:00
|
|
|
#ifdef INVARIANTS
|
1994-05-24 10:09:53 +00:00
|
|
|
long *end, *lp;
|
|
|
|
int copysize;
|
1999-01-27 21:50:00 +00:00
|
|
|
const char *savedtype;
|
1994-05-24 10:09:53 +00:00
|
|
|
#endif
|
1997-10-10 18:14:23 +00:00
|
|
|
register struct malloc_type *ksp = type;
|
1997-10-10 14:06:34 +00:00
|
|
|
|
1999-09-19 08:40:11 +00:00
|
|
|
#if defined(INVARIANTS) && defined(__i386__)
|
|
|
|
if (flags == M_WAITOK)
|
|
|
|
KASSERT(intr_nesting_level == 0,
|
|
|
|
("malloc(M_WAITOK) in interrupt context"));
|
|
|
|
#endif
|
1994-05-24 10:09:53 +00:00
|
|
|
indx = BUCKETINDX(size);
|
|
|
|
kbp = &bucket[indx];
|
2000-06-14 18:31:42 +00:00
|
|
|
s = splmem();
|
2000-09-11 02:32:30 +00:00
|
|
|
mtx_enter(&malloc_mtx, MTX_DEF);
|
1994-05-24 10:09:53 +00:00
|
|
|
while (ksp->ks_memuse >= ksp->ks_limit) {
|
1999-01-21 08:29:12 +00:00
|
|
|
if (flags & M_ASLEEP) {
|
|
|
|
if (ksp->ks_limblocks < 65535)
|
|
|
|
ksp->ks_limblocks++;
|
|
|
|
asleep((caddr_t)ksp, PSWP+2, type->ks_shortdesc, 0);
|
|
|
|
}
|
1994-05-24 10:09:53 +00:00
|
|
|
if (flags & M_NOWAIT) {
|
|
|
|
splx(s);
|
2000-09-11 02:32:30 +00:00
|
|
|
mtx_exit(&malloc_mtx, MTX_DEF);
|
1994-05-24 10:09:53 +00:00
|
|
|
return ((void *) NULL);
|
|
|
|
}
|
|
|
|
if (ksp->ks_limblocks < 65535)
|
|
|
|
ksp->ks_limblocks++;
|
2000-09-11 02:32:30 +00:00
|
|
|
msleep((caddr_t)ksp, &malloc_mtx, PSWP+2, type->ks_shortdesc,
|
|
|
|
0);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
ksp->ks_size |= 1 << indx;
|
1999-01-08 17:31:30 +00:00
|
|
|
#ifdef INVARIANTS
|
1994-05-24 10:09:53 +00:00
|
|
|
copysize = 1 << indx < MAX_COPY ? 1 << indx : MAX_COPY;
|
|
|
|
#endif
|
|
|
|
if (kbp->kb_next == NULL) {
|
|
|
|
kbp->kb_last = NULL;
|
|
|
|
if (size > MAXALLOCSAVE)
|
1996-05-02 10:43:17 +00:00
|
|
|
allocsize = roundup(size, PAGE_SIZE);
|
1994-05-24 10:09:53 +00:00
|
|
|
else
|
|
|
|
allocsize = 1 << indx;
|
1996-05-02 14:21:14 +00:00
|
|
|
npg = btoc(allocsize);
|
2000-09-11 02:32:30 +00:00
|
|
|
|
|
|
|
mtx_exit(&malloc_mtx, MTX_DEF);
|
|
|
|
mtx_enter(&Giant, MTX_DEF);
|
1995-02-02 08:49:08 +00:00
|
|
|
va = (caddr_t) kmem_malloc(kmem_map, (vm_size_t)ctob(npg), flags);
|
2000-09-11 02:32:30 +00:00
|
|
|
mtx_exit(&Giant, MTX_DEF);
|
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
if (va == NULL) {
|
|
|
|
splx(s);
|
|
|
|
return ((void *) NULL);
|
|
|
|
}
|
2000-09-11 02:32:30 +00:00
|
|
|
/*
|
|
|
|
* Enter malloc_mtx after the error check to avoid having to
|
|
|
|
* immediately exit it again if there is an error.
|
|
|
|
*/
|
|
|
|
mtx_enter(&malloc_mtx, MTX_DEF);
|
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
kbp->kb_total += kbp->kb_elmpercl;
|
|
|
|
kup = btokup(va);
|
|
|
|
kup->ku_indx = indx;
|
|
|
|
if (allocsize > MAXALLOCSAVE) {
|
|
|
|
if (npg > 65535)
|
|
|
|
panic("malloc: allocation too large");
|
|
|
|
kup->ku_pagecnt = npg;
|
|
|
|
ksp->ks_memuse += allocsize;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
kup->ku_freecnt = kbp->kb_elmpercl;
|
|
|
|
kbp->kb_totalfree += kbp->kb_elmpercl;
|
|
|
|
/*
|
|
|
|
* Just in case we blocked while allocating memory,
|
|
|
|
* and someone else also allocated memory for this
|
|
|
|
* bucket, don't assume the list is still empty.
|
|
|
|
*/
|
|
|
|
savedlist = kbp->kb_next;
|
1996-05-02 14:21:14 +00:00
|
|
|
kbp->kb_next = cp = va + (npg * PAGE_SIZE) - allocsize;
|
1994-05-24 10:09:53 +00:00
|
|
|
for (;;) {
|
|
|
|
freep = (struct freelist *)cp;
|
1999-01-08 17:31:30 +00:00
|
|
|
#ifdef INVARIANTS
|
1994-05-24 10:09:53 +00:00
|
|
|
/*
|
|
|
|
* Copy in known text to detect modification
|
|
|
|
* after freeing.
|
|
|
|
*/
|
|
|
|
end = (long *)&cp[copysize];
|
|
|
|
for (lp = (long *)cp; lp < end; lp++)
|
|
|
|
*lp = WEIRD_ADDR;
|
|
|
|
freep->type = M_FREE;
|
1999-01-08 17:31:30 +00:00
|
|
|
#endif /* INVARIANTS */
|
1994-05-24 10:09:53 +00:00
|
|
|
if (cp <= va)
|
|
|
|
break;
|
|
|
|
cp -= allocsize;
|
|
|
|
freep->next = cp;
|
|
|
|
}
|
|
|
|
freep->next = savedlist;
|
|
|
|
if (kbp->kb_last == NULL)
|
|
|
|
kbp->kb_last = (caddr_t)freep;
|
|
|
|
}
|
|
|
|
va = kbp->kb_next;
|
|
|
|
kbp->kb_next = ((struct freelist *)va)->next;
|
1999-01-08 17:31:30 +00:00
|
|
|
#ifdef INVARIANTS
|
1994-05-24 10:09:53 +00:00
|
|
|
freep = (struct freelist *)va;
|
2000-03-14 14:58:04 +00:00
|
|
|
savedtype = (const char *) freep->type->ks_shortdesc;
|
1994-05-24 10:09:53 +00:00
|
|
|
#if BYTE_ORDER == BIG_ENDIAN
|
1997-10-10 18:14:23 +00:00
|
|
|
freep->type = (struct malloc_type *)WEIRD_ADDR >> 16;
|
1994-05-24 10:09:53 +00:00
|
|
|
#endif
|
|
|
|
#if BYTE_ORDER == LITTLE_ENDIAN
|
1997-10-10 18:14:23 +00:00
|
|
|
freep->type = (struct malloc_type *)WEIRD_ADDR;
|
1994-05-24 10:09:53 +00:00
|
|
|
#endif
|
1998-08-16 01:21:52 +00:00
|
|
|
if ((intptr_t)(void *)&freep->next & 0x2)
|
1994-05-24 10:09:53 +00:00
|
|
|
freep->next = (caddr_t)((WEIRD_ADDR >> 16)|(WEIRD_ADDR << 16));
|
|
|
|
else
|
|
|
|
freep->next = (caddr_t)WEIRD_ADDR;
|
|
|
|
end = (long *)&va[copysize];
|
|
|
|
for (lp = (long *)va; lp < end; lp++) {
|
|
|
|
if (*lp == WEIRD_ADDR)
|
|
|
|
continue;
|
1998-07-29 17:38:14 +00:00
|
|
|
printf("%s %ld of object %p size %lu %s %s (0x%lx != 0x%lx)\n",
|
|
|
|
"Data modified on freelist: word",
|
|
|
|
(long)(lp - (long *)va), (void *)va, size,
|
|
|
|
"previous type", savedtype, *lp, (u_long)WEIRD_ADDR);
|
1994-05-24 10:09:53 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
freep->spare0 = 0;
|
1999-01-08 17:31:30 +00:00
|
|
|
#endif /* INVARIANTS */
|
1994-05-24 10:09:53 +00:00
|
|
|
kup = btokup(va);
|
|
|
|
if (kup->ku_indx != indx)
|
|
|
|
panic("malloc: wrong bucket");
|
|
|
|
if (kup->ku_freecnt == 0)
|
|
|
|
panic("malloc: lost data");
|
|
|
|
kup->ku_freecnt--;
|
|
|
|
kbp->kb_totalfree--;
|
|
|
|
ksp->ks_memuse += 1 << indx;
|
|
|
|
out:
|
|
|
|
kbp->kb_calls++;
|
|
|
|
ksp->ks_inuse++;
|
|
|
|
ksp->ks_calls++;
|
|
|
|
if (ksp->ks_memuse > ksp->ks_maxused)
|
|
|
|
ksp->ks_maxused = ksp->ks_memuse;
|
|
|
|
splx(s);
|
2000-09-11 02:32:30 +00:00
|
|
|
mtx_exit(&malloc_mtx, MTX_DEF);
|
2000-10-20 17:54:55 +00:00
|
|
|
/* XXX: Do idle pre-zeroing. */
|
|
|
|
if (va != NULL && (flags & M_ZERO))
|
|
|
|
bzero(va, size);
|
1994-05-24 10:09:53 +00:00
|
|
|
return ((void *) va);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1999-01-21 08:29:12 +00:00
|
|
|
* free:
|
|
|
|
*
|
|
|
|
* Free a block of memory allocated by malloc.
|
|
|
|
*
|
|
|
|
* This routine may not block.
|
1994-05-24 10:09:53 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
free(addr, type)
|
|
|
|
void *addr;
|
1997-10-10 18:14:23 +00:00
|
|
|
struct malloc_type *type;
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
|
|
|
register struct kmembuckets *kbp;
|
|
|
|
register struct kmemusage *kup;
|
|
|
|
register struct freelist *freep;
|
|
|
|
long size;
|
|
|
|
int s;
|
1999-01-08 17:31:30 +00:00
|
|
|
#ifdef INVARIANTS
|
1996-08-04 20:08:48 +00:00
|
|
|
struct freelist *fp;
|
1994-05-24 10:09:53 +00:00
|
|
|
long *end, *lp, alloc, copysize;
|
|
|
|
#endif
|
1997-10-10 18:14:23 +00:00
|
|
|
register struct malloc_type *ksp = type;
|
1997-10-10 14:06:34 +00:00
|
|
|
|
1999-01-08 17:31:30 +00:00
|
|
|
KASSERT(kmembase <= (char *)addr && (char *)addr < kmemlimit,
|
1999-01-10 01:58:29 +00:00
|
|
|
("free: address %p out of range", (void *)addr));
|
1994-05-24 10:09:53 +00:00
|
|
|
kup = btokup(addr);
|
|
|
|
size = 1 << kup->ku_indx;
|
|
|
|
kbp = &bucket[kup->ku_indx];
|
1998-03-08 09:59:44 +00:00
|
|
|
s = splmem();
|
2000-09-11 02:32:30 +00:00
|
|
|
mtx_enter(&malloc_mtx, MTX_DEF);
|
1999-01-08 17:31:30 +00:00
|
|
|
#ifdef INVARIANTS
|
1994-05-24 10:09:53 +00:00
|
|
|
/*
|
|
|
|
* Check for returns of data that do not point to the
|
|
|
|
* beginning of the allocation.
|
|
|
|
*/
|
1996-05-02 10:43:17 +00:00
|
|
|
if (size > PAGE_SIZE)
|
|
|
|
alloc = addrmask[BUCKETINDX(PAGE_SIZE)];
|
1994-05-24 10:09:53 +00:00
|
|
|
else
|
|
|
|
alloc = addrmask[kup->ku_indx];
|
1998-08-16 01:21:52 +00:00
|
|
|
if (((uintptr_t)(void *)addr & alloc) != 0)
|
1998-07-29 17:38:14 +00:00
|
|
|
panic("free: unaligned addr %p, size %ld, type %s, mask %ld",
|
|
|
|
(void *)addr, size, type->ks_shortdesc, alloc);
|
1999-01-08 17:31:30 +00:00
|
|
|
#endif /* INVARIANTS */
|
1994-05-24 10:09:53 +00:00
|
|
|
if (size > MAXALLOCSAVE) {
|
2000-09-11 02:32:30 +00:00
|
|
|
mtx_exit(&malloc_mtx, MTX_DEF);
|
|
|
|
mtx_enter(&Giant, MTX_DEF);
|
1994-05-24 10:09:53 +00:00
|
|
|
kmem_free(kmem_map, (vm_offset_t)addr, ctob(kup->ku_pagecnt));
|
2000-09-11 02:32:30 +00:00
|
|
|
mtx_exit(&Giant, MTX_DEF);
|
|
|
|
mtx_enter(&malloc_mtx, MTX_DEF);
|
|
|
|
|
1996-05-02 14:21:14 +00:00
|
|
|
size = kup->ku_pagecnt << PAGE_SHIFT;
|
1994-05-24 10:09:53 +00:00
|
|
|
ksp->ks_memuse -= size;
|
|
|
|
kup->ku_indx = 0;
|
|
|
|
kup->ku_pagecnt = 0;
|
|
|
|
if (ksp->ks_memuse + size >= ksp->ks_limit &&
|
|
|
|
ksp->ks_memuse < ksp->ks_limit)
|
|
|
|
wakeup((caddr_t)ksp);
|
|
|
|
ksp->ks_inuse--;
|
|
|
|
kbp->kb_total -= 1;
|
|
|
|
splx(s);
|
2000-09-11 02:32:30 +00:00
|
|
|
mtx_exit(&malloc_mtx, MTX_DEF);
|
1994-05-24 10:09:53 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
freep = (struct freelist *)addr;
|
1999-01-08 17:31:30 +00:00
|
|
|
#ifdef INVARIANTS
|
1994-05-24 10:09:53 +00:00
|
|
|
/*
|
|
|
|
* Check for multiple frees. Use a quick check to see if
|
|
|
|
* it looks free before laboriously searching the freelist.
|
|
|
|
*/
|
|
|
|
if (freep->spare0 == WEIRD_ADDR) {
|
1996-08-04 20:08:48 +00:00
|
|
|
fp = (struct freelist *)kbp->kb_next;
|
|
|
|
while (fp) {
|
1999-01-10 01:58:29 +00:00
|
|
|
if (fp->spare0 != WEIRD_ADDR)
|
1999-01-08 17:31:30 +00:00
|
|
|
panic("free: free item %p modified", fp);
|
1999-01-10 01:58:29 +00:00
|
|
|
else if (addr == (caddr_t)fp)
|
1999-01-08 17:31:30 +00:00
|
|
|
panic("free: multiple freed item %p", addr);
|
1996-08-04 20:08:48 +00:00
|
|
|
fp = (struct freelist *)fp->next;
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Copy in known text to detect modification after freeing
|
|
|
|
* and to make it look free. Also, save the type being freed
|
|
|
|
* so we can list likely culprit if modification is detected
|
|
|
|
* when the object is reallocated.
|
|
|
|
*/
|
|
|
|
copysize = size < MAX_COPY ? size : MAX_COPY;
|
|
|
|
end = (long *)&((caddr_t)addr)[copysize];
|
|
|
|
for (lp = (long *)addr; lp < end; lp++)
|
|
|
|
*lp = WEIRD_ADDR;
|
|
|
|
freep->type = type;
|
1999-01-08 17:31:30 +00:00
|
|
|
#endif /* INVARIANTS */
|
1994-05-24 10:09:53 +00:00
|
|
|
kup->ku_freecnt++;
|
1999-05-06 18:13:11 +00:00
|
|
|
if (kup->ku_freecnt >= kbp->kb_elmpercl) {
|
1994-05-24 10:09:53 +00:00
|
|
|
if (kup->ku_freecnt > kbp->kb_elmpercl)
|
|
|
|
panic("free: multiple frees");
|
|
|
|
else if (kbp->kb_totalfree > kbp->kb_highwat)
|
|
|
|
kbp->kb_couldfree++;
|
1999-05-06 18:13:11 +00:00
|
|
|
}
|
1994-05-24 10:09:53 +00:00
|
|
|
kbp->kb_totalfree++;
|
|
|
|
ksp->ks_memuse -= size;
|
|
|
|
if (ksp->ks_memuse + size >= ksp->ks_limit &&
|
|
|
|
ksp->ks_memuse < ksp->ks_limit)
|
|
|
|
wakeup((caddr_t)ksp);
|
|
|
|
ksp->ks_inuse--;
|
1996-05-18 22:33:13 +00:00
|
|
|
#ifdef OLD_MALLOC_MEMORY_POLICY
|
1994-05-24 10:09:53 +00:00
|
|
|
if (kbp->kb_next == NULL)
|
|
|
|
kbp->kb_next = addr;
|
|
|
|
else
|
|
|
|
((struct freelist *)kbp->kb_last)->next = addr;
|
|
|
|
freep->next = NULL;
|
|
|
|
kbp->kb_last = addr;
|
1996-05-18 22:33:13 +00:00
|
|
|
#else
|
|
|
|
/*
|
|
|
|
* Return memory to the head of the queue for quick reuse. This
|
|
|
|
* can improve performance by improving the probability of the
|
|
|
|
* item being in the cache when it is reused.
|
|
|
|
*/
|
|
|
|
if (kbp->kb_next == NULL) {
|
|
|
|
kbp->kb_next = addr;
|
|
|
|
kbp->kb_last = addr;
|
|
|
|
freep->next = NULL;
|
|
|
|
} else {
|
|
|
|
freep->next = kbp->kb_next;
|
|
|
|
kbp->kb_next = addr;
|
|
|
|
}
|
|
|
|
#endif
|
1994-05-24 10:09:53 +00:00
|
|
|
splx(s);
|
2000-09-11 02:32:30 +00:00
|
|
|
mtx_exit(&malloc_mtx, MTX_DEF);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize the kernel memory allocator
|
|
|
|
*/
|
1995-08-28 09:19:25 +00:00
|
|
|
/* ARGSUSED*/
|
|
|
|
static void
|
1995-12-02 17:11:20 +00:00
|
|
|
kmeminit(dummy)
|
|
|
|
void *dummy;
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
|
|
|
register long indx;
|
2000-01-28 04:04:58 +00:00
|
|
|
u_long npg;
|
|
|
|
u_long mem_size;
|
|
|
|
u_long xvm_kmem_size;
|
1994-05-24 10:09:53 +00:00
|
|
|
|
|
|
|
#if ((MAXALLOCSAVE & (MAXALLOCSAVE - 1)) != 0)
|
1996-05-10 19:28:55 +00:00
|
|
|
#error "kmeminit: MAXALLOCSAVE not power of 2"
|
1994-05-24 10:09:53 +00:00
|
|
|
#endif
|
|
|
|
#if (MAXALLOCSAVE > MINALLOCSIZE * 32768)
|
1996-05-10 19:28:55 +00:00
|
|
|
#error "kmeminit: MAXALLOCSAVE too big"
|
1994-05-24 10:09:53 +00:00
|
|
|
#endif
|
1996-05-02 10:43:17 +00:00
|
|
|
#if (MAXALLOCSAVE < PAGE_SIZE)
|
1996-05-10 19:28:55 +00:00
|
|
|
#error "kmeminit: MAXALLOCSAVE too small"
|
1994-05-24 10:09:53 +00:00
|
|
|
#endif
|
1998-02-23 07:41:23 +00:00
|
|
|
|
2000-10-20 07:29:16 +00:00
|
|
|
mtx_init(&malloc_mtx, "malloc", MTX_DEF | MTX_COLD);
|
2000-09-11 02:32:30 +00:00
|
|
|
|
1998-02-23 07:41:23 +00:00
|
|
|
/*
|
|
|
|
* Try to auto-tune the kernel memory size, so that it is
|
|
|
|
* more applicable for a wider range of machine sizes.
|
|
|
|
* On an X86, a VM_KMEM_SIZE_SCALE value of 4 is good, while
|
|
|
|
* a VM_KMEM_SIZE of 12MB is a fair compromise. The
|
|
|
|
* VM_KMEM_SIZE_MAX is dependent on the maximum KVA space
|
|
|
|
* available, and on an X86 with a total KVA space of 256MB,
|
|
|
|
* try to keep VM_KMEM_SIZE_MAX at 80MB or below.
|
|
|
|
*
|
|
|
|
* Note that the kmem_map is also used by the zone allocator,
|
|
|
|
* so make sure that there is enough space.
|
|
|
|
*/
|
1999-07-05 08:52:54 +00:00
|
|
|
xvm_kmem_size = VM_KMEM_SIZE;
|
1998-02-23 07:41:23 +00:00
|
|
|
mem_size = cnt.v_page_count * PAGE_SIZE;
|
|
|
|
|
|
|
|
#if defined(VM_KMEM_SIZE_SCALE)
|
1999-07-05 08:52:54 +00:00
|
|
|
if ((mem_size / VM_KMEM_SIZE_SCALE) > xvm_kmem_size)
|
|
|
|
xvm_kmem_size = mem_size / VM_KMEM_SIZE_SCALE;
|
1998-02-23 07:41:23 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(VM_KMEM_SIZE_MAX)
|
1999-07-05 08:52:54 +00:00
|
|
|
if (xvm_kmem_size >= VM_KMEM_SIZE_MAX)
|
|
|
|
xvm_kmem_size = VM_KMEM_SIZE_MAX;
|
1998-02-23 07:41:23 +00:00
|
|
|
#endif
|
|
|
|
|
1999-01-21 21:54:32 +00:00
|
|
|
/* Allow final override from the kernel environment */
|
1999-07-05 08:52:54 +00:00
|
|
|
TUNABLE_INT_FETCH("kern.vm.kmem.size", xvm_kmem_size, vm_kmem_size);
|
1999-01-21 21:54:32 +00:00
|
|
|
|
2000-01-28 04:04:58 +00:00
|
|
|
/*
|
|
|
|
* Limit kmem virtual size to twice the physical memory.
|
|
|
|
* This allows for kmem map sparseness, but limits the size
|
|
|
|
* to something sane. Be careful to not overflow the 32bit
|
|
|
|
* ints while doing the check.
|
|
|
|
*/
|
|
|
|
if ((vm_kmem_size / 2) > (cnt.v_page_count * PAGE_SIZE))
|
|
|
|
vm_kmem_size = 2 * cnt.v_page_count * PAGE_SIZE;
|
1998-02-23 07:41:23 +00:00
|
|
|
|
|
|
|
npg = (nmbufs * MSIZE + nmbclusters * MCLBYTES + vm_kmem_size)
|
1996-05-10 19:28:55 +00:00
|
|
|
/ PAGE_SIZE;
|
1995-05-30 08:16:23 +00:00
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
kmemusage = (struct kmemusage *) kmem_alloc(kernel_map,
|
|
|
|
(vm_size_t)(npg * sizeof(struct kmemusage)));
|
|
|
|
kmem_map = kmem_suballoc(kernel_map, (vm_offset_t *)&kmembase,
|
VM level code cleanups.
1) Start using TSM.
Struct procs continue to point to upages structure, after being freed.
Struct vmspace continues to point to pte object and kva space for kstack.
u_map is now superfluous.
2) vm_map's don't need to be reference counted. They always exist either
in the kernel or in a vmspace. The vmspaces are managed by reference
counts.
3) Remove the "wired" vm_map nonsense.
4) No need to keep a cache of kernel stack kva's.
5) Get rid of strange looking ++var, and change to var++.
6) Change more data structures to use our "zone" allocator. Added
struct proc, struct vmspace and struct vnode. This saves a significant
amount of kva space and physical memory. Additionally, this enables
TSM for the zone managed memory.
7) Keep ioopt disabled for now.
8) Remove the now bogus "single use" map concept.
9) Use generation counts or id's for data structures residing in TSM, where
it allows us to avoid unneeded restart overhead during traversals, where
blocking might occur.
10) Account better for memory deficits, so the pageout daemon will be able
to make enough memory available (experimental.)
11) Fix some vnode locking problems. (From Tor, I think.)
12) Add a check in ufs_lookup, to avoid lots of unneeded calls to bcmp.
(experimental.)
13) Significantly shrink, cleanup, and make slightly faster the vm_fault.c
code. Use generation counts, get rid of unneded collpase operations,
and clean up the cluster code.
14) Make vm_zone more suitable for TSM.
This commit is partially as a result of discussions and contributions from
other people, including DG, Tor Egge, PHK, and probably others that I
have forgotten to attribute (so let me know, if I forgot.)
This is not the infamous, final cleanup of the vnode stuff, but a necessary
step. Vnode mgmt should be correct, but things might still change, and
there is still some missing stuff (like ioopt, and physical backing of
non-merged cache files, debugging of layering concepts.)
1998-01-22 17:30:44 +00:00
|
|
|
(vm_offset_t *)&kmemlimit, (vm_size_t)(npg * PAGE_SIZE));
|
1997-08-05 00:02:08 +00:00
|
|
|
kmem_map->system_map = 1;
|
1994-05-24 10:09:53 +00:00
|
|
|
for (indx = 0; indx < MINBUCKET + 16; indx++) {
|
1996-05-02 10:43:17 +00:00
|
|
|
if (1 << indx >= PAGE_SIZE)
|
1994-05-24 10:09:53 +00:00
|
|
|
bucket[indx].kb_elmpercl = 1;
|
|
|
|
else
|
1996-05-02 10:43:17 +00:00
|
|
|
bucket[indx].kb_elmpercl = PAGE_SIZE / (1 << indx);
|
1994-05-24 10:09:53 +00:00
|
|
|
bucket[indx].kb_highwat = 5 * bucket[indx].kb_elmpercl;
|
|
|
|
}
|
1997-10-10 14:06:34 +00:00
|
|
|
}
|
|
|
|
|
1998-11-10 08:46:24 +00:00
|
|
|
void
|
|
|
|
malloc_init(data)
|
|
|
|
void *data;
|
1997-10-10 14:06:34 +00:00
|
|
|
{
|
1998-11-10 08:46:24 +00:00
|
|
|
struct malloc_type *type = (struct malloc_type *)data;
|
1997-10-10 14:06:34 +00:00
|
|
|
|
1999-11-22 02:45:11 +00:00
|
|
|
if (type->ks_magic != M_MAGIC)
|
1997-10-28 19:01:02 +00:00
|
|
|
panic("malloc type lacks magic");
|
|
|
|
|
1999-05-12 11:11:27 +00:00
|
|
|
if (type->ks_limit != 0)
|
1998-11-10 08:46:24 +00:00
|
|
|
return;
|
|
|
|
|
1997-12-05 05:36:58 +00:00
|
|
|
if (cnt.v_page_count == 0)
|
|
|
|
panic("malloc_init not allowed before vm init");
|
|
|
|
|
1996-01-29 11:12:37 +00:00
|
|
|
/*
|
1998-02-23 07:41:23 +00:00
|
|
|
* The default limits for each malloc region is 1/2 of the
|
|
|
|
* malloc portion of the kmem map size.
|
1996-01-29 11:12:37 +00:00
|
|
|
*/
|
1998-02-23 07:41:23 +00:00
|
|
|
type->ks_limit = vm_kmem_size / 2;
|
1997-10-10 14:06:34 +00:00
|
|
|
type->ks_next = kmemstatistics;
|
|
|
|
kmemstatistics = type;
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
1998-11-10 08:46:24 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
malloc_uninit(data)
|
|
|
|
void *data;
|
|
|
|
{
|
|
|
|
struct malloc_type *type = (struct malloc_type *)data;
|
|
|
|
struct malloc_type *t;
|
2000-06-29 09:26:26 +00:00
|
|
|
#ifdef INVARIANTS
|
2000-06-29 03:41:30 +00:00
|
|
|
struct kmembuckets *kbp;
|
|
|
|
struct freelist *freep;
|
|
|
|
long indx;
|
|
|
|
int s;
|
|
|
|
#endif
|
1998-11-10 08:46:24 +00:00
|
|
|
|
1999-11-22 02:45:11 +00:00
|
|
|
if (type->ks_magic != M_MAGIC)
|
1998-11-10 08:46:24 +00:00
|
|
|
panic("malloc type lacks magic");
|
|
|
|
|
|
|
|
if (cnt.v_page_count == 0)
|
|
|
|
panic("malloc_uninit not allowed before vm init");
|
|
|
|
|
1999-05-12 11:11:27 +00:00
|
|
|
if (type->ks_limit == 0)
|
|
|
|
panic("malloc_uninit on uninitialized type");
|
|
|
|
|
2000-06-29 03:41:30 +00:00
|
|
|
#ifdef INVARIANTS
|
|
|
|
s = splmem();
|
2000-09-11 02:32:30 +00:00
|
|
|
mtx_enter(&malloc_mtx, MTX_DEF);
|
2000-06-29 03:41:30 +00:00
|
|
|
for (indx = 0; indx < MINBUCKET + 16; indx++) {
|
|
|
|
kbp = bucket + indx;
|
|
|
|
freep = (struct freelist*)kbp->kb_next;
|
|
|
|
while (freep) {
|
|
|
|
if (freep->type == type)
|
|
|
|
freep->type = M_FREE;
|
|
|
|
freep = (struct freelist*)freep->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
splx(s);
|
2000-09-11 02:32:30 +00:00
|
|
|
mtx_exit(&malloc_mtx, MTX_DEF);
|
2000-06-29 03:41:30 +00:00
|
|
|
|
|
|
|
if (type->ks_memuse != 0)
|
|
|
|
printf("malloc_uninit: %ld bytes of '%s' still allocated\n",
|
|
|
|
type->ks_memuse, type->ks_shortdesc);
|
|
|
|
#endif
|
|
|
|
|
1998-11-10 08:46:24 +00:00
|
|
|
if (type == kmemstatistics)
|
|
|
|
kmemstatistics = type->ks_next;
|
|
|
|
else {
|
|
|
|
for (t = kmemstatistics; t->ks_next != NULL; t = t->ks_next) {
|
|
|
|
if (t->ks_next == type) {
|
|
|
|
t->ks_next = type->ks_next;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1999-05-12 11:11:27 +00:00
|
|
|
type->ks_next = NULL;
|
|
|
|
type->ks_limit = 0;
|
1998-11-10 08:46:24 +00:00
|
|
|
}
|