Rip out the old network stack. It's in the way.
[akaros.git] / kern / src / slab.c
index 92c6334..d394d70 100644 (file)
  * controlling bufctl at the top of the slab object.  Fix this with TODO (BUF).
  */
 
+#ifdef __IVY__
+#pragma nodeputy
+#pragma nosharc
+#endif
+
 #include <slab.h>
 #include <stdio.h>
 #include <assert.h>
 struct kmem_cache_list kmem_caches;
 spinlock_t kmem_caches_lock;
 
+/* Backend/internal functions, defined later.  Grab the lock before calling
+ * these. */
+static void kmem_cache_grow(struct kmem_cache *cp);
+
 /* Cache of the kmem_cache objects, needed for bootstrapping */
 struct kmem_cache kmem_cache_cache;
 struct kmem_cache *kmem_slab_cache, *kmem_bufctl_cache;
@@ -30,7 +39,7 @@ static void __kmem_cache_create(struct kmem_cache *kc, const char *name,
 {
        assert(kc);
        assert(align);
-       spinlock_init(&kc->cache_lock);
+       spinlock_init_irqsave(&kc->cache_lock);
        kc->name = name;
        kc->obj_size = obj_size;
        kc->align = align;
@@ -40,10 +49,11 @@ static void __kmem_cache_create(struct kmem_cache *kc, const char *name,
        TAILQ_INIT(&kc->empty_slab_list);
        kc->ctor = ctor;
        kc->dtor = dtor;
+       kc->nr_cur_alloc = 0;
        
        /* put in cache list based on it's size */
        struct kmem_cache *i, *prev = NULL;
-       spin_lock(&kmem_caches_lock);
+       spin_lock_irqsave(&kmem_caches_lock);
        /* find the kmem_cache before us in the list.  yes, this is O(n). */
        SLIST_FOREACH(i, &kmem_caches, link) {
                if (i->obj_size < kc->obj_size)
@@ -55,12 +65,12 @@ static void __kmem_cache_create(struct kmem_cache *kc, const char *name,
                SLIST_INSERT_AFTER(prev, kc, link);
        else
                SLIST_INSERT_HEAD(&kmem_caches, kc, link);
-       spin_unlock(&kmem_caches_lock);
+       spin_unlock_irqsave(&kmem_caches_lock);
 }
 
 void kmem_cache_init(void)
 {
-       spinlock_init(&kmem_caches_lock);
+       spinlock_init_irqsave(&kmem_caches_lock);
        SLIST_INIT(&kmem_caches);
        /* We need to call the __ version directly to bootstrap the global
         * kmem_cache_cache. */
@@ -97,13 +107,16 @@ static void kmem_slab_destroy(struct kmem_cache *cp, struct kmem_slab *a_slab)
                                buf += a_slab->obj_size;
                        }
                }
-               page_decref(kva2page(ROUNDDOWN(a_slab, PGSIZE)));
+               page_decref(kva2page((void*)ROUNDDOWN((uintptr_t)a_slab, PGSIZE)));
        } else {
                struct kmem_bufctl *i;
                void *page_start = (void*)-1;
-               // compute how many pages are allocated, given a power of two allocator
-               size_t num_pages = ROUNDUPPWR2(a_slab->num_total_obj * a_slab->obj_size)
-                                               / PGSIZE;
+               /* Figure out how much memory we asked for earlier.  We needed at least
+                * min_pgs.  We asked for the next highest order (power of 2) number of
+                * pages */
+               size_t min_pgs = ROUNDUP(NUM_BUF_PER_SLAB * a_slab->obj_size, PGSIZE) /
+                                        PGSIZE;
+               size_t order_pg_alloc = LOG2_UP(min_pgs);
                TAILQ_FOREACH(i, &a_slab->bufctl_freelist, link) {
                        // Track the lowest buffer address, which is the start of the buffer
                        page_start = MIN(page_start, i->buf_addr);
@@ -113,7 +126,7 @@ static void kmem_slab_destroy(struct kmem_cache *cp, struct kmem_slab *a_slab)
                        kmem_cache_free(kmem_bufctl_cache, i);
                }
                // free the pages for the slab's buffer
-               free_cont_pages(page_start, LOG2_UP(num_pages));
+               free_cont_pages(page_start, order_pg_alloc);
                // free the slab object
                kmem_cache_free(kmem_slab_cache, a_slab);
        }
@@ -125,7 +138,7 @@ void kmem_cache_destroy(struct kmem_cache *cp)
 {
        struct kmem_slab *a_slab, *next;
 
-       spin_lock(&cp->cache_lock);
+       spin_lock_irqsave(&cp->cache_lock);
        assert(TAILQ_EMPTY(&cp->full_slab_list));
        assert(TAILQ_EMPTY(&cp->partial_slab_list));
        /* Clean out the empty list.  We can't use a regular FOREACH here, since the
@@ -137,18 +150,18 @@ void kmem_cache_destroy(struct kmem_cache *cp)
                kmem_slab_destroy(cp, a_slab);
                a_slab = next;
        }
-       spin_lock(&kmem_caches_lock);
+       spin_lock_irqsave(&kmem_caches_lock);
        SLIST_REMOVE(&kmem_caches, cp, kmem_cache, link);
-       spin_unlock(&kmem_caches_lock);
+       spin_unlock_irqsave(&kmem_caches_lock);
        kmem_cache_free(&kmem_cache_cache, cp); 
-       spin_unlock(&cp->cache_lock);
+       spin_unlock_irqsave(&cp->cache_lock);
 }
 
 /* Front end: clients of caches use these */
 void *kmem_cache_alloc(struct kmem_cache *cp, int flags)
 {
        void *retval = NULL;
-       spin_lock(&cp->cache_lock);
+       spin_lock_irqsave(&cp->cache_lock);
        // look at partial list
        struct kmem_slab *a_slab = TAILQ_FIRST(&cp->partial_slab_list);
        //      if none, go to empty list and get an empty and make it partial
@@ -180,7 +193,8 @@ void *kmem_cache_alloc(struct kmem_cache *cp, int flags)
                TAILQ_REMOVE(&cp->partial_slab_list, a_slab, link);
                TAILQ_INSERT_HEAD(&cp->full_slab_list, a_slab, link);
        }
-       spin_unlock(&cp->cache_lock);
+       cp->nr_cur_alloc++;
+       spin_unlock_irqsave(&cp->cache_lock);
        return retval;
 }
 
@@ -195,11 +209,11 @@ void kmem_cache_free(struct kmem_cache *cp, void *buf)
        struct kmem_slab *a_slab;
        struct kmem_bufctl *a_bufctl;
 
-       spin_lock(&cp->cache_lock);
+       spin_lock_irqsave(&cp->cache_lock);
        if (cp->obj_size <= SLAB_LARGE_CUTOFF) {
                // find its slab
-               a_slab = (struct kmem_slab*)(ROUNDDOWN(buf, PGSIZE) + PGSIZE -
-                                            sizeof(struct kmem_slab));
+               a_slab = (struct kmem_slab*)(ROUNDDOWN((uintptr_t)buf, PGSIZE) +
+                                            PGSIZE - sizeof(struct kmem_slab));
                /* write location of next free small obj to the space at the end of the
                 * buffer, then list buf as the next free small obj */
                *(uintptr_t**)(buf + cp->obj_size) = a_slab->free_small_obj;
@@ -212,6 +226,7 @@ void kmem_cache_free(struct kmem_cache *cp, void *buf)
                TAILQ_INSERT_HEAD(&a_slab->bufctl_freelist, a_bufctl, link);
        }
        a_slab->num_busy_obj--;
+       cp->nr_cur_alloc--;
        // if it was full, move it to partial
        if (a_slab->num_busy_obj + 1 == a_slab->num_total_obj) {
                TAILQ_REMOVE(&cp->full_slab_list, a_slab, link);
@@ -221,7 +236,7 @@ void kmem_cache_free(struct kmem_cache *cp, void *buf)
                TAILQ_REMOVE(&cp->partial_slab_list, a_slab, link);
                TAILQ_INSERT_HEAD(&cp->empty_slab_list, a_slab, link);
        }
-       spin_unlock(&cp->cache_lock);
+       spin_unlock_irqsave(&cp->cache_lock);
 }
 
 /* Back end: internal functions */
@@ -232,17 +247,15 @@ void kmem_cache_free(struct kmem_cache *cp, void *buf)
  * Grab the cache lock before calling this.
  *
  * TODO: think about page colouring issues with kernel memory allocation. */
-void kmem_cache_grow(struct kmem_cache *cp)
+static void kmem_cache_grow(struct kmem_cache *cp)
 {
        struct kmem_slab *a_slab;
        struct kmem_bufctl *a_bufctl;
-       spin_unlock(&cp->cache_lock);
        if (cp->obj_size <= SLAB_LARGE_CUTOFF) {
                // Just get a single page for small slabs
                page_t *a_page;
-               if (page_alloc(&a_page))
-                       panic("[German Accent]: OOM!!!");
-               page_incref(a_page);
+               if (kpage_alloc(&a_page))
+                       panic("[German Accent]: OOM for a small slab growth!!!");
                // the slab struct is stored at the end of the page
                a_slab = (struct kmem_slab*)(page2kva(a_page) + PGSIZE -
                                             sizeof(struct kmem_slab));
@@ -268,13 +281,18 @@ void kmem_cache_grow(struct kmem_cache *cp)
                a_slab = kmem_cache_alloc(kmem_slab_cache, 0);
                // TODO: hash table for back reference (BUF)
                a_slab->obj_size = ROUNDUP(cp->obj_size + sizeof(uintptr_t), cp->align);
-               // alloc n pages, such that it can hold at least 8 items
-               size_t num_pgs = ROUNDUP(NUM_BUF_PER_SLAB * a_slab->obj_size, PGSIZE) /
-                                          PGSIZE;
-               // round up for the contiguous page allocator
-               void *buf = get_cont_pages(LOG2_UP(num_pgs), 0);
+               /* Figure out how much memory we want.  We need at least min_pgs.  We'll
+                * ask for the next highest order (power of 2) number of pages */
+               size_t min_pgs = ROUNDUP(NUM_BUF_PER_SLAB * a_slab->obj_size, PGSIZE) /
+                                        PGSIZE;
+               size_t order_pg_alloc = LOG2_UP(min_pgs);
+               void *buf = get_cont_pages(order_pg_alloc, 0);
+               if (!buf)
+                       panic("[German Accent]: OOM for a large slab growth!!!");
                a_slab->num_busy_obj = 0;
-               a_slab->num_total_obj = ROUNDUPPWR2(num_pgs)*PGSIZE / a_slab->obj_size;
+               /* The number of objects is based on the rounded up amt requested. */
+               a_slab->num_total_obj = ((1 << order_pg_alloc) * PGSIZE) /
+                                       a_slab->obj_size;
                TAILQ_INIT(&a_slab->bufctl_freelist);
                /* for each buffer, set up a bufctl and point to the buffer */
                for (int i = 0; i < a_slab->num_total_obj; i++) {
@@ -292,7 +310,6 @@ void kmem_cache_grow(struct kmem_cache *cp)
        }
        // add a_slab to the empty_list
        TAILQ_INSERT_HEAD(&cp->empty_slab_list, a_slab, link);
-       spin_unlock(&cp->cache_lock);
 }
 
 /* This deallocs every slab from the empty list.  TODO: think a bit more about
@@ -303,30 +320,31 @@ void kmem_cache_reap(struct kmem_cache *cp)
        struct kmem_slab *a_slab, *next;
        
        // Destroy all empty slabs.  Refer to the notes about the while loop
-       spin_lock(&cp->cache_lock);
+       spin_lock_irqsave(&cp->cache_lock);
        a_slab = TAILQ_FIRST(&cp->empty_slab_list);
        while (a_slab) {
                next = TAILQ_NEXT(a_slab, link);
                kmem_slab_destroy(cp, a_slab);
                a_slab = next;
        }
-       spin_unlock(&cp->cache_lock);
+       spin_unlock_irqsave(&cp->cache_lock);
 }
 
 void print_kmem_cache(struct kmem_cache *cp)
 {
-       spin_lock(&cp->cache_lock);
+       spin_lock_irqsave(&cp->cache_lock);
        printk("\nPrinting kmem_cache:\n---------------------\n");
        printk("Name: %s\n", cp->name);
        printk("Objsize: %d\n", cp->obj_size);
        printk("Align: %d\n", cp->align);
        printk("Flags: 0x%08x\n", cp->flags);
-       printk("Constructor: 0x%08x\n", cp->ctor);
-       printk("Destructor: 0x%08x\n", cp->dtor);
-       printk("Slab Full: 0x%08x\n", cp->full_slab_list);
-       printk("Slab Partial: 0x%08x\n", cp->partial_slab_list);
-       printk("Slab Empty: 0x%08x\n", cp->empty_slab_list);
-       spin_unlock(&cp->cache_lock);
+       printk("Constructor: %p\n", cp->ctor);
+       printk("Destructor: %p\n", cp->dtor);
+       printk("Slab Full: %p\n", cp->full_slab_list);
+       printk("Slab Partial: %p\n", cp->partial_slab_list);
+       printk("Slab Empty: %p\n", cp->empty_slab_list);
+       printk("Current Allocations: %d\n", cp->nr_cur_alloc);
+       spin_unlock_irqsave(&cp->cache_lock);
 }
 
 void print_kmem_slab(struct kmem_slab *slab)
@@ -336,10 +354,10 @@ void print_kmem_slab(struct kmem_slab *slab)
        printk("NumBusy: %d\n", slab->num_busy_obj);
        printk("Num_total: %d\n", slab->num_total_obj);
        if (slab->obj_size + sizeof(uintptr_t) < SLAB_LARGE_CUTOFF) {
-               printk("Free Small obj: 0x%08x\n", slab->free_small_obj);
+               printk("Free Small obj: %p\n", slab->free_small_obj);
                void *buf = slab->free_small_obj;
                for (int i = 0; i < slab->num_total_obj; i++) {
-                       printk("Addr of buf: 0x%08x, Addr of next: 0x%08x\n", buf,
+                       printk("Addr of buf: %p, Addr of next: %p\n", buf,
                               *((uintptr_t**)buf));
                        buf += slab->obj_size;
                }