statfs.f_bsize => statfs.f_iosize; statfs.f_fsize => statfs.f_bsize (for SunOS)
[unix-history] / usr / src / sys / kern / kern_malloc.c
index f13544e..4f04615 100644 (file)
 /*
 /*
- * Copyright (c) 1987 Regents of the University of California.
+ * Copyright (c) 1987, 1991 The Regents of the University of California.
  * All rights reserved.
  *
  * All rights reserved.
  *
- * Redistribution and use in source and binary forms are permitted
- * provided that this notice is preserved and that due credit is given
- * to the University of California at Berkeley. The name of the University
- * may not be used to endorse or promote products derived from this
- * software without specific prior written permission. This software
- * is provided ``as is'' without express or implied warranty.
+ * %sccs.include.redist.c%
  *
  *
- *     @(#)kern_malloc.c       7.6 (Berkeley) %G%
+ *     @(#)kern_malloc.c       7.28 (Berkeley) %G%
  */
 
 #include "param.h"
  */
 
 #include "param.h"
-#include "vm.h"
-#include "cmap.h"
-#include "time.h"
 #include "proc.h"
 #include "map.h"
 #include "kernel.h"
 #include "malloc.h"
 #include "proc.h"
 #include "map.h"
 #include "kernel.h"
 #include "malloc.h"
-
-#include "../machine/pte.h"
+#include "vm/vm.h"
+#include "vm/vm_kern.h"
 
 struct kmembuckets bucket[MINBUCKET + 16];
 struct kmemstats kmemstats[M_LAST];
 struct kmemusage *kmemusage;
 
 struct kmembuckets bucket[MINBUCKET + 16];
 struct kmemstats kmemstats[M_LAST];
 struct kmemusage *kmemusage;
-long wantkmemmap;
+char *memname[] = INITKMEMNAMES;
+char *kmembase, *kmemlimit;
+char *memname[] = INITKMEMNAMES;
+long malloc_reentered;
+#define IN { if (malloc_reentered) panic("malloc reentered");\
+                       else malloc_reentered = 1;}
+#define OUT (malloc_reentered = 0)
+
+#ifdef DIAGNOSTIC
+/*
+ * This structure serves two purposes.
+ * The first is to provide a set of masks to catch unaligned frees.
+ * The second is to provide known text to copy into free objects so
+ * that modifications after frees can be detected.
+ */
+#define WEIRD_ADDR 0xdeadbeef
+long addrmask[] = { WEIRD_ADDR,
+       0x00000001, 0x00000003, 0x00000007, 0x0000000f,
+       0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff,
+       0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff,
+       0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff,
+};
+
+/*
+ * 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;
+       long    spare1;
+       short   type;
+       short   spare2;
+       caddr_t next;
+};
+#else /* !DIAGNOSTIC */
+struct freelist {
+       caddr_t next;
+};
+#endif /* DIAGNOSTIC */
 
 /*
  * Allocate a block of memory
  */
 
 /*
  * Allocate a block of memory
  */
-qaddr_t malloc(size, type, flags)
+void *
+malloc(size, type, flags)
        unsigned long size;
        unsigned long size;
