slab: Warn when creating multiple caches with the same name
authorBarret Rhoden <brho@cs.berkeley.edu>
Tue, 9 Oct 2018 20:12:16 +0000 (16:12 -0400)
committerBarret Rhoden <brho@cs.berkeley.edu>
Tue, 9 Oct 2018 21:45:56 +0000 (17:45 -0400)
Tracing infrastructure will look up caches by name.  If there is a
collision, we will only be able to trace the first one.

Signed-off-by: Barret Rhoden <brho@cs.berkeley.edu>
kern/src/slab.c

index 60d3867..c09ec65 100644 (file)
@@ -98,6 +98,27 @@ unsigned int resize_threshold = 1;
 struct kmem_cache_tailq all_kmem_caches =
                TAILQ_HEAD_INITIALIZER(all_kmem_caches);
 
+static void kmc_track(struct kmem_cache *kc)
+{
+       struct kmem_cache *kc_i;
+
+       qlock(&arenas_and_slabs_lock);
+       TAILQ_FOREACH(kc_i, &all_kmem_caches, all_kmc_link) {
+               if (!strcmp(kc->name, kc_i->name))
+                       warn("New KMC %s created, but one with that name exists!",
+                            kc->name);
+       }
+       TAILQ_INSERT_TAIL(&all_kmem_caches, kc, all_kmc_link);
+       qunlock(&arenas_and_slabs_lock);
+}
+
+static void kmc_untrack(struct kmem_cache *kc)
+{
+       qlock(&arenas_and_slabs_lock);
+       TAILQ_REMOVE(&all_kmem_caches, kc, all_kmc_link);
+       qunlock(&arenas_and_slabs_lock);
+}
+
 /* Backend/internal functions, defined later.  Grab the lock before calling
  * these. */
 static bool kmem_cache_grow(struct kmem_cache *cp);
@@ -300,9 +321,7 @@ void __kmem_cache_create(struct kmem_cache *kc, const char *name,
         * could be creating on this call! */
        kc->pcpu_caches = build_pcpu_caches();
        add_importing_slab(kc->source, kc);
-       qlock(&arenas_and_slabs_lock);
-       TAILQ_INSERT_TAIL(&all_kmem_caches, kc, all_kmc_link);
-       qunlock(&arenas_and_slabs_lock);
+       kmc_track(kc);
 }
 
 static int __mag_ctor(void *obj, void *priv, int flags)
@@ -412,9 +431,7 @@ void kmem_cache_destroy(struct kmem_cache *cp)
 {
        struct kmem_slab *a_slab, *next;
 
-       qlock(&arenas_and_slabs_lock);
-       TAILQ_REMOVE(&all_kmem_caches, cp, all_kmc_link);
-       qunlock(&arenas_and_slabs_lock);
+       kmc_untrack(cp);
        del_importing_slab(cp->source, cp);
        drain_pcpu_caches(cp);
        depot_destroy(cp);