Deleted Added
full compact
kern_malloc.c (71859) kern_malloc.c (72200)
1/*
2 * Copyright (c) 1987, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)kern_malloc.c 8.3 (Berkeley) 1/4/94
1/*
2 * Copyright (c) 1987, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)kern_malloc.c 8.3 (Berkeley) 1/4/94
34 * $FreeBSD: head/sys/kern/kern_malloc.c 71859 2001-01-31 04:50:20Z bp $
34 * $FreeBSD: head/sys/kern/kern_malloc.c 72200 2001-02-09 06:11:45Z bmilekic $
35 */
36
37#include "opt_vm.h"
38
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/kernel.h>
42#include <sys/malloc.h>
43#include <sys/mbuf.h>
44#include <sys/mutex.h>
45#include <sys/vmmeter.h>
46#include <sys/lock.h>
47#include <sys/proc.h>
48
49#include <vm/vm.h>
50#include <vm/vm_param.h>
51#include <vm/vm_kern.h>
52#include <vm/vm_extern.h>
53#include <vm/pmap.h>
54#include <vm/vm_map.h>
55
56#if defined(INVARIANTS) && defined(__i386__)
57#include <machine/cpu.h>
58#endif
59
60MALLOC_DEFINE(M_CACHE, "cache", "Various Dynamically allocated caches");
61MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory");
62MALLOC_DEFINE(M_TEMP, "temp", "misc temporary data buffers");
63
64MALLOC_DEFINE(M_IP6OPT, "ip6opt", "IPv6 options");
65MALLOC_DEFINE(M_IP6NDP, "ip6ndp", "IPv6 Neighbor Discovery");
66
67static void kmeminit __P((void *));
68SYSINIT(kmem, SI_SUB_KMEM, SI_ORDER_FIRST, kmeminit, NULL)
69
70static MALLOC_DEFINE(M_FREE, "free", "should be on free list");
71
72static struct malloc_type *kmemstatistics;
73static struct kmembuckets bucket[MINBUCKET + 16];
74static struct kmemusage *kmemusage;
75static char *kmembase;
76static char *kmemlimit;
77
78static struct mtx malloc_mtx;
79
80u_int vm_kmem_size;
81
82#ifdef INVARIANTS
83/*
84 * This structure provides a set of masks to catch unaligned frees.
85 */
86static long addrmask[] = { 0,
87 0x00000001, 0x00000003, 0x00000007, 0x0000000f,
88 0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff,
89 0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff,
90 0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff,
91};
92
93/*
94 * The WEIRD_ADDR is used as known text to copy into free objects so
95 * that modifications after frees can be detected.
96 */
97#define WEIRD_ADDR 0xdeadc0de
98#define MAX_COPY 64
99
100/*
101 * Normally the first word of the structure is used to hold the list
102 * pointer for free objects. However, when running with diagnostics,
103 * we use the third and fourth fields, so as to catch modifications
104 * in the most commonly trashed first two words.
105 */
106struct freelist {
107 long spare0;
108 struct malloc_type *type;
109 long spare1;
110 caddr_t next;
111};
112#else /* !INVARIANTS */
113struct freelist {
114 caddr_t next;
115};
116#endif /* INVARIANTS */
117
118/*
119 * malloc:
120 *
121 * Allocate a block of memory.
122 *
123 * If M_NOWAIT is set, this routine will not block and return NULL if
124 * the allocation fails.
125 *
126 * If M_ASLEEP is set (M_NOWAIT must also be set), this routine
127 * will have the side effect of calling asleep() if it returns NULL,
128 * allowing the parent to await() at some future time.
129 */
130void *
131malloc(size, type, flags)
132 unsigned long size;
133 struct malloc_type *type;
134 int flags;
135{
136 register struct kmembuckets *kbp;
137 register struct kmemusage *kup;
138 register struct freelist *freep;
139 long indx, npg, allocsize;
140 int s;
141 caddr_t va, cp, savedlist;
142#ifdef INVARIANTS
143 long *end, *lp;
144 int copysize;
145 const char *savedtype;
146#endif
147 register struct malloc_type *ksp = type;
148
149#if defined(INVARIANTS)
150 if (flags == M_WAITOK)
151 KASSERT(curproc->p_intr_nesting_level == 0,
152 ("malloc(M_WAITOK) in interrupt context"));
153#endif
154 indx = BUCKETINDX(size);
155 kbp = &bucket[indx];
156 s = splmem();
35 */
36
37#include "opt_vm.h"
38
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/kernel.h>
42#include <sys/malloc.h>
43#include <sys/mbuf.h>
44#include <sys/mutex.h>
45#include <sys/vmmeter.h>
46#include <sys/lock.h>
47#include <sys/proc.h>
48
49#include <vm/vm.h>
50#include <vm/vm_param.h>
51#include <vm/vm_kern.h>
52#include <vm/vm_extern.h>
53#include <vm/pmap.h>
54#include <vm/vm_map.h>
55
56#if defined(INVARIANTS) && defined(__i386__)
57#include <machine/cpu.h>
58#endif
59
60MALLOC_DEFINE(M_CACHE, "cache", "Various Dynamically allocated caches");
61MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory");
62MALLOC_DEFINE(M_TEMP, "temp", "misc temporary data buffers");
63
64MALLOC_DEFINE(M_IP6OPT, "ip6opt", "IPv6 options");
65MALLOC_DEFINE(M_IP6NDP, "ip6ndp", "IPv6 Neighbor Discovery");
66
67static void kmeminit __P((void *));
68SYSINIT(kmem, SI_SUB_KMEM, SI_ORDER_FIRST, kmeminit, NULL)
69
70static MALLOC_DEFINE(M_FREE, "free", "should be on free list");
71
72static struct malloc_type *kmemstatistics;
73static struct kmembuckets bucket[MINBUCKET + 16];
74static struct kmemusage *kmemusage;
75static char *kmembase;
76static char *kmemlimit;
77
78static struct mtx malloc_mtx;
79
80u_int vm_kmem_size;
81
82#ifdef INVARIANTS
83/*
84 * This structure provides a set of masks to catch unaligned frees.
85 */
86static long addrmask[] = { 0,
87 0x00000001, 0x00000003, 0x00000007, 0x0000000f,
88 0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff,
89 0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff,
90 0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff,
91};
92
93/*
94 * The WEIRD_ADDR is used as known text to copy into free objects so
95 * that modifications after frees can be detected.
96 */
97#define WEIRD_ADDR 0xdeadc0de
98#define MAX_COPY 64
99
100/*
101 * Normally the first word of the structure is used to hold the list
102 * pointer for free objects. However, when running with diagnostics,
103 * we use the third and fourth fields, so as to catch modifications
104 * in the most commonly trashed first two words.
105 */
106struct freelist {
107 long spare0;
108 struct malloc_type *type;
109 long spare1;
110 caddr_t next;
111};
112#else /* !INVARIANTS */
113struct freelist {
114 caddr_t next;
115};
116#endif /* INVARIANTS */
117
118/*
119 * malloc:
120 *
121 * Allocate a block of memory.
122 *
123 * If M_NOWAIT is set, this routine will not block and return NULL if
124 * the allocation fails.
125 *
126 * If M_ASLEEP is set (M_NOWAIT must also be set), this routine
127 * will have the side effect of calling asleep() if it returns NULL,
128 * allowing the parent to await() at some future time.
129 */
130void *
131malloc(size, type, flags)
132 unsigned long size;
133 struct malloc_type *type;
134 int flags;
135{
136 register struct kmembuckets *kbp;
137 register struct kmemusage *kup;
138 register struct freelist *freep;
139 long indx, npg, allocsize;
140 int s;
141 caddr_t va, cp, savedlist;
142#ifdef INVARIANTS
143 long *end, *lp;
144 int copysize;
145 const char *savedtype;
146#endif
147 register struct malloc_type *ksp = type;
148
149#if defined(INVARIANTS)
150 if (flags == M_WAITOK)
151 KASSERT(curproc->p_intr_nesting_level == 0,
152 ("malloc(M_WAITOK) in interrupt context"));
153#endif
154 indx = BUCKETINDX(size);
155 kbp = &bucket[indx];
156 s = splmem();
157 mtx_enter(&malloc_mtx, MTX_DEF);
157 mtx_lock(&malloc_mtx);
158 while (ksp->ks_memuse >= ksp->ks_limit) {
159 if (flags & M_ASLEEP) {
160 if (ksp->ks_limblocks < 65535)
161 ksp->ks_limblocks++;
162 asleep((caddr_t)ksp, PSWP+2, type->ks_shortdesc, 0);
163 }
164 if (flags & M_NOWAIT) {
165 splx(s);
158 while (ksp->ks_memuse >= ksp->ks_limit) {
159 if (flags & M_ASLEEP) {
160 if (ksp->ks_limblocks < 65535)
161 ksp->ks_limblocks++;
162 asleep((caddr_t)ksp, PSWP+2, type->ks_shortdesc, 0);
163 }
164 if (flags & M_NOWAIT) {
165 splx(s);
166 mtx_exit(&malloc_mtx, MTX_DEF);
166 mtx_unlock(&malloc_mtx);
167 return ((void *) NULL);
168 }
169 if (ksp->ks_limblocks < 65535)
170 ksp->ks_limblocks++;
171 msleep((caddr_t)ksp, &malloc_mtx, PSWP+2, type->ks_shortdesc,
172 0);
173 }
174 ksp->ks_size |= 1 << indx;
175#ifdef INVARIANTS
176 copysize = 1 << indx < MAX_COPY ? 1 << indx : MAX_COPY;
177#endif
178 if (kbp->kb_next == NULL) {
179 kbp->kb_last = NULL;
180 if (size > MAXALLOCSAVE)
181 allocsize = roundup(size, PAGE_SIZE);
182 else
183 allocsize = 1 << indx;
184 npg = btoc(allocsize);
185
167 return ((void *) NULL);
168 }
169 if (ksp->ks_limblocks < 65535)
170 ksp->ks_limblocks++;
171 msleep((caddr_t)ksp, &malloc_mtx, PSWP+2, type->ks_shortdesc,
172 0);
173 }
174 ksp->ks_size |= 1 << indx;
175#ifdef INVARIANTS
176 copysize = 1 << indx < MAX_COPY ? 1 << indx : MAX_COPY;
177#endif
178 if (kbp->kb_next == NULL) {
179 kbp->kb_last = NULL;
180 if (size > MAXALLOCSAVE)
181 allocsize = roundup(size, PAGE_SIZE);
182 else
183 allocsize = 1 << indx;
184 npg = btoc(allocsize);
185
186 mtx_exit(&malloc_mtx, MTX_DEF);
186 mtx_unlock(&malloc_mtx);
187 va = (caddr_t) kmem_malloc(kmem_map, (vm_size_t)ctob(npg), flags);
188
189 if (va == NULL) {
190 splx(s);
191 return ((void *) NULL);
192 }
193 /*
194 * Enter malloc_mtx after the error check to avoid having to
195 * immediately exit it again if there is an error.
196 */
187 va = (caddr_t) kmem_malloc(kmem_map, (vm_size_t)ctob(npg), flags);
188
189 if (va == NULL) {
190 splx(s);
191 return ((void *) NULL);
192 }
193 /*
194 * Enter malloc_mtx after the error check to avoid having to
195 * immediately exit it again if there is an error.
196 */
197 mtx_enter(&malloc_mtx, MTX_DEF);
197 mtx_lock(&malloc_mtx);
198
199 kbp->kb_total += kbp->kb_elmpercl;
200 kup = btokup(va);
201 kup->ku_indx = indx;
202 if (allocsize > MAXALLOCSAVE) {
203 if (npg > 65535)
204 panic("malloc: allocation too large");
205 kup->ku_pagecnt = npg;
206 ksp->ks_memuse += allocsize;
207 goto out;
208 }
209 kup->ku_freecnt = kbp->kb_elmpercl;
210 kbp->kb_totalfree += kbp->kb_elmpercl;
211 /*
212 * Just in case we blocked while allocating memory,
213 * and someone else also allocated memory for this
214 * bucket, don't assume the list is still empty.
215 */
216 savedlist = kbp->kb_next;
217 kbp->kb_next = cp = va + (npg * PAGE_SIZE) - allocsize;
218 for (;;) {
219 freep = (struct freelist *)cp;
220#ifdef INVARIANTS
221 /*
222 * Copy in known text to detect modification
223 * after freeing.
224 */
225 end = (long *)&cp[copysize];
226 for (lp = (long *)cp; lp < end; lp++)
227 *lp = WEIRD_ADDR;
228 freep->type = M_FREE;
229#endif /* INVARIANTS */
230 if (cp <= va)
231 break;
232 cp -= allocsize;
233 freep->next = cp;
234 }
235 freep->next = savedlist;
236 if (kbp->kb_last == NULL)
237 kbp->kb_last = (caddr_t)freep;
238 }
239 va = kbp->kb_next;
240 kbp->kb_next = ((struct freelist *)va)->next;
241#ifdef INVARIANTS
242 freep = (struct freelist *)va;
243 savedtype = (const char *) freep->type->ks_shortdesc;
244#if BYTE_ORDER == BIG_ENDIAN
245 freep->type = (struct malloc_type *)WEIRD_ADDR >> 16;
246#endif
247#if BYTE_ORDER == LITTLE_ENDIAN
248 freep->type = (struct malloc_type *)WEIRD_ADDR;
249#endif
250 if ((intptr_t)(void *)&freep->next & 0x2)
251 freep->next = (caddr_t)((WEIRD_ADDR >> 16)|(WEIRD_ADDR << 16));
252 else
253 freep->next = (caddr_t)WEIRD_ADDR;
254 end = (long *)&va[copysize];
255 for (lp = (long *)va; lp < end; lp++) {
256 if (*lp == WEIRD_ADDR)
257 continue;
258 printf("%s %ld of object %p size %lu %s %s (0x%lx != 0x%lx)\n",
259 "Data modified on freelist: word",
260 (long)(lp - (long *)va), (void *)va, size,
261 "previous type", savedtype, *lp, (u_long)WEIRD_ADDR);
262 break;
263 }
264 freep->spare0 = 0;
265#endif /* INVARIANTS */
266 kup = btokup(va);
267 if (kup->ku_indx != indx)
268 panic("malloc: wrong bucket");
269 if (kup->ku_freecnt == 0)
270 panic("malloc: lost data");
271 kup->ku_freecnt--;
272 kbp->kb_totalfree--;
273 ksp->ks_memuse += 1 << indx;
274out:
275 kbp->kb_calls++;
276 ksp->ks_inuse++;
277 ksp->ks_calls++;
278 if (ksp->ks_memuse > ksp->ks_maxused)
279 ksp->ks_maxused = ksp->ks_memuse;
280 splx(s);
198
199 kbp->kb_total += kbp->kb_elmpercl;
200 kup = btokup(va);
201 kup->ku_indx = indx;
202 if (allocsize > MAXALLOCSAVE) {
203 if (npg > 65535)
204 panic("malloc: allocation too large");
205 kup->ku_pagecnt = npg;
206 ksp->ks_memuse += allocsize;
207 goto out;
208 }
209 kup->ku_freecnt = kbp->kb_elmpercl;
210 kbp->kb_totalfree += kbp->kb_elmpercl;
211 /*
212 * Just in case we blocked while allocating memory,
213 * and someone else also allocated memory for this
214 * bucket, don't assume the list is still empty.
215 */
216 savedlist = kbp->kb_next;
217 kbp->kb_next = cp = va + (npg * PAGE_SIZE) - allocsize;
218 for (;;) {
219 freep = (struct freelist *)cp;
220#ifdef INVARIANTS
221 /*
222 * Copy in known text to detect modification
223 * after freeing.
224 */
225 end = (long *)&cp[copysize];
226 for (lp = (long *)cp; lp < end; lp++)
227 *lp = WEIRD_ADDR;
228 freep->type = M_FREE;
229#endif /* INVARIANTS */
230 if (cp <= va)
231 break;
232 cp -= allocsize;
233 freep->next = cp;
234 }
235 freep->next = savedlist;
236 if (kbp->kb_last == NULL)
237 kbp->kb_last = (caddr_t)freep;
238 }
239 va = kbp->kb_next;
240 kbp->kb_next = ((struct freelist *)va)->next;
241#ifdef INVARIANTS
242 freep = (struct freelist *)va;
243 savedtype = (const char *) freep->type->ks_shortdesc;
244#if BYTE_ORDER == BIG_ENDIAN
245 freep->type = (struct malloc_type *)WEIRD_ADDR >> 16;
246#endif
247#if BYTE_ORDER == LITTLE_ENDIAN
248 freep->type = (struct malloc_type *)WEIRD_ADDR;
249#endif
250 if ((intptr_t)(void *)&freep->next & 0x2)
251 freep->next = (caddr_t)((WEIRD_ADDR >> 16)|(WEIRD_ADDR << 16));
252 else
253 freep->next = (caddr_t)WEIRD_ADDR;
254 end = (long *)&va[copysize];
255 for (lp = (long *)va; lp < end; lp++) {
256 if (*lp == WEIRD_ADDR)
257 continue;
258 printf("%s %ld of object %p size %lu %s %s (0x%lx != 0x%lx)\n",
259 "Data modified on freelist: word",
260 (long)(lp - (long *)va), (void *)va, size,
261 "previous type", savedtype, *lp, (u_long)WEIRD_ADDR);
262 break;
263 }
264 freep->spare0 = 0;
265#endif /* INVARIANTS */
266 kup = btokup(va);
267 if (kup->ku_indx != indx)
268 panic("malloc: wrong bucket");
269 if (kup->ku_freecnt == 0)
270 panic("malloc: lost data");
271 kup->ku_freecnt--;
272 kbp->kb_totalfree--;
273 ksp->ks_memuse += 1 << indx;
274out:
275 kbp->kb_calls++;
276 ksp->ks_inuse++;
277 ksp->ks_calls++;
278 if (ksp->ks_memuse > ksp->ks_maxused)
279 ksp->ks_maxused = ksp->ks_memuse;
280 splx(s);
281 mtx_exit(&malloc_mtx, MTX_DEF);
281 mtx_unlock(&malloc_mtx);
282 /* XXX: Do idle pre-zeroing. */
283 if (va != NULL && (flags & M_ZERO))
284 bzero(va, size);
285 return ((void *) va);
286}
287
288/*
289 * free:
290 *
291 * Free a block of memory allocated by malloc.
292 *
293 * This routine may not block.
294 */
295void
296free(addr, type)
297 void *addr;
298 struct malloc_type *type;
299{
300 register struct kmembuckets *kbp;
301 register struct kmemusage *kup;
302 register struct freelist *freep;
303 long size;
304 int s;
305#ifdef INVARIANTS
306 struct freelist *fp;
307 long *end, *lp, alloc, copysize;
308#endif
309 register struct malloc_type *ksp = type;
310
311 KASSERT(kmembase <= (char *)addr && (char *)addr < kmemlimit,
312 ("free: address %p out of range", (void *)addr));
313 kup = btokup(addr);
314 size = 1 << kup->ku_indx;
315 kbp = &bucket[kup->ku_indx];
316 s = splmem();
282 /* XXX: Do idle pre-zeroing. */
283 if (va != NULL && (flags & M_ZERO))
284 bzero(va, size);
285 return ((void *) va);
286}
287
288/*
289 * free:
290 *
291 * Free a block of memory allocated by malloc.
292 *
293 * This routine may not block.
294 */
295void
296free(addr, type)
297 void *addr;
298 struct malloc_type *type;
299{
300 register struct kmembuckets *kbp;
301 register struct kmemusage *kup;
302 register struct freelist *freep;
303 long size;
304 int s;
305#ifdef INVARIANTS
306 struct freelist *fp;
307 long *end, *lp, alloc, copysize;
308#endif
309 register struct malloc_type *ksp = type;
310
311 KASSERT(kmembase <= (char *)addr && (char *)addr < kmemlimit,
312 ("free: address %p out of range", (void *)addr));
313 kup = btokup(addr);
314 size = 1 << kup->ku_indx;
315 kbp = &bucket[kup->ku_indx];
316 s = splmem();
317 mtx_enter(&malloc_mtx, MTX_DEF);
317 mtx_lock(&malloc_mtx);
318#ifdef INVARIANTS
319 /*
320 * Check for returns of data that do not point to the
321 * beginning of the allocation.
322 */
323 if (size > PAGE_SIZE)
324 alloc = addrmask[BUCKETINDX(PAGE_SIZE)];
325 else
326 alloc = addrmask[kup->ku_indx];
327 if (((uintptr_t)(void *)addr & alloc) != 0)
328 panic("free: unaligned addr %p, size %ld, type %s, mask %ld",
329 (void *)addr, size, type->ks_shortdesc, alloc);
330#endif /* INVARIANTS */
331 if (size > MAXALLOCSAVE) {
318#ifdef INVARIANTS
319 /*
320 * Check for returns of data that do not point to the
321 * beginning of the allocation.
322 */
323 if (size > PAGE_SIZE)
324 alloc = addrmask[BUCKETINDX(PAGE_SIZE)];
325 else
326 alloc = addrmask[kup->ku_indx];
327 if (((uintptr_t)(void *)addr & alloc) != 0)
328 panic("free: unaligned addr %p, size %ld, type %s, mask %ld",
329 (void *)addr, size, type->ks_shortdesc, alloc);
330#endif /* INVARIANTS */
331 if (size > MAXALLOCSAVE) {
332 mtx_exit(&malloc_mtx, MTX_DEF);
332 mtx_unlock(&malloc_mtx);
333 kmem_free(kmem_map, (vm_offset_t)addr, ctob(kup->ku_pagecnt));
333 kmem_free(kmem_map, (vm_offset_t)addr, ctob(kup->ku_pagecnt));
334 mtx_enter(&malloc_mtx, MTX_DEF);
334 mtx_lock(&malloc_mtx);
335
336 size = kup->ku_pagecnt << PAGE_SHIFT;
337 ksp->ks_memuse -= size;
338 kup->ku_indx = 0;
339 kup->ku_pagecnt = 0;
340 if (ksp->ks_memuse + size >= ksp->ks_limit &&
341 ksp->ks_memuse < ksp->ks_limit)
342 wakeup((caddr_t)ksp);
343 ksp->ks_inuse--;
344 kbp->kb_total -= 1;
345 splx(s);
335
336 size = kup->ku_pagecnt << PAGE_SHIFT;
337 ksp->ks_memuse -= size;
338 kup->ku_indx = 0;
339 kup->ku_pagecnt = 0;
340 if (ksp->ks_memuse + size >= ksp->ks_limit &&
341 ksp->ks_memuse < ksp->ks_limit)
342 wakeup((caddr_t)ksp);
343 ksp->ks_inuse--;
344 kbp->kb_total -= 1;
345 splx(s);
346 mtx_exit(&malloc_mtx, MTX_DEF);
346 mtx_unlock(&malloc_mtx);
347 return;
348 }
349 freep = (struct freelist *)addr;
350#ifdef INVARIANTS
351 /*
352 * Check for multiple frees. Use a quick check to see if
353 * it looks free before laboriously searching the freelist.
354 */
355 if (freep->spare0 == WEIRD_ADDR) {
356 fp = (struct freelist *)kbp->kb_next;
357 while (fp) {
358 if (fp->spare0 != WEIRD_ADDR)
359 panic("free: free item %p modified", fp);
360 else if (addr == (caddr_t)fp)
361 panic("free: multiple freed item %p", addr);
362 fp = (struct freelist *)fp->next;
363 }
364 }
365 /*
366 * Copy in known text to detect modification after freeing
367 * and to make it look free. Also, save the type being freed
368 * so we can list likely culprit if modification is detected
369 * when the object is reallocated.
370 */
371 copysize = size < MAX_COPY ? size : MAX_COPY;
372 end = (long *)&((caddr_t)addr)[copysize];
373 for (lp = (long *)addr; lp < end; lp++)
374 *lp = WEIRD_ADDR;
375 freep->type = type;
376#endif /* INVARIANTS */
377 kup->ku_freecnt++;
378 if (kup->ku_freecnt >= kbp->kb_elmpercl) {
379 if (kup->ku_freecnt > kbp->kb_elmpercl)
380 panic("free: multiple frees");
381 else if (kbp->kb_totalfree > kbp->kb_highwat)
382 kbp->kb_couldfree++;
383 }
384 kbp->kb_totalfree++;
385 ksp->ks_memuse -= size;
386 if (ksp->ks_memuse + size >= ksp->ks_limit &&
387 ksp->ks_memuse < ksp->ks_limit)
388 wakeup((caddr_t)ksp);
389 ksp->ks_inuse--;
390#ifdef OLD_MALLOC_MEMORY_POLICY
391 if (kbp->kb_next == NULL)
392 kbp->kb_next = addr;
393 else
394 ((struct freelist *)kbp->kb_last)->next = addr;
395 freep->next = NULL;
396 kbp->kb_last = addr;
397#else
398 /*
399 * Return memory to the head of the queue for quick reuse. This
400 * can improve performance by improving the probability of the
401 * item being in the cache when it is reused.
402 */
403 if (kbp->kb_next == NULL) {
404 kbp->kb_next = addr;
405 kbp->kb_last = addr;
406 freep->next = NULL;
407 } else {
408 freep->next = kbp->kb_next;
409 kbp->kb_next = addr;
410 }
411#endif
412 splx(s);
347 return;
348 }
349 freep = (struct freelist *)addr;
350#ifdef INVARIANTS
351 /*
352 * Check for multiple frees. Use a quick check to see if
353 * it looks free before laboriously searching the freelist.
354 */
355 if (freep->spare0 == WEIRD_ADDR) {
356 fp = (struct freelist *)kbp->kb_next;
357 while (fp) {
358 if (fp->spare0 != WEIRD_ADDR)
359 panic("free: free item %p modified", fp);
360 else if (addr == (caddr_t)fp)
361 panic("free: multiple freed item %p", addr);
362 fp = (struct freelist *)fp->next;
363 }
364 }
365 /*
366 * Copy in known text to detect modification after freeing
367 * and to make it look free. Also, save the type being freed
368 * so we can list likely culprit if modification is detected
369 * when the object is reallocated.
370 */
371 copysize = size < MAX_COPY ? size : MAX_COPY;
372 end = (long *)&((caddr_t)addr)[copysize];
373 for (lp = (long *)addr; lp < end; lp++)
374 *lp = WEIRD_ADDR;
375 freep->type = type;
376#endif /* INVARIANTS */
377 kup->ku_freecnt++;
378 if (kup->ku_freecnt >= kbp->kb_elmpercl) {
379 if (kup->ku_freecnt > kbp->kb_elmpercl)
380 panic("free: multiple frees");
381 else if (kbp->kb_totalfree > kbp->kb_highwat)
382 kbp->kb_couldfree++;
383 }
384 kbp->kb_totalfree++;
385 ksp->ks_memuse -= size;
386 if (ksp->ks_memuse + size >= ksp->ks_limit &&
387 ksp->ks_memuse < ksp->ks_limit)
388 wakeup((caddr_t)ksp);
389 ksp->ks_inuse--;
390#ifdef OLD_MALLOC_MEMORY_POLICY
391 if (kbp->kb_next == NULL)
392 kbp->kb_next = addr;
393 else
394 ((struct freelist *)kbp->kb_last)->next = addr;
395 freep->next = NULL;
396 kbp->kb_last = addr;
397#else
398 /*
399 * Return memory to the head of the queue for quick reuse. This
400 * can improve performance by improving the probability of the
401 * item being in the cache when it is reused.
402 */
403 if (kbp->kb_next == NULL) {
404 kbp->kb_next = addr;
405 kbp->kb_last = addr;
406 freep->next = NULL;
407 } else {
408 freep->next = kbp->kb_next;
409 kbp->kb_next = addr;
410 }
411#endif
412 splx(s);
413 mtx_exit(&malloc_mtx, MTX_DEF);
413 mtx_unlock(&malloc_mtx);
414}
415
416/*
417 * Initialize the kernel memory allocator
418 */
419/* ARGSUSED*/
420static void
421kmeminit(dummy)
422 void *dummy;
423{
424 register long indx;
425 u_long npg;
426 u_long mem_size;
427 u_long xvm_kmem_size;
428
429#if ((MAXALLOCSAVE & (MAXALLOCSAVE - 1)) != 0)
430#error "kmeminit: MAXALLOCSAVE not power of 2"
431#endif
432#if (MAXALLOCSAVE > MINALLOCSIZE * 32768)
433#error "kmeminit: MAXALLOCSAVE too big"
434#endif
435#if (MAXALLOCSAVE < PAGE_SIZE)
436#error "kmeminit: MAXALLOCSAVE too small"
437#endif
438
439 mtx_init(&malloc_mtx, "malloc", MTX_DEF);
440
441 /*
442 * Try to auto-tune the kernel memory size, so that it is
443 * more applicable for a wider range of machine sizes.
444 * On an X86, a VM_KMEM_SIZE_SCALE value of 4 is good, while
445 * a VM_KMEM_SIZE of 12MB is a fair compromise. The
446 * VM_KMEM_SIZE_MAX is dependent on the maximum KVA space
447 * available, and on an X86 with a total KVA space of 256MB,
448 * try to keep VM_KMEM_SIZE_MAX at 80MB or below.
449 *
450 * Note that the kmem_map is also used by the zone allocator,
451 * so make sure that there is enough space.
452 */
453 xvm_kmem_size = VM_KMEM_SIZE;
454 mem_size = cnt.v_page_count * PAGE_SIZE;
455
456#if defined(VM_KMEM_SIZE_SCALE)
457 if ((mem_size / VM_KMEM_SIZE_SCALE) > xvm_kmem_size)
458 xvm_kmem_size = mem_size / VM_KMEM_SIZE_SCALE;
459#endif
460
461#if defined(VM_KMEM_SIZE_MAX)
462 if (xvm_kmem_size >= VM_KMEM_SIZE_MAX)
463 xvm_kmem_size = VM_KMEM_SIZE_MAX;
464#endif
465
466 /* Allow final override from the kernel environment */
467 TUNABLE_INT_FETCH("kern.vm.kmem.size", xvm_kmem_size, vm_kmem_size);
468
469 /*
470 * Limit kmem virtual size to twice the physical memory.
471 * This allows for kmem map sparseness, but limits the size
472 * to something sane. Be careful to not overflow the 32bit
473 * ints while doing the check.
474 */
475 if ((vm_kmem_size / 2) > (cnt.v_page_count * PAGE_SIZE))
476 vm_kmem_size = 2 * cnt.v_page_count * PAGE_SIZE;
477
478 npg = (nmbufs * MSIZE + nmbclusters * MCLBYTES + vm_kmem_size)
479 / PAGE_SIZE;
480
481 kmemusage = (struct kmemusage *) kmem_alloc(kernel_map,
482 (vm_size_t)(npg * sizeof(struct kmemusage)));
483 kmem_map = kmem_suballoc(kernel_map, (vm_offset_t *)&kmembase,
484 (vm_offset_t *)&kmemlimit, (vm_size_t)(npg * PAGE_SIZE));
485 kmem_map->system_map = 1;
486 for (indx = 0; indx < MINBUCKET + 16; indx++) {
487 if (1 << indx >= PAGE_SIZE)
488 bucket[indx].kb_elmpercl = 1;
489 else
490 bucket[indx].kb_elmpercl = PAGE_SIZE / (1 << indx);
491 bucket[indx].kb_highwat = 5 * bucket[indx].kb_elmpercl;
492 }
493}
494
495void
496malloc_init(data)
497 void *data;
498{
499 struct malloc_type *type = (struct malloc_type *)data;
500
501 if (type->ks_magic != M_MAGIC)
502 panic("malloc type lacks magic");
503
504 if (type->ks_limit != 0)
505 return;
506
507 if (cnt.v_page_count == 0)
508 panic("malloc_init not allowed before vm init");
509
510 /*
511 * The default limits for each malloc region is 1/2 of the
512 * malloc portion of the kmem map size.
513 */
514 type->ks_limit = vm_kmem_size / 2;
515 type->ks_next = kmemstatistics;
516 kmemstatistics = type;
517}
518
519void
520malloc_uninit(data)
521 void *data;
522{
523 struct malloc_type *type = (struct malloc_type *)data;
524 struct malloc_type *t;
525#ifdef INVARIANTS
526 struct kmembuckets *kbp;
527 struct freelist *freep;
528 long indx;
529 int s;
530#endif
531
532 if (type->ks_magic != M_MAGIC)
533 panic("malloc type lacks magic");
534
535 if (cnt.v_page_count == 0)
536 panic("malloc_uninit not allowed before vm init");
537
538 if (type->ks_limit == 0)
539 panic("malloc_uninit on uninitialized type");
540
541#ifdef INVARIANTS
542 s = splmem();
414}
415
416/*
417 * Initialize the kernel memory allocator
418 */
419/* ARGSUSED*/
420static void
421kmeminit(dummy)
422 void *dummy;
423{
424 register long indx;
425 u_long npg;
426 u_long mem_size;
427 u_long xvm_kmem_size;
428
429#if ((MAXALLOCSAVE & (MAXALLOCSAVE - 1)) != 0)
430#error "kmeminit: MAXALLOCSAVE not power of 2"
431#endif
432#if (MAXALLOCSAVE > MINALLOCSIZE * 32768)
433#error "kmeminit: MAXALLOCSAVE too big"
434#endif
435#if (MAXALLOCSAVE < PAGE_SIZE)
436#error "kmeminit: MAXALLOCSAVE too small"
437#endif
438
439 mtx_init(&malloc_mtx, "malloc", MTX_DEF);
440
441 /*
442 * Try to auto-tune the kernel memory size, so that it is
443 * more applicable for a wider range of machine sizes.
444 * On an X86, a VM_KMEM_SIZE_SCALE value of 4 is good, while
445 * a VM_KMEM_SIZE of 12MB is a fair compromise. The
446 * VM_KMEM_SIZE_MAX is dependent on the maximum KVA space
447 * available, and on an X86 with a total KVA space of 256MB,
448 * try to keep VM_KMEM_SIZE_MAX at 80MB or below.
449 *
450 * Note that the kmem_map is also used by the zone allocator,
451 * so make sure that there is enough space.
452 */
453 xvm_kmem_size = VM_KMEM_SIZE;
454 mem_size = cnt.v_page_count * PAGE_SIZE;
455
456#if defined(VM_KMEM_SIZE_SCALE)
457 if ((mem_size / VM_KMEM_SIZE_SCALE) > xvm_kmem_size)
458 xvm_kmem_size = mem_size / VM_KMEM_SIZE_SCALE;
459#endif
460
461#if defined(VM_KMEM_SIZE_MAX)
462 if (xvm_kmem_size >= VM_KMEM_SIZE_MAX)
463 xvm_kmem_size = VM_KMEM_SIZE_MAX;
464#endif
465
466 /* Allow final override from the kernel environment */
467 TUNABLE_INT_FETCH("kern.vm.kmem.size", xvm_kmem_size, vm_kmem_size);
468
469 /*
470 * Limit kmem virtual size to twice the physical memory.
471 * This allows for kmem map sparseness, but limits the size
472 * to something sane. Be careful to not overflow the 32bit
473 * ints while doing the check.
474 */
475 if ((vm_kmem_size / 2) > (cnt.v_page_count * PAGE_SIZE))
476 vm_kmem_size = 2 * cnt.v_page_count * PAGE_SIZE;
477
478 npg = (nmbufs * MSIZE + nmbclusters * MCLBYTES + vm_kmem_size)
479 / PAGE_SIZE;
480
481 kmemusage = (struct kmemusage *) kmem_alloc(kernel_map,
482 (vm_size_t)(npg * sizeof(struct kmemusage)));
483 kmem_map = kmem_suballoc(kernel_map, (vm_offset_t *)&kmembase,
484 (vm_offset_t *)&kmemlimit, (vm_size_t)(npg * PAGE_SIZE));
485 kmem_map->system_map = 1;
486 for (indx = 0; indx < MINBUCKET + 16; indx++) {
487 if (1 << indx >= PAGE_SIZE)
488 bucket[indx].kb_elmpercl = 1;
489 else
490 bucket[indx].kb_elmpercl = PAGE_SIZE / (1 << indx);
491 bucket[indx].kb_highwat = 5 * bucket[indx].kb_elmpercl;
492 }
493}
494
495void
496malloc_init(data)
497 void *data;
498{
499 struct malloc_type *type = (struct malloc_type *)data;
500
501 if (type->ks_magic != M_MAGIC)
502 panic("malloc type lacks magic");
503
504 if (type->ks_limit != 0)
505 return;
506
507 if (cnt.v_page_count == 0)
508 panic("malloc_init not allowed before vm init");
509
510 /*
511 * The default limits for each malloc region is 1/2 of the
512 * malloc portion of the kmem map size.
513 */
514 type->ks_limit = vm_kmem_size / 2;
515 type->ks_next = kmemstatistics;
516 kmemstatistics = type;
517}
518
519void
520malloc_uninit(data)
521 void *data;
522{
523 struct malloc_type *type = (struct malloc_type *)data;
524 struct malloc_type *t;
525#ifdef INVARIANTS
526 struct kmembuckets *kbp;
527 struct freelist *freep;
528 long indx;
529 int s;
530#endif
531
532 if (type->ks_magic != M_MAGIC)
533 panic("malloc type lacks magic");
534
535 if (cnt.v_page_count == 0)
536 panic("malloc_uninit not allowed before vm init");
537
538 if (type->ks_limit == 0)
539 panic("malloc_uninit on uninitialized type");
540
541#ifdef INVARIANTS
542 s = splmem();
543 mtx_enter(&malloc_mtx, MTX_DEF);
543 mtx_lock(&malloc_mtx);
544 for (indx = 0; indx < MINBUCKET + 16; indx++) {
545 kbp = bucket + indx;
546 freep = (struct freelist*)kbp->kb_next;
547 while (freep) {
548 if (freep->type == type)
549 freep->type = M_FREE;
550 freep = (struct freelist*)freep->next;
551 }
552 }
553 splx(s);
544 for (indx = 0; indx < MINBUCKET + 16; indx++) {
545 kbp = bucket + indx;
546 freep = (struct freelist*)kbp->kb_next;
547 while (freep) {
548 if (freep->type == type)
549 freep->type = M_FREE;
550 freep = (struct freelist*)freep->next;
551 }
552 }
553 splx(s);
554 mtx_exit(&malloc_mtx, MTX_DEF);
554 mtx_unlock(&malloc_mtx);
555
556 if (type->ks_memuse != 0)
557 printf("malloc_uninit: %ld bytes of '%s' still allocated\n",
558 type->ks_memuse, type->ks_shortdesc);
559#endif
560
561 if (type == kmemstatistics)
562 kmemstatistics = type->ks_next;
563 else {
564 for (t = kmemstatistics; t->ks_next != NULL; t = t->ks_next) {
565 if (t->ks_next == type) {
566 t->ks_next = type->ks_next;
567 break;
568 }
569 }
570 }
571 type->ks_next = NULL;
572 type->ks_limit = 0;
573}
555
556 if (type->ks_memuse != 0)
557 printf("malloc_uninit: %ld bytes of '%s' still allocated\n",
558 type->ks_memuse, type->ks_shortdesc);
559#endif
560
561 if (type == kmemstatistics)
562 kmemstatistics = type->ks_next;
563 else {
564 for (t = kmemstatistics; t->ks_next != NULL; t = t->ks_next) {
565 if (t->ks_next == type) {
566 t->ks_next = type->ks_next;
567 break;
568 }
569 }
570 }
571 type->ks_next = NULL;
572 type->ks_limit = 0;
573}