-       long type, flags;
+       int type, flags;
 {
        register struct kmembuckets *kbp;
        register struct kmemusage *kup;
 {
        register struct kmembuckets *kbp;
        register struct kmemusage *kup;
-       long indx, npg, alloc, allocsize, s;
-       caddr_t va, cp;
+       register struct freelist *freep;
+       long indx, npg, alloc, allocsize;
+       int s;
+       caddr_t va, cp, rp;
 #ifdef KMEMSTATS
        register struct kmemstats *ksp = &kmemstats[type];
 #ifdef KMEMSTATS
        register struct kmemstats *ksp = &kmemstats[type];
-#endif
+
+#ifdef DIAGNOSTIC
+       if (((unsigned long)type) > M_LAST)
+               panic("malloc - bogus type");
+       if (type == M_NAMEI)
+               curproc->p_spare[0]++;
 
        indx = BUCKETINDX(size);
        kbp = &bucket[indx];
        s = splimp();
 
        indx = BUCKETINDX(size);
        kbp = &bucket[indx];
        s = splimp();
-again:
+       IN;
 #ifdef KMEMSTATS
 #ifdef KMEMSTATS
-       while (ksp->ks_inuse >= ksp->ks_limit) {
+       while (ksp->ks_memuse >= ksp->ks_limit) {
                if (flags & M_NOWAIT) {
                if (flags & M_NOWAIT) {
+                       OUT;
                        splx(s);
                        splx(s);
-                       return (0);
+                       return ((void *) NULL);
                }
                if (ksp->ks_limblocks < 65535)
                        ksp->ks_limblocks++;
                }
                if (ksp->ks_limblocks < 65535)
                        ksp->ks_limblocks++;
-               sleep((caddr_t)ksp, PSWP+2);
+               OUT;
+               tsleep((caddr_t)ksp, PSWP+2, memname[type], 0);
+               IN;
        }
        }
+#endif
+#ifdef DIAGNOSTIC
+       copysize = 1 << indx < sizeof addrmask ? 1 << indx : sizeof addrmask;
 #endif
        if (kbp->kb_next == NULL) {
                if (size > MAXALLOCSAVE)
 #endif
        if (kbp->kb_next == NULL) {
                if (size > MAXALLOCSAVE)
@@ -64,28 +110,13 @@ again:
                else
                        allocsize = 1 << indx;
                npg = clrnd(btoc(allocsize));
                else
                        allocsize = 1 << indx;
                npg = clrnd(btoc(allocsize));
-               if ((flags & M_NOWAIT) && freemem < npg) {
+               va = (caddr_t) kmem_malloc(kmem_map, (vm_size_t)ctob(npg),
+                                          !(flags & M_NOWAIT));
+               if (va == NULL) {
+                       OUT;
                        splx(s);
                        splx(s);
-                       return (0);
-               }
-               alloc = rmalloc(kmemmap, npg);
-               if (alloc == 0) {
-                       if (flags & M_NOWAIT) {
-                               splx(s);
-                               return (0);
-                       }
-#ifdef KMEMSTATS
-                       if (ksp->ks_mapblocks < 65535)
-                               ksp->ks_mapblocks++;
-#endif
-                       wantkmemmap++;
-                       sleep((caddr_t)&wantkmemmap, PSWP+2);
-                       goto again;
+                       return ((void *) NULL);
                }
                }
-               alloc -= CLSIZE;                /* convert to base 0 */
-               (void) vmemall(&kmempt[alloc], npg, &proc[0], CSYS);
-               va = (caddr_t) kmemxtob(alloc);
-               vmaccess(&kmempt[alloc], va, npg);
 #ifdef KMEMSTATS
                kbp->kb_total += kbp->kb_elmpercl;
 #endif
 #ifdef KMEMSTATS
                kbp->kb_total += kbp->kb_elmpercl;
 #endif
@@ -104,13 +135,29 @@ again:
                kup->ku_freecnt = kbp->kb_elmpercl;
                kbp->kb_totalfree += kbp->kb_elmpercl;
 #endif
                kup->ku_freecnt = kbp->kb_elmpercl;
                kbp->kb_totalfree += kbp->kb_elmpercl;
 #endif
+               /*
+                * 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;
+               rp = kbp->kb_next; /* returned while blocked in vmemall */
                kbp->kb_next = va + (npg * NBPG) - allocsize;
                kbp->kb_next = va + (npg * NBPG) - allocsize;
-               for (cp = kbp->kb_next; cp > va; cp -= allocsize)
-                       *(caddr_t *)cp = cp - allocsize;
-               *(caddr_t *)cp = NULL;
+               for (cp = kbp->kb_next; cp >= va; cp -= allocsize) {
+                       ((caddr_t *)cp)[2] = (cp > va ? cp - allocsize : rp);
+                       if (indx == 7) {
+                               long *lp = (long *)cp;
+                               lp[0] = lp[1] = lp[3] = lp[4] = -1;
+                       }
+               }
        }
        va = kbp->kb_next;
        }
        va = kbp->kb_next;
-       kbp->kb_next = *(caddr_t *)va;
+       kbp->kb_next = ((caddr_t *)va)[2];
+       if (indx == 7) {
+               long *lp = (long *)va;
+               if (lp[0] != -1 || lp[1] != -1 || lp[3] != -1 || lp[4] != -1)
+                       panic("malloc meddled");
+       }
 #ifdef KMEMSTATS
        kup = btokup(va);
        if (kup->ku_indx != indx)
 #ifdef KMEMSTATS
        kup = btokup(va);
        if (kup->ku_indx != indx)
@@ -124,51 +171,129 @@ out:
        kbp->kb_calls++;
        ksp->ks_inuse++;
        ksp->ks_calls++;
        kbp->kb_calls++;
        ksp->ks_inuse++;
        ksp->ks_calls++;
-       if (ksp->ks_inuse > ksp->ks_maxused)
-               ksp->ks_maxused = ksp->ks_inuse;
+       if (ksp->ks_memuse > ksp->ks_maxused)
+               ksp->ks_maxused = ksp->ks_memuse;
 #else
 out:
 #endif
 #else
 out:
 #endif
+       OUT;
        splx(s);
        splx(s);
-       return ((qaddr_t)va);
+       return ((void *) va);
 }
 
 }
 
