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$
35 */
36
37 #include "opt_vm.h"
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #define MALLOC_INSTANTIATE
43 #include <sys/malloc.h>
44 #include <sys/mbuf.h>
45 #include <sys/vmmeter.h>
46 #include <sys/lock.h>
47
48 #include <vm/vm.h>
49 #include <vm/vm_param.h>
50 #include <vm/vm_kern.h>
51 #include <vm/vm_extern.h>
52 #include <vm/pmap.h>
53 #include <vm/vm_map.h>
54
55 static void kmeminit __P((void *));
56 SYSINIT(kmem, SI_SUB_KMEM, SI_ORDER_FIRST, kmeminit, NULL)
57
58 static MALLOC_DEFINE(M_FREE, "free", "should be on free list");
59
60 static struct malloc_type *kmemstatistics;
61 static struct kmembuckets bucket[MINBUCKET + 16];
62 static struct kmemusage *kmemusage;
63 static char *kmembase;
64 static char *kmemlimit;
65 static int vm_kmem_size;
66
67 #ifdef INVARIANTS
68 /*
69 * This structure provides a set of masks to catch unaligned frees.
70 */
71 static long addrmask[] = { 0,
72 0x00000001, 0x00000003, 0x00000007, 0x0000000f,
73 0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff,
74 0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff,
75 0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff,
76 };
77
78 /*
79 * The WEIRD_ADDR is used as known text to copy into free objects so
80 * that modifications after frees can be detected.
81 */
82 #define WEIRD_ADDR 0xdeadc0de
83 #define MAX_COPY 64
84
85 /*
86 * Normally the first word of the structure is used to hold the list
87 * pointer for free objects. However, when running with diagnostics,
88 * we use the third and fourth fields, so as to catch modifications
89 * in the most commonly trashed first two words.
90 */
91 struct freelist {
92 long spare0;
93 struct malloc_type *type;
94 long spare1;
95 caddr_t next;
96 };
97 #else /* !INVARIANTS */
98 struct freelist {
99 caddr_t next;
100 };
101 #endif /* INVARIANTS */
102
103 /*
104 * Allocate a block of memory
105 */
106 void *
107 malloc(size, type, flags)
108 unsigned long size;
109 struct malloc_type *type;
110 int flags;
111 {
112 register struct kmembuckets *kbp;
113 register struct kmemusage *kup;
114 register struct freelist *freep;
115 long indx, npg, allocsize;
116 int s;
117 caddr_t va, cp, savedlist;
118 #ifdef INVARIANTS
119 long *end, *lp;
120 int copysize;
121 char *savedtype;
122 #endif
123 register struct malloc_type *ksp = type;
124
125 /*
126 * Must be at splmem() prior to initializing segment to handle
127 * potential initialization race.
128 */
129
130 s = splmem();
131
132 if (type->ks_limit == 0)
133 malloc_init(type);
134
135 indx = BUCKETINDX(size);
136 kbp = &bucket[indx];
137 while (ksp->ks_memuse >= ksp->ks_limit) {
138 if (flags & M_NOWAIT) {
139 splx(s);
140 return ((void *) NULL);
141 }
142 if (ksp->ks_limblocks < 65535)
143 ksp->ks_limblocks++;
144 tsleep((caddr_t)ksp, PSWP+2, type->ks_shortdesc, 0);
145 }
146 ksp->ks_size |= 1 << indx;
147 #ifdef INVARIANTS
148 copysize = 1 << indx < MAX_COPY ? 1 << indx : MAX_COPY;
149 #endif
150 if (kbp->kb_next == NULL) {
151 kbp->kb_last = NULL;
152 if (size > MAXALLOCSAVE)
153 allocsize = roundup(size, PAGE_SIZE);
154 else
155 allocsize = 1 << indx;
156 npg = btoc(allocsize);
157 va = (caddr_t) kmem_malloc(kmem_map, (vm_size_t)ctob(npg), flags);
158 if (va == NULL) {
159 splx(s);
160 return ((void *) NULL);
161 }
162 kbp->kb_total += kbp->kb_elmpercl;
163 kup = btokup(va);
164 kup->ku_indx = indx;
165 if (allocsize > MAXALLOCSAVE) {
166 if (npg > 65535)
167 panic("malloc: allocation too large");
168 kup->ku_pagecnt = npg;
169 ksp->ks_memuse += allocsize;
170 goto out;
171 }
172 kup->ku_freecnt = kbp->kb_elmpercl;
173 kbp->kb_totalfree += kbp->kb_elmpercl;
174 /*
175 * Just in case we blocked while allocating memory,
176 * and someone else also allocated memory for this
177 * bucket, don't assume the list is still empty.
178 */
179 savedlist = kbp->kb_next;
180 kbp->kb_next = cp = va + (npg * PAGE_SIZE) - allocsize;
181 for (;;) {
182 freep = (struct freelist *)cp;
183 #ifdef INVARIANTS
184 /*
185 * Copy in known text to detect modification
186 * after freeing.
187 */
188 end = (long *)&cp[copysize];
189 for (lp = (long *)cp; lp < end; lp++)
190 *lp = WEIRD_ADDR;
191 freep->type = M_FREE;
192 #endif /* INVARIANTS */
193 if (cp <= va)
194 break;
195 cp -= allocsize;
196 freep->next = cp;
197 }
198 freep->next = savedlist;
199 if (kbp->kb_last == NULL)
200 kbp->kb_last = (caddr_t)freep;
201 }
202 va = kbp->kb_next;
203 kbp->kb_next = ((struct freelist *)va)->next;
204 #ifdef INVARIANTS
205 freep = (struct freelist *)va;
206 savedtype = (char *) type->ks_shortdesc;
207 #if BYTE_ORDER == BIG_ENDIAN
208 freep->type = (struct malloc_type *)WEIRD_ADDR >> 16;
209 #endif
210 #if BYTE_ORDER == LITTLE_ENDIAN
211 freep->type = (struct malloc_type *)WEIRD_ADDR;
212 #endif
213 if ((intptr_t)(void *)&freep->next & 0x2)
214 freep->next = (caddr_t)((WEIRD_ADDR >> 16)|(WEIRD_ADDR << 16));
215 else
216 freep->next = (caddr_t)WEIRD_ADDR;
217 end = (long *)&va[copysize];
218 for (lp = (long *)va; lp < end; lp++) {
219 if (*lp == WEIRD_ADDR)
220 continue;
221 printf("%s %ld of object %p size %lu %s %s (0x%lx != 0x%lx)\n",
222 "Data modified on freelist: word",
223 (long)(lp - (long *)va), (void *)va, size,
224 "previous type", savedtype, *lp, (u_long)WEIRD_ADDR);
225 break;
226 }
227 freep->spare0 = 0;
228 #endif /* INVARIANTS */
229 kup = btokup(va);
230 if (kup->ku_indx != indx)
231 panic("malloc: wrong bucket");
232 if (kup->ku_freecnt == 0)
233 panic("malloc: lost data");
234 kup->ku_freecnt--;
235 kbp->kb_totalfree--;
236 ksp->ks_memuse += 1 << indx;
237 out:
238 kbp->kb_calls++;
239 ksp->ks_inuse++;
240 ksp->ks_calls++;
241 if (ksp->ks_memuse > ksp->ks_maxused)
242 ksp->ks_maxused = ksp->ks_memuse;
243 splx(s);
244 return ((void *) va);
245 }
246
247 /*
248 * Free a block of memory allocated by malloc.
249 */
250 void
251 free(addr, type)
252 void *addr;
253 struct malloc_type *type;
254 {
255 register struct kmembuckets *kbp;
256 register struct kmemusage *kup;
257 register struct freelist *freep;
258 long size;
259 int s;
260 #ifdef INVARIANTS
261 struct freelist *fp;
262 long *end, *lp, alloc, copysize;
263 #endif
264 register struct malloc_type *ksp = type;
265
266 if (type->ks_limit == 0)
267 panic("freeing with unknown type (%s)", type->ks_shortdesc);
268
269 KASSERT(kmembase <= (char *)addr && (char *)addr < kmemlimit,
270 ("free: address %p out of range", (void *)addr));
271 kup = btokup(addr);
272 size = 1 << kup->ku_indx;
273 kbp = &bucket[kup->ku_indx];
274 s = splmem();
275 #ifdef INVARIANTS
276 /*
277 * Check for returns of data that do not point to the
278 * beginning of the allocation.
279 */
280 if (size > PAGE_SIZE)
281 alloc = addrmask[BUCKETINDX(PAGE_SIZE)];
282 else
283 alloc = addrmask[kup->ku_indx];
284 if (((uintptr_t)(void *)addr & alloc) != 0)
285 panic("free: unaligned addr %p, size %ld, type %s, mask %ld",
286 (void *)addr, size, type->ks_shortdesc, alloc);
287 #endif /* INVARIANTS */
288 if (size > MAXALLOCSAVE) {
289 kmem_free(kmem_map, (vm_offset_t)addr, ctob(kup->ku_pagecnt));
290 size = kup->ku_pagecnt << PAGE_SHIFT;
291 ksp->ks_memuse -= size;
292 kup->ku_indx = 0;
293 kup->ku_pagecnt = 0;
294 if (ksp->ks_memuse + size >= ksp->ks_limit &&
295 ksp->ks_memuse < ksp->ks_limit)
296 wakeup((caddr_t)ksp);
297 ksp->ks_inuse--;
298 kbp->kb_total -= 1;
299 splx(s);
300 return;
301 }
302 freep = (struct freelist *)addr;
303 #ifdef INVARIANTS
304 /*
305 * Check for multiple frees. Use a quick check to see if
306 * it looks free before laboriously searching the freelist.
307 */
308 if (freep->spare0 == WEIRD_ADDR) {
309 fp = (struct freelist *)kbp->kb_next;
310 while (fp) {
311 if (fp->spare0 != WEIRD_ADDR)
312 panic("free: free item %p modified", fp);
313 else if (addr == (caddr_t)fp)
314 panic("free: multiple freed item %p", addr);
315 fp = (struct freelist *)fp->next;
316 }
317 }
318 /*
319 * Copy in known text to detect modification after freeing
320 * and to make it look free. Also, save the type being freed
321 * so we can list likely culprit if modification is detected
322 * when the object is reallocated.
323 */
324 copysize = size < MAX_COPY ? size : MAX_COPY;
325 end = (long *)&((caddr_t)addr)[copysize];
326 for (lp = (long *)addr; lp < end; lp++)
327 *lp = WEIRD_ADDR;
328 freep->type = type;
329 #endif /* INVARIANTS */
330 kup->ku_freecnt++;
331 if (kup->ku_freecnt >= kbp->kb_elmpercl)
332 if (kup->ku_freecnt > kbp->kb_elmpercl)
333 panic("free: multiple frees");
334 else if (kbp->kb_totalfree > kbp->kb_highwat)
335 kbp->kb_couldfree++;
336 kbp->kb_totalfree++;
337 ksp->ks_memuse -= size;
338 if (ksp->ks_memuse + size >= ksp->ks_limit &&
339 ksp->ks_memuse < ksp->ks_limit)
340 wakeup((caddr_t)ksp);
341 ksp->ks_inuse--;
342 #ifdef OLD_MALLOC_MEMORY_POLICY
343 if (kbp->kb_next == NULL)
344 kbp->kb_next = addr;
345 else
346 ((struct freelist *)kbp->kb_last)->next = addr;
347 freep->next = NULL;
348 kbp->kb_last = addr;
349 #else
350 /*
351 * Return memory to the head of the queue for quick reuse. This
352 * can improve performance by improving the probability of the
353 * item being in the cache when it is reused.
354 */
355 if (kbp->kb_next == NULL) {
356 kbp->kb_next = addr;
357 kbp->kb_last = addr;
358 freep->next = NULL;
359 } else {
360 freep->next = kbp->kb_next;
361 kbp->kb_next = addr;
362 }
363 #endif
364 splx(s);
365 }
366
367 /*
368 * Initialize the kernel memory allocator
369 */
370 /* ARGSUSED*/
371 static void
372 kmeminit(dummy)
373 void *dummy;
374 {
375 register long indx;
376 int npg;
377 int mem_size;
378 int xvm_kmem_size;
379
380 #if ((MAXALLOCSAVE & (MAXALLOCSAVE - 1)) != 0)
381 #error "kmeminit: MAXALLOCSAVE not power of 2"
382 #endif
383 #if (MAXALLOCSAVE > MINALLOCSIZE * 32768)
384 #error "kmeminit: MAXALLOCSAVE too big"
385 #endif
386 #if (MAXALLOCSAVE < PAGE_SIZE)
387 #error "kmeminit: MAXALLOCSAVE too small"
388 #endif
389
390 /*
391 * Try to auto-tune the kernel memory size, so that it is
392 * more applicable for a wider range of machine sizes.
393 * On an X86, a VM_KMEM_SIZE_SCALE value of 4 is good, while
394 * a VM_KMEM_SIZE of 12MB is a fair compromise. The
395 * VM_KMEM_SIZE_MAX is dependent on the maximum KVA space
396 * available, and on an X86 with a total KVA space of 256MB,
397 * try to keep VM_KMEM_SIZE_MAX at 80MB or below.
398 *
399 * Note that the kmem_map is also used by the zone allocator,
400 * so make sure that there is enough space.
401 */
402 vm_kmem_size = VM_KMEM_SIZE;
403 mem_size = cnt.v_page_count * PAGE_SIZE;
404
405 #if defined(VM_KMEM_SIZE_SCALE)
406 if ((mem_size / VM_KMEM_SIZE_SCALE) > vm_kmem_size)
407 vm_kmem_size = mem_size / VM_KMEM_SIZE_SCALE;
408 #endif
409
410 #if defined(VM_KMEM_SIZE_MAX)
411 if (vm_kmem_size >= VM_KMEM_SIZE_MAX)
412 vm_kmem_size = VM_KMEM_SIZE_MAX;
413 #endif
414
415 /* Allow final override from the kernel environment */
416 if (getenv_int("kern.vm.kmem.size", &xvm_kmem_size))
417 vm_kmem_size = xvm_kmem_size;
418
419 if (vm_kmem_size > 2 * (cnt.v_page_count * PAGE_SIZE))
420 vm_kmem_size = 2 * (cnt.v_page_count * PAGE_SIZE);
421
422 npg = (nmbufs * MSIZE + nmbclusters * MCLBYTES + vm_kmem_size)
423 / PAGE_SIZE;
424
425 kmemusage = (struct kmemusage *) kmem_alloc(kernel_map,
426 (vm_size_t)(npg * sizeof(struct kmemusage)));
427 kmem_map = kmem_suballoc(kernel_map, (vm_offset_t *)&kmembase,
428 (vm_offset_t *)&kmemlimit, (vm_size_t)(npg * PAGE_SIZE));
429 kmem_map->system_map = 1;
430 for (indx = 0; indx < MINBUCKET + 16; indx++) {
431 if (1 << indx >= PAGE_SIZE)
432 bucket[indx].kb_elmpercl = 1;
433 else
434 bucket[indx].kb_elmpercl = PAGE_SIZE / (1 << indx);
435 bucket[indx].kb_highwat = 5 * bucket[indx].kb_elmpercl;
436 }
437 }
438
439 void
440 malloc_init(data)
441 void *data;
442 {
443 struct malloc_type *type = (struct malloc_type *)data;
444
445 if (type->ks_magic != M_MAGIC)
446 panic("malloc type lacks magic");
447
448 if (type->ks_limit != 0)
449 return;
450
451 if (cnt.v_page_count == 0)
452 panic("malloc_init not allowed before vm init");
453
454 /*
455 * The default limits for each malloc region is 1/2 of the
456 * malloc portion of the kmem map size.
457 */
458 type->ks_limit = vm_kmem_size / 2;
459 type->ks_next = kmemstatistics;
460 kmemstatistics = type;
461 }
462
463 void
464 malloc_uninit(data)
465 void *data;
466 {
467 struct malloc_type *type = (struct malloc_type *)data;
468 struct malloc_type *t;
469
470 if (type->ks_magic != M_MAGIC)
471 panic("malloc type lacks magic");
472
473 if (cnt.v_page_count == 0)
474 panic("malloc_uninit not allowed before vm init");
475
476 if (type->ks_limit == 0)
477 panic("malloc_uninit on uninitialized type");
478
479 if (type == kmemstatistics)
480 kmemstatistics = type->ks_next;
481 else {
482 for (t = kmemstatistics; t->ks_next != NULL; t = t->ks_next) {
483 if (t->ks_next == type) {
484 t->ks_next = type->ks_next;
485 break;
486 }
487 }
488 }
489 type->ks_next = NULL;
490 type->ks_limit = 0;
491 }
Cache object: 6367de91a3f7a23206f1d7ab58f31751
|