kern_malloc.c revision 1.99.2.10 1 /* $NetBSD: kern_malloc.c,v 1.99.2.10 2008/03/24 09:39:01 yamt Exp $ */
2
3 /*
4 * Copyright (c) 1987, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * @(#)kern_malloc.c 8.4 (Berkeley) 5/20/95
32 */
33
34 /*
35 * Copyright (c) 1996 Christopher G. Demetriou. All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 * 3. All advertising materials mentioning features or use of this software
46 * must display the following acknowledgement:
47 * This product includes software developed by the University of
48 * California, Berkeley and its contributors.
49 * 4. Neither the name of the University nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
52 *
53 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63 * SUCH DAMAGE.
64 *
65 * @(#)kern_malloc.c 8.4 (Berkeley) 5/20/95
66 */
67
68 #include <sys/cdefs.h>
69 __KERNEL_RCSID(0, "$NetBSD: kern_malloc.c,v 1.99.2.10 2008/03/24 09:39:01 yamt Exp $");
70
71 #include <sys/param.h>
72 #include <sys/proc.h>
73 #include <sys/kernel.h>
74 #include <sys/malloc.h>
75 #include <sys/systm.h>
76 #include <sys/debug.h>
77 #include <sys/mutex.h>
78 #include <sys/lockdebug.h>
79
80 #include <uvm/uvm_extern.h>
81
82 static struct vm_map_kernel kmem_map_store;
83 struct vm_map *kmem_map = NULL;
84
85 #include "opt_kmempages.h"
86
87 #ifdef NKMEMCLUSTERS
88 #error NKMEMCLUSTERS is obsolete; remove it from your kernel config file and use NKMEMPAGES instead or let the kernel auto-size
89 #endif
90
91 /*
92 * Default number of pages in kmem_map. We attempt to calculate this
93 * at run-time, but allow it to be either patched or set in the kernel
94 * config file.
95 */
96 #ifndef NKMEMPAGES
97 #define NKMEMPAGES 0
98 #endif
99 int nkmempages = NKMEMPAGES;
100
101 /*
102 * Defaults for lower- and upper-bounds for the kmem_map page count.
103 * Can be overridden by kernel config options.
104 */
105 #ifndef NKMEMPAGES_MIN
106 #define NKMEMPAGES_MIN NKMEMPAGES_MIN_DEFAULT
107 #endif
108
109 #ifndef NKMEMPAGES_MAX
110 #define NKMEMPAGES_MAX NKMEMPAGES_MAX_DEFAULT
111 #endif
112
113 #include "opt_kmemstats.h"
114 #include "opt_malloclog.h"
115 #include "opt_malloc_debug.h"
116
117 #define MINALLOCSIZE (1 << MINBUCKET)
118 #define BUCKETINDX(size) \
119 ((size) <= (MINALLOCSIZE * 128) \
120 ? (size) <= (MINALLOCSIZE * 8) \
121 ? (size) <= (MINALLOCSIZE * 2) \
122 ? (size) <= (MINALLOCSIZE * 1) \
123 ? (MINBUCKET + 0) \
124 : (MINBUCKET + 1) \
125 : (size) <= (MINALLOCSIZE * 4) \
126 ? (MINBUCKET + 2) \
127 : (MINBUCKET + 3) \
128 : (size) <= (MINALLOCSIZE* 32) \
129 ? (size) <= (MINALLOCSIZE * 16) \
130 ? (MINBUCKET + 4) \
131 : (MINBUCKET + 5) \
132 : (size) <= (MINALLOCSIZE * 64) \
133 ? (MINBUCKET + 6) \
134 : (MINBUCKET + 7) \
135 : (size) <= (MINALLOCSIZE * 2048) \
136 ? (size) <= (MINALLOCSIZE * 512) \
137 ? (size) <= (MINALLOCSIZE * 256) \
138 ? (MINBUCKET + 8) \
139 : (MINBUCKET + 9) \
140 : (size) <= (MINALLOCSIZE * 1024) \
141 ? (MINBUCKET + 10) \
142 : (MINBUCKET + 11) \
143 : (size) <= (MINALLOCSIZE * 8192) \
144 ? (size) <= (MINALLOCSIZE * 4096) \
145 ? (MINBUCKET + 12) \
146 : (MINBUCKET + 13) \
147 : (size) <= (MINALLOCSIZE * 16384) \
148 ? (MINBUCKET + 14) \
149 : (MINBUCKET + 15))
150
151 /*
152 * Array of descriptors that describe the contents of each page
153 */
154 struct kmemusage {
155 short ku_indx; /* bucket index */
156 union {
157 u_short freecnt;/* for small allocations, free pieces in page */
158 u_short pagecnt;/* for large allocations, pages alloced */
159 } ku_un;
160 };
161 #define ku_freecnt ku_un.freecnt
162 #define ku_pagecnt ku_un.pagecnt
163
164 struct kmembuckets kmembuckets[MINBUCKET + 16];
165 struct kmemusage *kmemusage;
166 char *kmembase, *kmemlimit;
167
168 #ifdef DEBUG
169 static void *malloc_freecheck;
170 #endif
171
172 /*
173 * Turn virtual addresses into kmem map indicies
174 */
175 #define btokup(addr) (&kmemusage[((char *)(addr) - kmembase) >> PGSHIFT])
176
177 struct malloc_type *kmemstatistics;
178
179 #ifdef MALLOCLOG
180 #ifndef MALLOCLOGSIZE
181 #define MALLOCLOGSIZE 100000
182 #endif
183
184 struct malloclog {
185 void *addr;
186 long size;
187 struct malloc_type *type;
188 int action;
189 const char *file;
190 long line;
191 } malloclog[MALLOCLOGSIZE];
192
193 long malloclogptr;
194
195 static void
196 domlog(void *a, long size, struct malloc_type *type, int action,
197 const char *file, long line)
198 {
199
200 malloclog[malloclogptr].addr = a;
201 malloclog[malloclogptr].size = size;
202 malloclog[malloclogptr].type = type;
203 malloclog[malloclogptr].action = action;
204 malloclog[malloclogptr].file = file;
205 malloclog[malloclogptr].line = line;
206 malloclogptr++;
207 if (malloclogptr >= MALLOCLOGSIZE)
208 malloclogptr = 0;
209 }
210
211 static void
212 hitmlog(void *a)
213 {
214 struct malloclog *lp;
215 long l;
216
217 #define PRT do { \
218 lp = &malloclog[l]; \
219 if (lp->addr == a && lp->action) { \
220 printf("malloc log entry %ld:\n", l); \
221 printf("\taddr = %p\n", lp->addr); \
222 printf("\tsize = %ld\n", lp->size); \
223 printf("\ttype = %s\n", lp->type->ks_shortdesc); \
224 printf("\taction = %s\n", lp->action == 1 ? "alloc" : "free"); \
225 printf("\tfile = %s\n", lp->file); \
226 printf("\tline = %ld\n", lp->line); \
227 } \
228 } while (/* CONSTCOND */0)
229
230 for (l = malloclogptr; l < MALLOCLOGSIZE; l++)
231 PRT;
232
233 for (l = 0; l < malloclogptr; l++)
234 PRT;
235 #undef PRT
236 }
237 #endif /* MALLOCLOG */
238
239 #ifdef DIAGNOSTIC
240 /*
241 * This structure provides a set of masks to catch unaligned frees.
242 */
243 const long addrmask[] = { 0,
244 0x00000001, 0x00000003, 0x00000007, 0x0000000f,
245 0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff,
246 0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff,
247 0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff,
248 };
249
250 /*
251 * The WEIRD_ADDR is used as known text to copy into free objects so
252 * that modifications after frees can be detected.
253 */
254 #define WEIRD_ADDR ((uint32_t) 0xdeadbeef)
255 #ifdef DEBUG
256 #define MAX_COPY PAGE_SIZE
257 #else
258 #define MAX_COPY 32
259 #endif
260
261 /*
262 * Normally the freelist structure is used only to hold the list pointer
263 * for free objects. However, when running with diagnostics, the first
264 * 8/16 bytes of the structure is unused except for diagnostic information,
265 * and the free list pointer is at offset 8/16 in the structure. Since the
266 * first 8 bytes is the portion of the structure most often modified, this
267 * helps to detect memory reuse problems and avoid free list corruption.
268 */
269 struct freelist {
270 uint32_t spare0;
271 #ifdef _LP64
272 uint32_t spare1; /* explicit padding */
273 #endif
274 struct malloc_type *type;
275 void * next;
276 };
277 #else /* !DIAGNOSTIC */
278 struct freelist {
279 void * next;
280 };
281 #endif /* DIAGNOSTIC */
282
283 /*
284 * The following are standard, built-in malloc types and are not
285 * specific to any subsystem.
286 */
287 MALLOC_DEFINE(M_DEVBUF, "devbuf", "device driver memory");
288 MALLOC_DEFINE(M_DMAMAP, "DMA map", "bus_dma(9) structures");
289 MALLOC_DEFINE(M_FREE, "free", "should be on free list");
290 MALLOC_DEFINE(M_PCB, "pcb", "protocol control block");
291 MALLOC_DEFINE(M_SOFTINTR, "softintr", "Softinterrupt structures");
292 MALLOC_DEFINE(M_TEMP, "temp", "misc. temporary data buffers");
293
294 /* XXX These should all be elsewhere. */
295 MALLOC_DEFINE(M_RTABLE, "routetbl", "routing tables");
296 MALLOC_DEFINE(M_FTABLE, "fragtbl", "fragment reassembly header");
297 MALLOC_DEFINE(M_UFSMNT, "UFS mount", "UFS mount structure");
298 MALLOC_DEFINE(M_NETADDR, "Export Host", "Export host address structure");
299 MALLOC_DEFINE(M_IPMOPTS, "ip_moptions", "internet multicast options");
300 MALLOC_DEFINE(M_IPMADDR, "in_multi", "internet multicast address");
301 MALLOC_DEFINE(M_MRTABLE, "mrt", "multicast routing tables");
302 MALLOC_DEFINE(M_BWMETER, "bwmeter", "multicast upcall bw meters");
303 MALLOC_DEFINE(M_1394DATA, "1394data", "IEEE 1394 data buffers");
304
305 kmutex_t malloc_lock;
306
307 /*
308 * Allocate a block of memory
309 */
310 #ifdef MALLOCLOG
311 void *
312 _malloc(unsigned long size, struct malloc_type *ksp, int flags,
313 const char *file, long line)
314 #else
315 void *
316 malloc(unsigned long size, struct malloc_type *ksp, int flags)
317 #endif /* MALLOCLOG */
318 {
319 struct kmembuckets *kbp;
320 struct kmemusage *kup;
321 struct freelist *freep;
322 long indx, npg, allocsize;
323 char *va, *cp, *savedlist;
324 #ifdef DIAGNOSTIC
325 uint32_t *end, *lp;
326 int copysize;
327 #endif
328
329 #ifdef LOCKDEBUG
330 if ((flags & M_NOWAIT) == 0) {
331 ASSERT_SLEEPABLE();
332 }
333 #endif
334 #ifdef MALLOC_DEBUG
335 if (debug_malloc(size, ksp, flags, (void *) &va)) {
336 if (va != 0)
337 FREECHECK_OUT(&malloc_freecheck, (void *)va);
338 return ((void *) va);
339 }
340 #endif
341 indx = BUCKETINDX(size);
342 kbp = &kmembuckets[indx];
343 mutex_spin_enter(&malloc_lock);
344 #ifdef KMEMSTATS
345 while (ksp->ks_memuse >= ksp->ks_limit) {
346 if (flags & M_NOWAIT) {
347 mutex_spin_exit(&malloc_lock);
348 return ((void *) NULL);
349 }
350 if (ksp->ks_limblocks < 65535)
351 ksp->ks_limblocks++;
352 mtsleep((void *)ksp, PSWP+2, ksp->ks_shortdesc, 0,
353 &malloc_lock);
354 }
355 ksp->ks_size |= 1 << indx;
356 #endif
357 #ifdef DIAGNOSTIC
358 copysize = 1 << indx < MAX_COPY ? 1 << indx : MAX_COPY;
359 #endif
360 if (kbp->kb_next == NULL) {
361 int s;
362 kbp->kb_last = NULL;
363 if (size > MAXALLOCSAVE)
364 allocsize = round_page(size);
365 else
366 allocsize = 1 << indx;
367 npg = btoc(allocsize);
368 mutex_spin_exit(&malloc_lock);
369 s = splvm();
370 va = (void *) uvm_km_alloc(kmem_map,
371 (vsize_t)ctob(npg), 0,
372 ((flags & M_NOWAIT) ? UVM_KMF_NOWAIT : 0) |
373 ((flags & M_CANFAIL) ? UVM_KMF_CANFAIL : 0) |
374 UVM_KMF_WIRED);
375 splx(s);
376 if (__predict_false(va == NULL)) {
377 /*
378 * Kmem_malloc() can return NULL, even if it can
379 * wait, if there is no map space available, because
380 * it can't fix that problem. Neither can we,
381 * right now. (We should release pages which
382 * are completely free and which are in kmembuckets
383 * with too many free elements.)
384 */
385 if ((flags & (M_NOWAIT|M_CANFAIL)) == 0)
386 panic("malloc: out of space in kmem_map");
387 return (NULL);
388 }
389 mutex_spin_enter(&malloc_lock);
390 #ifdef KMEMSTATS
391 kbp->kb_total += kbp->kb_elmpercl;
392 #endif
393 kup = btokup(va);
394 kup->ku_indx = indx;
395 if (allocsize > MAXALLOCSAVE) {
396 if (npg > 65535)
397 panic("malloc: allocation too large");
398 kup->ku_pagecnt = npg;
399 #ifdef KMEMSTATS
400 ksp->ks_memuse += allocsize;
401 #endif
402 goto out;
403 }
404 #ifdef KMEMSTATS
405 kup->ku_freecnt = kbp->kb_elmpercl;
406 kbp->kb_totalfree += kbp->kb_elmpercl;
407 #endif
408 /*
409 * Just in case we blocked while allocating memory,
410 * and someone else also allocated memory for this
411 * kmembucket, don't assume the list is still empty.
412 */
413 savedlist = kbp->kb_next;
414 kbp->kb_next = cp = va + (npg << PAGE_SHIFT) - allocsize;
415 for (;;) {
416 freep = (struct freelist *)cp;
417 #ifdef DIAGNOSTIC
418 /*
419 * Copy in known text to detect modification
420 * after freeing.
421 */
422 end = (uint32_t *)&cp[copysize];
423 for (lp = (uint32_t *)cp; lp < end; lp++)
424 *lp = WEIRD_ADDR;
425 freep->type = M_FREE;
426 #endif /* DIAGNOSTIC */
427 if (cp <= va)
428 break;
429 cp -= allocsize;
430 freep->next = cp;
431 }
432 freep->next = savedlist;
433 if (savedlist == NULL)
434 kbp->kb_last = (void *)freep;
435 }
436 va = kbp->kb_next;
437 kbp->kb_next = ((struct freelist *)va)->next;
438 #ifdef DIAGNOSTIC
439 freep = (struct freelist *)va;
440 /* XXX potential to get garbage pointer here. */
441 if (kbp->kb_next) {
442 int rv;
443 vaddr_t addr = (vaddr_t)kbp->kb_next;
444
445 vm_map_lock(kmem_map);
446 rv = uvm_map_checkprot(kmem_map, addr,
447 addr + sizeof(struct freelist), VM_PROT_WRITE);
448 vm_map_unlock(kmem_map);
449
450 if (__predict_false(rv == 0)) {
451 printf("Data modified on freelist: "
452 "word %ld of object %p size %ld previous type %s "
453 "(invalid addr %p)\n",
454 (long)((int32_t *)&kbp->kb_next - (int32_t *)kbp),
455 va, size, "foo", kbp->kb_next);
456 #ifdef MALLOCLOG
457 hitmlog(va);
458 #endif
459 kbp->kb_next = NULL;
460 }
461 }
462
463 /* Fill the fields that we've used with WEIRD_ADDR */
464 #ifdef _LP64
465 freep->type = (struct malloc_type *)
466 (WEIRD_ADDR | (((u_long) WEIRD_ADDR) << 32));
467 #else
468 freep->type = (struct malloc_type *) WEIRD_ADDR;
469 #endif
470 end = (uint32_t *)&freep->next +
471 (sizeof(freep->next) / sizeof(int32_t));
472 for (lp = (uint32_t *)&freep->next; lp < end; lp++)
473 *lp = WEIRD_ADDR;
474
475 /* and check that the data hasn't been modified. */
476 end = (uint32_t *)&va[copysize];
477 for (lp = (uint32_t *)va; lp < end; lp++) {
478 if (__predict_true(*lp == WEIRD_ADDR))
479 continue;
480 printf("Data modified on freelist: "
481 "word %ld of object %p size %ld previous type %s "
482 "(0x%x != 0x%x)\n",
483 (long)(lp - (uint32_t *)va), va, size,
484 "bar", *lp, WEIRD_ADDR);
485 #ifdef MALLOCLOG
486 hitmlog(va);
487 #endif
488 break;
489 }
490
491 freep->spare0 = 0;
492 #endif /* DIAGNOSTIC */
493 #ifdef KMEMSTATS
494 kup = btokup(va);
495 if (kup->ku_indx != indx)
496 panic("malloc: wrong bucket");
497 if (kup->ku_freecnt == 0)
498 panic("malloc: lost data");
499 kup->ku_freecnt--;
500 kbp->kb_totalfree--;
501 ksp->ks_memuse += 1 << indx;
502 out:
503 kbp->kb_calls++;
504 ksp->ks_inuse++;
505 ksp->ks_calls++;
506 if (ksp->ks_memuse > ksp->ks_maxused)
507 ksp->ks_maxused = ksp->ks_memuse;
508 #else
509 out:
510 #endif
511 #ifdef MALLOCLOG
512 domlog(va, size, ksp, 1, file, line);
513 #endif
514 mutex_spin_exit(&malloc_lock);
515 if ((flags & M_ZERO) != 0)
516 memset(va, 0, size);
517 FREECHECK_OUT(&malloc_freecheck, (void *)va);
518 return ((void *) va);
519 }
520
521 /*
522 * Free a block of memory allocated by malloc.
523 */
524 #ifdef MALLOCLOG
525 void
526 _free(void *addr, struct malloc_type *ksp, const char *file, long line)
527 #else
528 void
529 free(void *addr, struct malloc_type *ksp)
530 #endif /* MALLOCLOG */
531 {
532 struct kmembuckets *kbp;
533 struct kmemusage *kup;
534 struct freelist *freep;
535 long size;
536 #ifdef DIAGNOSTIC
537 void *cp;
538 int32_t *end, *lp;
539 long alloc, copysize;
540 #endif
541
542 FREECHECK_IN(&malloc_freecheck, addr);
543 #ifdef MALLOC_DEBUG
544 if (debug_free(addr, ksp))
545 return;
546 #endif
547
548 #ifdef DIAGNOSTIC
549 /*
550 * Ensure that we're free'ing something that we could
551 * have allocated in the first place. That is, check
552 * to see that the address is within kmem_map.
553 */
554 if (__predict_false((vaddr_t)addr < vm_map_min(kmem_map) ||
555 (vaddr_t)addr >= vm_map_max(kmem_map)))
556 panic("free: addr %p not within kmem_map", addr);
557 #endif
558
559 kup = btokup(addr);
560 size = 1 << kup->ku_indx;
561 kbp = &kmembuckets[kup->ku_indx];
562
563 LOCKDEBUG_MEM_CHECK(addr,
564 size <= MAXALLOCSAVE ? size : ctob(kup->ku_pagecnt));
565
566 mutex_spin_enter(&malloc_lock);
567 #ifdef MALLOCLOG
568 domlog(addr, 0, ksp, 2, file, line);
569 #endif
570 #ifdef DIAGNOSTIC
571 /*
572 * Check for returns of data that do not point to the
573 * beginning of the allocation.
574 */
575 if (size > PAGE_SIZE)
576 alloc = addrmask[BUCKETINDX(PAGE_SIZE)];
577 else
578 alloc = addrmask[kup->ku_indx];
579 if (((u_long)addr & alloc) != 0)
580 panic("free: unaligned addr %p, size %ld, type %s, mask %ld",
581 addr, size, ksp->ks_shortdesc, alloc);
582 #endif /* DIAGNOSTIC */
583 if (size > MAXALLOCSAVE) {
584 uvm_km_free(kmem_map, (vaddr_t)addr, ctob(kup->ku_pagecnt),
585 UVM_KMF_WIRED);
586 #ifdef KMEMSTATS
587 size = kup->ku_pagecnt << PGSHIFT;
588 ksp->ks_memuse -= size;
589 kup->ku_indx = 0;
590 kup->ku_pagecnt = 0;
591 if (ksp->ks_memuse + size >= ksp->ks_limit &&
592 ksp->ks_memuse < ksp->ks_limit)
593 wakeup((void *)ksp);
594 #ifdef DIAGNOSTIC
595 if (ksp->ks_inuse == 0)
596 panic("free 1: inuse 0, probable double free");
597 #endif
598 ksp->ks_inuse--;
599 kbp->kb_total -= 1;
600 #endif
601 mutex_spin_exit(&malloc_lock);
602 return;
603 }
604 freep = (struct freelist *)addr;
605 #ifdef DIAGNOSTIC
606 /*
607 * Check for multiple frees. Use a quick check to see if
608 * it looks free before laboriously searching the freelist.
609 */
610 if (__predict_false(freep->spare0 == WEIRD_ADDR)) {
611 for (cp = kbp->kb_next; cp;
612 cp = ((struct freelist *)cp)->next) {
613 if (addr != cp)
614 continue;
615 printf("multiply freed item %p\n", addr);
616 #ifdef MALLOCLOG
617 hitmlog(addr);
618 #endif
619 panic("free: duplicated free");
620 }
621 }
622
623 /*
624 * Copy in known text to detect modification after freeing
625 * and to make it look free. Also, save the type being freed
626 * so we can list likely culprit if modification is detected
627 * when the object is reallocated.
628 */
629 copysize = size < MAX_COPY ? size : MAX_COPY;
630 end = (int32_t *)&((char *)addr)[copysize];
631 for (lp = (int32_t *)addr; lp < end; lp++)
632 *lp = WEIRD_ADDR;
633 freep->type = ksp;
634 #endif /* DIAGNOSTIC */
635 #ifdef KMEMSTATS
636 kup->ku_freecnt++;
637 if (kup->ku_freecnt >= kbp->kb_elmpercl) {
638 if (kup->ku_freecnt > kbp->kb_elmpercl)
639 panic("free: multiple frees");
640 else if (kbp->kb_totalfree > kbp->kb_highwat)
641 kbp->kb_couldfree++;
642 }
643 kbp->kb_totalfree++;
644 ksp->ks_memuse -= size;
645 if (ksp->ks_memuse + size >= ksp->ks_limit &&
646 ksp->ks_memuse < ksp->ks_limit)
647 wakeup((void *)ksp);
648 #ifdef DIAGNOSTIC
649 if (ksp->ks_inuse == 0)
650 panic("free 2: inuse 0, probable double free");
651 #endif
652 ksp->ks_inuse--;
653 #endif
654 if (kbp->kb_next == NULL)
655 kbp->kb_next = addr;
656 else
657 ((struct freelist *)kbp->kb_last)->next = addr;
658 freep->next = NULL;
659 kbp->kb_last = addr;
660 mutex_spin_exit(&malloc_lock);
661 }
662
663 /*
664 * Change the size of a block of memory.
665 */
666 void *
667 realloc(void *curaddr, unsigned long newsize, struct malloc_type *ksp,
668 int flags)
669 {
670 struct kmemusage *kup;
671 unsigned long cursize;
672 void *newaddr;
673 #ifdef DIAGNOSTIC
674 long alloc;
675 #endif
676
677 /*
678 * realloc() with a NULL pointer is the same as malloc().
679 */
680 if (curaddr == NULL)
681 return (malloc(newsize, ksp, flags));
682
683 /*
684 * realloc() with zero size is the same as free().
685 */
686 if (newsize == 0) {
687 free(curaddr, ksp);
688 return (NULL);
689 }
690
691 #ifdef LOCKDEBUG
692 if ((flags & M_NOWAIT) == 0) {
693 ASSERT_SLEEPABLE();
694 }
695 #endif
696
697 /*
698 * Find out how large the old allocation was (and do some
699 * sanity checking).
700 */
701 kup = btokup(curaddr);
702 cursize = 1 << kup->ku_indx;
703
704 #ifdef DIAGNOSTIC
705 /*
706 * Check for returns of data that do not point to the
707 * beginning of the allocation.
708 */
709 if (cursize > PAGE_SIZE)
710 alloc = addrmask[BUCKETINDX(PAGE_SIZE)];
711 else
712 alloc = addrmask[kup->ku_indx];
713 if (((u_long)curaddr & alloc) != 0)
714 panic("realloc: "
715 "unaligned addr %p, size %ld, type %s, mask %ld\n",
716 curaddr, cursize, ksp->ks_shortdesc, alloc);
717 #endif /* DIAGNOSTIC */
718
719 if (cursize > MAXALLOCSAVE)
720 cursize = ctob(kup->ku_pagecnt);
721
722 /*
723 * If we already actually have as much as they want, we're done.
724 */
725 if (newsize <= cursize)
726 return (curaddr);
727
728 /*
729 * Can't satisfy the allocation with the existing block.
730 * Allocate a new one and copy the data.
731 */
732 newaddr = malloc(newsize, ksp, flags);
733 if (__predict_false(newaddr == NULL)) {
734 /*
735 * malloc() failed, because flags included M_NOWAIT.
736 * Return NULL to indicate that failure. The old
737 * pointer is still valid.
738 */
739 return (NULL);
740 }
741 memcpy(newaddr, curaddr, cursize);
742
743 /*
744 * We were successful: free the old allocation and return
745 * the new one.
746 */
747 free(curaddr, ksp);
748 return (newaddr);
749 }
750
751 /*
752 * Roundup size to the actual allocation size.
753 */
754 unsigned long
755 malloc_roundup(unsigned long size)
756 {
757
758 if (size > MAXALLOCSAVE)
759 return (roundup(size, PAGE_SIZE));
760 else
761 return (1 << BUCKETINDX(size));
762 }
763
764 /*
765 * Add a malloc type to the system.
766 */
767 void
768 malloc_type_attach(struct malloc_type *type)
769 {
770
771 if (nkmempages == 0)
772 panic("malloc_type_attach: nkmempages == 0");
773
774 if (type->ks_magic != M_MAGIC)
775 panic("malloc_type_attach: bad magic");
776
777 #ifdef DIAGNOSTIC
778 {
779 struct malloc_type *ksp;
780 for (ksp = kmemstatistics; ksp != NULL; ksp = ksp->ks_next) {
781 if (ksp == type)
782 panic("malloc_type_attach: already on list");
783 }
784 }
785 #endif
786
787 #ifdef KMEMSTATS
788 if (type->ks_limit == 0)
789 type->ks_limit = ((u_long)nkmempages << PAGE_SHIFT) * 6U / 10U;
790 #else
791 type->ks_limit = 0;
792 #endif
793
794 type->ks_next = kmemstatistics;
795 kmemstatistics = type;
796 }
797
798 /*
799 * Remove a malloc type from the system..
800 */
801 void
802 malloc_type_detach(struct malloc_type *type)
803 {
804 struct malloc_type *ksp;
805
806 #ifdef DIAGNOSTIC
807 if (type->ks_magic != M_MAGIC)
808 panic("malloc_type_detach: bad magic");
809 #endif
810
811 if (type == kmemstatistics)
812 kmemstatistics = type->ks_next;
813 else {
814 for (ksp = kmemstatistics; ksp->ks_next != NULL;
815 ksp = ksp->ks_next) {
816 if (ksp->ks_next == type) {
817 ksp->ks_next = type->ks_next;
818 break;
819 }
820 }
821 #ifdef DIAGNOSTIC
822 if (ksp->ks_next == NULL)
823 panic("malloc_type_detach: not on list");
824 #endif
825 }
826 type->ks_next = NULL;
827 }
828
829 /*
830 * Set the limit on a malloc type.
831 */
832 void
833 malloc_type_setlimit(struct malloc_type *type, u_long limit)
834 {
835 #ifdef KMEMSTATS
836 mutex_spin_enter(&malloc_lock);
837 type->ks_limit = limit;
838 mutex_spin_exit(&malloc_lock);
839 #endif
840 }
841
842 /*
843 * Compute the number of pages that kmem_map will map, that is,
844 * the size of the kernel malloc arena.
845 */
846 void
847 kmeminit_nkmempages(void)
848 {
849 int npages;
850
851 if (nkmempages != 0) {
852 /*
853 * It's already been set (by us being here before, or
854 * by patching or kernel config options), bail out now.
855 */
856 return;
857 }
858
859 npages = physmem;
860
861 if (npages > NKMEMPAGES_MAX)
862 npages = NKMEMPAGES_MAX;
863
864 if (npages < NKMEMPAGES_MIN)
865 npages = NKMEMPAGES_MIN;
866
867 nkmempages = npages;
868 }
869
870 /*
871 * Initialize the kernel memory allocator
872 */
873 void
874 kmeminit(void)
875 {
876 __link_set_decl(malloc_types, struct malloc_type);
877 struct malloc_type * const *ksp;
878 vaddr_t kmb, kml;
879 #ifdef KMEMSTATS
880 long indx;
881 #endif
882
883 #if ((MAXALLOCSAVE & (MAXALLOCSAVE - 1)) != 0)
884 ERROR!_kmeminit:_MAXALLOCSAVE_not_power_of_2
885 #endif
886 #if (MAXALLOCSAVE > MINALLOCSIZE * 32768)
887 ERROR!_kmeminit:_MAXALLOCSAVE_too_big
888 #endif
889 #if (MAXALLOCSAVE < NBPG)
890 ERROR!_kmeminit:_MAXALLOCSAVE_too_small
891 #endif
892
893 if (sizeof(struct freelist) > (1 << MINBUCKET))
894 panic("minbucket too small/struct freelist too big");
895
896 mutex_init(&malloc_lock, MUTEX_DEFAULT, IPL_VM);
897
898 /*
899 * Compute the number of kmem_map pages, if we have not
900 * done so already.
901 */
902 kmeminit_nkmempages();
903
904 kmemusage = (struct kmemusage *) uvm_km_alloc(kernel_map,
905 (vsize_t)(nkmempages * sizeof(struct kmemusage)), 0,
906 UVM_KMF_WIRED|UVM_KMF_ZERO);
907 kmb = 0;
908 kmem_map = uvm_km_suballoc(kernel_map, &kmb,
909 &kml, ((vsize_t)nkmempages << PAGE_SHIFT),
910 VM_MAP_INTRSAFE, false, &kmem_map_store);
911 uvm_km_vacache_init(kmem_map, "kvakmem", 0);
912 kmembase = (char *)kmb;
913 kmemlimit = (char *)kml;
914 #ifdef KMEMSTATS
915 for (indx = 0; indx < MINBUCKET + 16; indx++) {
916 if (1 << indx >= PAGE_SIZE)
917 kmembuckets[indx].kb_elmpercl = 1;
918 else
919 kmembuckets[indx].kb_elmpercl = PAGE_SIZE / (1 << indx);
920 kmembuckets[indx].kb_highwat =
921 5 * kmembuckets[indx].kb_elmpercl;
922 }
923 #endif
924
925 /* Attach all of the statically-linked malloc types. */
926 __link_set_foreach(ksp, malloc_types)
927 malloc_type_attach(*ksp);
928 }
929
930 #ifdef DDB
931 #include <ddb/db_output.h>
932
933 /*
934 * Dump kmem statistics from ddb.
935 *
936 * usage: call dump_kmemstats
937 */
938 void dump_kmemstats(void);
939
940 void
941 dump_kmemstats(void)
942 {
943 #ifdef KMEMSTATS
944 struct malloc_type *ksp;
945
946 for (ksp = kmemstatistics; ksp != NULL; ksp = ksp->ks_next) {
947 if (ksp->ks_memuse == 0)
948 continue;
949 db_printf("%s%.*s %ld\n", ksp->ks_shortdesc,
950 (int)(20 - strlen(ksp->ks_shortdesc)),
951 " ",
952 ksp->ks_memuse);
953 }
954 #else
955 db_printf("Kmem stats are not being collected.\n");
956 #endif /* KMEMSTATS */
957 }
958 #endif /* DDB */
959
960
961 #if 0
962 /*
963 * Diagnostic messages about "Data modified on
964 * freelist" indicate a memory corruption, but
965 * they do not help tracking it down.
966 * This function can be called at various places
967 * to sanity check malloc's freelist and discover
968 * where does the corruption take place.
969 */
970 int
971 freelist_sanitycheck(void) {
972 int i,j;
973 struct kmembuckets *kbp;
974 struct freelist *freep;
975 int rv = 0;
976
977 for (i = MINBUCKET; i <= MINBUCKET + 15; i++) {
978 kbp = &kmembuckets[i];
979 freep = (struct freelist *)kbp->kb_next;
980 j = 0;
981 while(freep) {
982 vm_map_lock(kmem_map);
983 rv = uvm_map_checkprot(kmem_map, (vaddr_t)freep,
984 (vaddr_t)freep + sizeof(struct freelist),
985 VM_PROT_WRITE);
986 vm_map_unlock(kmem_map);
987
988 if ((rv == 0) || (*(int *)freep != WEIRD_ADDR)) {
989 printf("bucket %i, chunck %d at %p modified\n",
990 i, j, freep);
991 return 1;
992 }
993 freep = (struct freelist *)freep->next;
994 j++;
995 }
996 }
997
998 return 0;
999 }
1000 #endif
1001