+#ifdef DIAGNOSTIC
+long addrmask[] = { 0x00000000,
+       0x00000001, 0x00000003, 0x00000007, 0x0000000f,
+       0x0000001f, 0x0000003f, 0x0000007f, 0x000000ff,
+       0x000001ff, 0x000003ff, 0x000007ff, 0x00000fff,
+       0x00001fff, 0x00003fff, 0x00007fff, 0x0000ffff,
+};
+#endif /* DIAGNOSTIC */
+
 /*
  * Free a block of memory allocated by malloc.
  */
 /*
  * Free a block of memory allocated by malloc.
  */
-void free(addr, type)
-       caddr_t addr;
-       long type;
+void
+free(addr, type)
+       void *addr;
+       int type;
 {
        register struct kmembuckets *kbp;
        register struct kmemusage *kup;
 {
        register struct kmembuckets *kbp;
        register struct kmemusage *kup;
-       long alloc, s;
+       register struct freelist *freep;
+       long size;
+       int s;
+#ifdef DIAGNOSTIC
+       caddr_t cp;
+       long alloc, copysize;
+#endif
 #ifdef KMEMSTATS
        register struct kmemstats *ksp = &kmemstats[type];
 #endif
 
        kup = btokup(addr);
 #ifdef KMEMSTATS
        register struct kmemstats *ksp = &kmemstats[type];
 #endif
 
        kup = btokup(addr);
+       size = 1 << kup->ku_indx;
+#ifdef DIAGNOSTIC
+       if (size > NBPG * CLSIZE)
+               alloc = addrmask[BUCKETINDX(NBPG * CLSIZE)];
+       else
+               alloc = addrmask[kup->ku_indx];
+       if (((u_long)addr & alloc) != 0) {
+               printf("free: unaligned addr 0x%x, size %d, type %d, mask %d\n",
+                       addr, size, type, alloc);
+               panic("free: unaligned addr");
+       }
+#endif /* DIAGNOSTIC */
+       size = 1 << kup->ku_indx;
+       kbp = &bucket[kup->ku_indx];
        s = splimp();
        s = splimp();
-       if (1 << kup->ku_indx > MAXALLOCSAVE) {
-               alloc = btokmemx(addr);
-               (void) memfree(&kmempt[alloc], kup->ku_pagecnt, 0);
-               rmfree(kmemmap, (long)kup->ku_pagecnt, alloc + CLSIZE);
-               if (wantkmemmap) {
-                       wakeup((caddr_t)&wantkmemmap);
-                       wantkmemmap = 0;
-               }
+#ifdef DIAGNOSTIC
+       /*
+        * Check for returns of data that do not point to the
+        * beginning of the allocation.
+        */
+       if (type == M_NAMEI)
+               curproc->p_spare[0]--;
+       if (size > NBPG * CLSIZE)
+               alloc = addrmask[BUCKETINDX(NBPG * CLSIZE)];
+       else
+               alloc = addrmask[kup->ku_indx];
+       if (((u_long)addr & alloc) != 0) {
+               printf("free: unaligned addr 0x%x, size %d, type %d, mask %d\n",
+                       addr, size, type, alloc);
+               panic("free: unaligned addr");
+       }
+#endif /* DIAGNOSTIC */
+       IN;
+       if (size > MAXALLOCSAVE) {
+               kmem_free(kmem_map, (vm_offset_t)addr, ctob(kup->ku_pagecnt));
 #ifdef KMEMSTATS
 #ifdef KMEMSTATS
-               ksp->ks_memuse -= kup->ku_pagecnt << PGSHIFT;
+               size = kup->ku_pagecnt << PGSHIFT;
+               ksp->ks_memuse -= size;
                kup->ku_indx = 0;
                kup->ku_pagecnt = 0;
                kup->ku_indx = 0;
                kup->ku_pagecnt = 0;
-               if (ksp->ks_inuse == ksp->ks_limit)
+               if (ksp->ks_memuse + size >= ksp->ks_limit &&
+                   ksp->ks_memuse < ksp->ks_limit)
                        wakeup((caddr_t)ksp);
                ksp->ks_inuse--;
                        wakeup((caddr_t)ksp);
                ksp->ks_inuse--;
+               kbp->kb_total -= 1;
 #endif
                splx(s);
                return;
        }
 #endif
                splx(s);
                return;
        }
-       kbp = &bucket[kup->ku_indx];
+       freep = (struct freelist *)addr;
+#ifdef DIAGNOSTIC
+       /*
+        * Check for multiple frees. Use a quick check to see if
+        * it looks free before laboriously searching the freelist.
+        */
+       copysize = size < sizeof addrmask ? size : sizeof addrmask;
+       if (freep->spare0 == WEIRD_ADDR) {
+               freep->type = ((struct freelist *)addrmask)->type;
+               freep->next = ((struct freelist *)addrmask)->next;
+               if (!bcmp(addrmask, addr, copysize)) {
+                       for (cp = kbp->kb_next; cp; cp = *(caddr_t *)cp) {
+                               if (addr == cp) {
+                                       printf("multiply freed item 0x%x\n",
+                                           addr);
+                                       panic("free: duplicated free");
+                               }
+                       }
+               }
+       }
+       /*
+        * 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.
+        */
+       bcopy(addrmask, addr, copysize);
+       freep->type = type;
+#endif /* DIAGNOSTIC */
+       if (size == 128) {
+               long *lp = (long *)addr;
+               lp[0] = lp[1] = lp[3] = lp[4] = -1;
+       }
 #ifdef KMEMSTATS
        kup->ku_freecnt++;
        if (kup->ku_freecnt >= kbp->kb_elmpercl)
 #ifdef KMEMSTATS
        kup->ku_freecnt++;
        if (kup->ku_freecnt >= kbp->kb_elmpercl)
@@ -177,13 +302,15 @@ void free(addr, type)
                else if (kbp->kb_totalfree > kbp->kb_highwat)
                        kbp->kb_couldfree++;
        kbp->kb_totalfree++;
                else if (kbp->kb_totalfree > kbp->kb_highwat)
                        kbp->kb_couldfree++;
        kbp->kb_totalfree++;
-       if (ksp->ks_inuse == ksp->ks_limit)
+       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--;
                wakeup((caddr_t)ksp);
        ksp->ks_inuse--;
-       ksp->ks_memuse -= 1 << kup->ku_indx;
 #endif
 #endif
-       *(caddr_t *)addr = kbp->kb_next;
+       ((caddr_t *)addr)[2] = kbp->kb_next;
        kbp->kb_next = addr;
        kbp->kb_next = addr;
+       OUT;
        splx(s);
 }
 
        splx(s);
 }
 
@@ -193,15 +320,22 @@ void free(addr, type)
 kmeminit()
 {
        register long indx;
 kmeminit()
 {
        register long indx;
+       int npg;
 
 
-       if (!powerof2(MAXALLOCSAVE))
-               panic("kmeminit: MAXALLOCSAVE not power of 2");
-       if (MAXALLOCSAVE > MINALLOCSIZE * 32768)
-               panic("kmeminit: MAXALLOCSAVE too big");
-       if (MAXALLOCSAVE < CLBYTES)
-               panic("kmeminit: MAXALLOCSAVE too small");
-       rminit(kmemmap, ekmempt - kmempt, (long)CLSIZE,
-               "malloc map", ekmempt - kmempt);
+#if    ((MAXALLOCSAVE & (MAXALLOCSAVE - 1)) != 0)
+               ERROR!_kmeminit:_MAXALLOCSAVE_not_power_of_2
+#endif
+#if    (MAXALLOCSAVE > MINALLOCSIZE * 32768)
+               ERROR!_kmeminit:_MAXALLOCSAVE_too_big
+#endif
+#if    (MAXALLOCSAVE < CLBYTES)
+               ERROR!_kmeminit:_MAXALLOCSAVE_too_small
+#endif
+       npg = VM_KMEM_SIZE/ NBPG;
+       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_offset_t)&kmemlimit, (vm_size_t)(npg * NBPG), FALSE);
 #ifdef KMEMSTATS
        for (indx = 0; indx < MINBUCKET + 16; indx++) {
                if (1 << indx >= CLBYTES)
 #ifdef KMEMSTATS
        for (indx = 0; indx < MINBUCKET + 16; indx++) {
                if (1 << indx >= CLBYTES)
@@ -211,7 +345,6 @@ kmeminit()
                bucket[indx].kb_highwat = 5 * bucket[indx].kb_elmpercl;
        }
        for (indx = 0; indx < M_LAST; indx++)
                bucket[indx].kb_highwat = 5 * bucket[indx].kb_elmpercl;
        }
        for (indx = 0; indx < M_LAST; indx++)
-               kmemstats[indx].ks_limit =
-                       (ekmempt - kmempt) * CLBYTES * 9 / 10;
+               kmemstats[indx].ks_limit = npg * NBPG * 6 / 10;
 #endif
 }
 #endif
 }