]> git.proxmox.com Git - mirror_spl.git/blobdiff - module/splat/splat-kmem.c
Fix vmem_size()
[mirror_spl.git] / module / splat / splat-kmem.c
index e3b6a781c42a2e4af4886a422d6534acee748f89..102a76ea817b824de3cab362b68dd31b7ddb801b 100644 (file)
@@ -6,7 +6,7 @@
  *  UCRL-CODE-235197
  *
  *  This file is part of the SPL, Solaris Porting Layer.
- *  For details, see <http://github.com/behlendorf/spl/>.
+ *  For details, see <http://zfsonlinux.org/>.
  *
  *  The SPL is free software; you can redistribute it and/or modify it
  *  under the terms of the GNU General Public License as published by the
  *  Solaris Porting LAyer Tests (SPLAT) Kmem Tests.
 \*****************************************************************************/
 
+#include <sys/kmem.h>
+#include <sys/kmem_cache.h>
+#include <sys/vmem.h>
+#include <sys/random.h>
+#include <sys/thread.h>
+#include <sys/vmsystm.h>
 #include "splat-internal.h"
 
 #define SPLAT_KMEM_NAME                        "kmem"
 #define SPLAT_KMEM_TEST10_NAME         "slab_lock"
 #define SPLAT_KMEM_TEST10_DESC         "Slab locking test"
 
-#ifdef _LP64
+#if 0
 #define SPLAT_KMEM_TEST11_ID           0x010b
 #define SPLAT_KMEM_TEST11_NAME         "slab_overcommit"
 #define SPLAT_KMEM_TEST11_DESC         "Slab memory overcommit test"
-#endif /* _LP64 */
+#endif
 
-#define SPLAT_KMEM_TEST12_ID           0x010c
-#define SPLAT_KMEM_TEST12_NAME         "vmem_size"
-#define SPLAT_KMEM_TEST12_DESC         "Memory zone test"
+#define SPLAT_KMEM_TEST13_ID           0x010d
+#define SPLAT_KMEM_TEST13_NAME         "slab_reclaim"
+#define SPLAT_KMEM_TEST13_DESC         "Slab direct memory reclaim test"
 
 #define SPLAT_KMEM_ALLOC_COUNT         10
 #define SPLAT_VMEM_ALLOC_COUNT         10
@@ -90,11 +96,11 @@ splat_kmem_test1(struct file *file, void *arg)
        int size = PAGE_SIZE;
        int i, count, rc = 0;
 
-       while ((!rc) && (size <= (PAGE_SIZE * 32))) {
+       while ((!rc) && (size <= spl_kmem_alloc_warn)) {
                count = 0;
 
                for (i = 0; i < SPLAT_KMEM_ALLOC_COUNT; i++) {
-                       ptr[i] = kmem_alloc(size, KM_SLEEP | __GFP_NOWARN);
+                       ptr[i] = kmem_alloc(size, KM_SLEEP);
                        if (ptr[i])
                                count++;
                }
@@ -122,11 +128,11 @@ splat_kmem_test2(struct file *file, void *arg)
        int size = PAGE_SIZE;
        int i, j, count, rc = 0;
 
-       while ((!rc) && (size <= (PAGE_SIZE * 32))) {
+       while ((!rc) && (size <= spl_kmem_alloc_warn)) {
                count = 0;
 
                for (i = 0; i < SPLAT_KMEM_ALLOC_COUNT; i++) {
-                       ptr[i] = kmem_zalloc(size, KM_SLEEP | __GFP_NOWARN);
+                       ptr[i] = kmem_zalloc(size, KM_SLEEP);
                        if (ptr[i])
                                count++;
                }
@@ -166,7 +172,11 @@ splat_kmem_test3(struct file *file, void *arg)
        int size = PAGE_SIZE;
        int i, count, rc = 0;
 
-       while ((!rc) && (size <= (PAGE_SIZE * 1024))) {
+       /*
+        * Test up to 4x the maximum kmem_alloc() size to ensure both
+        * the kmem_alloc() and vmem_alloc() call paths are used.
+        */
+       while ((!rc) && (size <= (4 * spl_kmem_alloc_max))) {
                count = 0;
 
                for (i = 0; i < SPLAT_VMEM_ALLOC_COUNT; i++) {
@@ -198,7 +208,11 @@ splat_kmem_test4(struct file *file, void *arg)
        int size = PAGE_SIZE;
        int i, j, count, rc = 0;
 
-       while ((!rc) && (size <= (PAGE_SIZE * 1024))) {
+       /*
+        * Test up to 4x the maximum kmem_zalloc() size to ensure both
+        * the kmem_zalloc() and vmem_zalloc() call paths are used.
+        */
+       while ((!rc) && (size <= (4 * spl_kmem_alloc_max))) {
                count = 0;
 
                for (i = 0; i < SPLAT_VMEM_ALLOC_COUNT; i++) {
@@ -238,23 +252,22 @@ splat_kmem_test4(struct file *file, void *arg)
 #define SPLAT_KMEM_TEST_MAGIC          0x004488CCUL
 #define SPLAT_KMEM_CACHE_NAME          "kmem_test"
 #define SPLAT_KMEM_OBJ_COUNT           1024
-#define SPLAT_KMEM_OBJ_RECLAIM         20 /* percent */
+#define SPLAT_KMEM_OBJ_RECLAIM         32 /* objects */
 #define SPLAT_KMEM_THREADS             32
 
 #define KCP_FLAG_READY                 0x01
 
 typedef struct kmem_cache_data {
        unsigned long kcd_magic;
+       struct list_head kcd_node;
        int kcd_flag;
        char kcd_buf[0];
 } kmem_cache_data_t;
 
 typedef struct kmem_cache_thread {
-       kmem_cache_t *kct_cache;
        spinlock_t kct_lock;
        int kct_id;
-       int kct_kcd_count;
-       kmem_cache_data_t *kct_kcd[0];
+       struct list_head kct_list;
 } kmem_cache_thread_t;
 
 typedef struct kmem_cache_priv {
@@ -272,18 +285,15 @@ typedef struct kmem_cache_priv {
        int kcp_count;
        int kcp_alloc;
        int kcp_rc;
-       int kcp_kcd_count;
-       kmem_cache_data_t *kcp_kcd[0];
 } kmem_cache_priv_t;
 
 static kmem_cache_priv_t *
 splat_kmem_cache_test_kcp_alloc(struct file *file, char *name,
-                               int size, int align, int alloc, int count)
+                               int size, int align, int alloc)
 {
        kmem_cache_priv_t *kcp;
 
-       kcp = vmem_zalloc(sizeof(kmem_cache_priv_t) +
-                         count * sizeof(kmem_cache_data_t *), KM_SLEEP);
+       kcp = kmem_zalloc(sizeof(kmem_cache_priv_t), KM_SLEEP);
        if (!kcp)
                return NULL;
 
@@ -300,7 +310,6 @@ splat_kmem_cache_test_kcp_alloc(struct file *file, char *name,
        kcp->kcp_count = 0;
        kcp->kcp_alloc = alloc;
        kcp->kcp_rc = 0;
-       kcp->kcp_kcd_count = count;
 
        return kcp;
 }
@@ -308,34 +317,112 @@ splat_kmem_cache_test_kcp_alloc(struct file *file, char *name,
 static void
 splat_kmem_cache_test_kcp_free(kmem_cache_priv_t *kcp)
 {
-       vmem_free(kcp, sizeof(kmem_cache_priv_t) +
-                 kcp->kcp_kcd_count * sizeof(kmem_cache_data_t *));
+       kmem_free(kcp, sizeof(kmem_cache_priv_t));
 }
 
 static kmem_cache_thread_t *
-splat_kmem_cache_test_kct_alloc(int id, int count)
+splat_kmem_cache_test_kct_alloc(kmem_cache_priv_t *kcp, int id)
 {
        kmem_cache_thread_t *kct;
 
-       ASSERTF(id < SPLAT_KMEM_THREADS, "id=%d\n", id);
-       kct = vmem_zalloc(sizeof(kmem_cache_thread_t) +
-                         count * sizeof(kmem_cache_data_t *), KM_SLEEP);
+       ASSERT3S(id, <, SPLAT_KMEM_THREADS);
+       ASSERT(kcp->kcp_kct[id] == NULL);
+
+       kct = kmem_zalloc(sizeof(kmem_cache_thread_t), KM_SLEEP);
        if (!kct)
                return NULL;
 
        spin_lock_init(&kct->kct_lock);
-       kct->kct_cache = NULL;
        kct->kct_id = id;
-       kct->kct_kcd_count = count;
+       INIT_LIST_HEAD(&kct->kct_list);
+
+       spin_lock(&kcp->kcp_lock);
+       kcp->kcp_kct[id] = kct;
+       spin_unlock(&kcp->kcp_lock);
 
        return kct;
 }
 
 static void
-splat_kmem_cache_test_kct_free(kmem_cache_thread_t *kct)
+splat_kmem_cache_test_kct_free(kmem_cache_priv_t *kcp,
+                              kmem_cache_thread_t *kct)
+{
+       spin_lock(&kcp->kcp_lock);
+       kcp->kcp_kct[kct->kct_id] = NULL;
+       spin_unlock(&kcp->kcp_lock);
+
+       kmem_free(kct, sizeof(kmem_cache_thread_t));
+}
+
+static void
+splat_kmem_cache_test_kcd_free(kmem_cache_priv_t *kcp,
+                              kmem_cache_thread_t *kct)
 {
-       vmem_free(kct, sizeof(kmem_cache_thread_t) +
-                 kct->kct_kcd_count * sizeof(kmem_cache_data_t *));
+       kmem_cache_data_t *kcd;
+
+       spin_lock(&kct->kct_lock);
+       while (!list_empty(&kct->kct_list)) {
+               kcd = list_entry(kct->kct_list.next,
+                                kmem_cache_data_t, kcd_node);
+               list_del(&kcd->kcd_node);
+               spin_unlock(&kct->kct_lock);
+
+               kmem_cache_free(kcp->kcp_cache, kcd);
+
+               spin_lock(&kct->kct_lock);
+       }
+       spin_unlock(&kct->kct_lock);
+}
+
+static int
+splat_kmem_cache_test_kcd_alloc(kmem_cache_priv_t *kcp,
+                               kmem_cache_thread_t *kct, int count)
+{
+       kmem_cache_data_t *kcd;
+       int i;
+
+       for (i = 0; i < count; i++) {
+               kcd = kmem_cache_alloc(kcp->kcp_cache, KM_SLEEP);
+               if (kcd == NULL) {
+                       splat_kmem_cache_test_kcd_free(kcp, kct);
+                       return -ENOMEM;
+               }
+
+               spin_lock(&kct->kct_lock);
+               list_add_tail(&kcd->kcd_node, &kct->kct_list);
+               spin_unlock(&kct->kct_lock);
+       }
+
+       return 0;
+}
+
+static void
+splat_kmem_cache_test_debug(struct file *file, char *name,
+                           kmem_cache_priv_t *kcp)
+{
+       int j;
+
+       splat_vprint(file, name, "%s cache objects %d",
+            kcp->kcp_cache->skc_name, kcp->kcp_count);
+
+       if (kcp->kcp_cache->skc_flags & (KMC_KMEM | KMC_VMEM)) {
+               splat_vprint(file, name, ", slabs %u/%u objs %u/%u",
+                    (unsigned)kcp->kcp_cache->skc_slab_alloc,
+                    (unsigned)kcp->kcp_cache->skc_slab_total,
+                    (unsigned)kcp->kcp_cache->skc_obj_alloc,
+                    (unsigned)kcp->kcp_cache->skc_obj_total);
+
+               if (!(kcp->kcp_cache->skc_flags & KMC_NOMAGAZINE)) {
+                       splat_vprint(file, name, "%s", "mags");
+
+                       for_each_online_cpu(j)
+                               splat_print(file, "%u/%u ",
+                                    kcp->kcp_cache->skc_mag[j]->skm_avail,
+                                    kcp->kcp_cache->skc_mag[j]->skm_size);
+               }
+       }
+
+       splat_print(file, "%s\n", "");
 }
 
 static int
@@ -346,6 +433,7 @@ splat_kmem_cache_test_constructor(void *ptr, void *priv, int flags)
 
        if (kcd && kcp) {
                kcd->kcd_magic = kcp->kcp_magic;
+               INIT_LIST_HEAD(&kcd->kcd_node);
                kcd->kcd_flag = 1;
                memset(kcd->kcd_buf, 0xaa, kcp->kcp_size - (sizeof *kcd));
                kcp->kcp_count++;
@@ -380,51 +468,41 @@ splat_kmem_cache_test_reclaim(void *priv)
 {
        kmem_cache_priv_t *kcp = (kmem_cache_priv_t *)priv;
        kmem_cache_thread_t *kct;
-       int i, j, count;
+       kmem_cache_data_t *kcd;
+       LIST_HEAD(reclaim);
+       int i, count;
 
        ASSERT(kcp->kcp_magic == SPLAT_KMEM_TEST_MAGIC);
-       count = kcp->kcp_kcd_count * SPLAT_KMEM_OBJ_RECLAIM / 100;
 
-       /* Objects directly attached to the kcp */
+       /* For each kct thread reclaim some objects */
        spin_lock(&kcp->kcp_lock);
-       for (i = 0; i < kcp->kcp_kcd_count; i++) {
-               if (kcp->kcp_kcd[i]) {
-                       kmem_cache_free(kcp->kcp_cache, kcp->kcp_kcd[i]);
-                       kcp->kcp_kcd[i] = NULL;
-
-                       if ((--count) == 0)
-                               break;
-               }
-       }
-       spin_unlock(&kcp->kcp_lock);
-
-       /* No threads containing objects to consider */
-       if (kcp->kcp_kct_count == -1)
-               return;
-
-       /* Objects attached to a kct thread */
-       for (i = 0; i < kcp->kcp_kct_count; i++) {
-               spin_lock(&kcp->kcp_lock);
+       for (i = 0; i < SPLAT_KMEM_THREADS; i++) {
                kct = kcp->kcp_kct[i];
-               if (!kct) {
-                       spin_unlock(&kcp->kcp_lock);
+               if (!kct)
                        continue;
-               }
 
+               spin_unlock(&kcp->kcp_lock);
                spin_lock(&kct->kct_lock);
-               count = kct->kct_kcd_count * SPLAT_KMEM_OBJ_RECLAIM / 100;
 
-               for (j = 0; j < kct->kct_kcd_count; j++) {
-                       if (kct->kct_kcd[j]) {
-                               kmem_cache_free(kcp->kcp_cache,kct->kct_kcd[j]);
-                               kct->kct_kcd[j] = NULL;
-
-                               if ((--count) == 0)
-                                       break;
-                       }
+               count = SPLAT_KMEM_OBJ_RECLAIM;
+               while (count > 0 && !list_empty(&kct->kct_list)) {
+                       kcd = list_entry(kct->kct_list.next,
+                                        kmem_cache_data_t, kcd_node);
+                       list_del(&kcd->kcd_node);
+                       list_add(&kcd->kcd_node, &reclaim);
+                       count--;
                }
+
                spin_unlock(&kct->kct_lock);
-               spin_unlock(&kcp->kcp_lock);
+               spin_lock(&kcp->kcp_lock);
+       }
+       spin_unlock(&kcp->kcp_lock);
+
+       /* Freed outside the spin lock */
+       while (!list_empty(&reclaim)) {
+               kcd = list_entry(reclaim.next, kmem_cache_data_t, kcd_node);
+               list_del(&kcd->kcd_node);
+               kmem_cache_free(kcp->kcp_cache, kcd);
        }
 
        return;
@@ -459,8 +537,7 @@ splat_kmem_cache_test_thread(void *arg)
 {
        kmem_cache_priv_t *kcp = (kmem_cache_priv_t *)arg;
        kmem_cache_thread_t *kct;
-       int rc = 0, id, i;
-       void *obj;
+       int rc = 0, id;
 
        ASSERT(kcp->kcp_magic == SPLAT_KMEM_TEST_MAGIC);
 
@@ -473,16 +550,12 @@ splat_kmem_cache_test_thread(void *arg)
        kcp->kcp_kct_count++;
        spin_unlock(&kcp->kcp_lock);
 
-       kct = splat_kmem_cache_test_kct_alloc(id, kcp->kcp_alloc);
+       kct = splat_kmem_cache_test_kct_alloc(kcp, id);
        if (!kct) {
                rc = -ENOMEM;
                goto out;
        }
 
-       spin_lock(&kcp->kcp_lock);
-       kcp->kcp_kct[id] = kct;
-       spin_unlock(&kcp->kcp_lock);
-
        /* Wait for all threads to have started and report they are ready */
        if (kcp->kcp_kct_count == SPLAT_KMEM_THREADS)
                wake_up(&kcp->kcp_ctl_waitq);
@@ -490,34 +563,14 @@ splat_kmem_cache_test_thread(void *arg)
        wait_event(kcp->kcp_thr_waitq,
                splat_kmem_cache_test_flags(kcp, KCP_FLAG_READY));
 
-       /*
-        * Updates to kct->kct_kcd[] are performed under a spin_lock so
-        * they may safely run concurrent with the reclaim function.  If
-        * we are not in a low memory situation we have one lock per-
-        * thread so they are not expected to be contended.
-        */
-       for (i = 0; i < kct->kct_kcd_count; i++) {
-               obj = kmem_cache_alloc(kcp->kcp_cache, KM_SLEEP);
-               spin_lock(&kct->kct_lock);
-               kct->kct_kcd[i] = obj;
-               spin_unlock(&kct->kct_lock);
-       }
-
-       for (i = 0; i < kct->kct_kcd_count; i++) {
-               spin_lock(&kct->kct_lock);
-               if (kct->kct_kcd[i]) {
-                       kmem_cache_free(kcp->kcp_cache, kct->kct_kcd[i]);
-                       kct->kct_kcd[i] = NULL;
-               }
-               spin_unlock(&kct->kct_lock);
-       }
+       /* Create and destroy objects */
+       rc = splat_kmem_cache_test_kcd_alloc(kcp, kct, kcp->kcp_alloc);
+       splat_kmem_cache_test_kcd_free(kcp, kct);
 out:
-       spin_lock(&kcp->kcp_lock);
-       if (kct) {
-               splat_kmem_cache_test_kct_free(kct);
-               kcp->kcp_kct[id] = kct = NULL;
-       }
+       if (kct)
+               splat_kmem_cache_test_kct_free(kcp, kct);
 
+       spin_lock(&kcp->kcp_lock);
        if (!kcp->kcp_rc)
                kcp->kcp_rc = rc;
 
@@ -531,90 +584,119 @@ out:
 
 static int
 splat_kmem_cache_test(struct file *file, void *arg, char *name,
-                     int size, int align, int flags)
+    int size, int align, int flags)
 {
-       kmem_cache_priv_t *kcp;
-       kmem_cache_data_t *kcd;
-       int rc = 0, max;
+       kmem_cache_priv_t *kcp = NULL;
+       kmem_cache_data_t **kcd = NULL;
+       int i, rc = 0, objs = 0;
 
-       kcp = splat_kmem_cache_test_kcp_alloc(file, name, size, align, 0, 1);
+       /* Limit size for low memory machines (1/128 of memory) */
+       size = MIN(size, (physmem * PAGE_SIZE) >> 7);
+
+       splat_vprint(file, name,
+           "Testing size=%d, align=%d, flags=0x%04x\n",
+           size, align, flags);
+
+       kcp = splat_kmem_cache_test_kcp_alloc(file, name, size, align, 0);
        if (!kcp) {
                splat_vprint(file, name, "Unable to create '%s'\n", "kcp");
-               return -ENOMEM;
+               return (-ENOMEM);
        }
 
-       kcp->kcp_kcd[0] = NULL;
-       kcp->kcp_cache =
-               kmem_cache_create(SPLAT_KMEM_CACHE_NAME,
-                                 kcp->kcp_size, kcp->kcp_align,
-                                 splat_kmem_cache_test_constructor,
-                                 splat_kmem_cache_test_destructor,
-                                 NULL, kcp, NULL, flags);
-       if (!kcp->kcp_cache) {
-               splat_vprint(file, name,
-                            "Unable to create '%s'\n",
-                            SPLAT_KMEM_CACHE_NAME);
+       kcp->kcp_cache = kmem_cache_create(SPLAT_KMEM_CACHE_NAME,
+           kcp->kcp_size, kcp->kcp_align,
+           splat_kmem_cache_test_constructor,
+           splat_kmem_cache_test_destructor,
+           NULL, kcp, NULL, flags);
+       if (kcp->kcp_cache == NULL) {
+               splat_vprint(file, name, "Unable to create "
+                   "name='%s', size=%d, align=%d, flags=0x%x\n",
+                   SPLAT_KMEM_CACHE_NAME, size, align, flags);
                rc = -ENOMEM;
                goto out_free;
        }
 
-       kcd = kmem_cache_alloc(kcp->kcp_cache, KM_SLEEP);
-       if (!kcd) {
-               splat_vprint(file, name,
-                            "Unable to allocate from '%s'\n",
-                            SPLAT_KMEM_CACHE_NAME);
-               rc = -EINVAL;
+       /*
+        * Allocate several slabs worth of objects to verify functionality.
+        * However, on 32-bit systems with limited address space constrain
+        * it to a single slab for the purposes of this test.
+        */
+#ifdef _LP64
+       objs = kcp->kcp_cache->skc_slab_objs * 4;
+#else
+       objs = 1;
+#endif
+       kcd = kmem_zalloc(sizeof (kmem_cache_data_t *) * objs, KM_SLEEP);
+       if (kcd == NULL) {
+               splat_vprint(file, name, "Unable to allocate pointers "
+                   "for %d objects\n", objs);
+               rc = -ENOMEM;
                goto out_free;
        }
-       spin_lock(&kcp->kcp_lock);
-       kcp->kcp_kcd[0] = kcd;
-       spin_unlock(&kcp->kcp_lock);
 
-       if (!kcp->kcp_kcd[0]->kcd_flag) {
-               splat_vprint(file, name,
-                            "Failed to run contructor for '%s'\n",
-                            SPLAT_KMEM_CACHE_NAME);
-               rc = -EINVAL;
-               goto out_free;
-       }
+       for (i = 0; i < objs; i++) {
+               kcd[i] = kmem_cache_alloc(kcp->kcp_cache, KM_SLEEP);
+               if (kcd[i] == NULL) {
+                       splat_vprint(file, name, "Unable to allocate "
+                           "from '%s'\n", SPLAT_KMEM_CACHE_NAME);
+                       rc = -EINVAL;
+                       goto out_free;
+               }
 
-       if (kcp->kcp_kcd[0]->kcd_magic != kcp->kcp_magic) {
-               splat_vprint(file, name,
-                            "Failed to pass private data to constructor "
-                            "for '%s'\n", SPLAT_KMEM_CACHE_NAME);
-               rc = -EINVAL;
-               goto out_free;
+               if (!kcd[i]->kcd_flag) {
+                       splat_vprint(file, name, "Failed to run constructor "
+                           "for '%s'\n", SPLAT_KMEM_CACHE_NAME);
+                       rc = -EINVAL;
+                       goto out_free;
+               }
+
+               if (kcd[i]->kcd_magic != kcp->kcp_magic) {
+                       splat_vprint(file, name,
+                           "Failed to pass private data to constructor "
+                           "for '%s'\n", SPLAT_KMEM_CACHE_NAME);
+                       rc = -EINVAL;
+                       goto out_free;
+               }
        }
 
-       max = kcp->kcp_count;
-       spin_lock(&kcp->kcp_lock);
-       kmem_cache_free(kcp->kcp_cache, kcp->kcp_kcd[0]);
-       kcp->kcp_kcd[0] = NULL;
-       spin_unlock(&kcp->kcp_lock);
+       for (i = 0; i < objs; i++) {
+               kmem_cache_free(kcp->kcp_cache, kcd[i]);
+
+               /* Destructors are run for every kmem_cache_free() */
+               if (kcd[i]->kcd_flag) {
+                       splat_vprint(file, name,
+                           "Failed to run destructor for '%s'\n",
+                           SPLAT_KMEM_CACHE_NAME);
+                       rc = -EINVAL;
+                       goto out_free;
+               }
+       }
 
-       /* Destroy the entire cache which will force destructors to
-        * run and we can verify one was called for every object */
-       kmem_cache_destroy(kcp->kcp_cache);
        if (kcp->kcp_count) {
                splat_vprint(file, name,
-                            "Failed to run destructor on all slab objects "
-                            "for '%s'\n", SPLAT_KMEM_CACHE_NAME);
+                   "Failed to run destructor on all slab objects for '%s'\n",
+                   SPLAT_KMEM_CACHE_NAME);
                rc = -EINVAL;
        }
 
+       kmem_free(kcd, sizeof (kmem_cache_data_t *) * objs);
+       kmem_cache_destroy(kcp->kcp_cache);
+
        splat_kmem_cache_test_kcp_free(kcp);
        splat_vprint(file, name,
-                    "Successfully ran ctors/dtors for %d elements in '%s'\n",
-                    max, SPLAT_KMEM_CACHE_NAME);
+           "Success ran alloc'd/free'd %d objects of size %d\n",
+           objs, size);
 
-       return rc;
+       return (rc);
 
 out_free:
-       if (kcp->kcp_kcd[0]) {
-               spin_lock(&kcp->kcp_lock);
-               kmem_cache_free(kcp->kcp_cache, kcp->kcp_kcd[0]);
-               kcp->kcp_kcd[0] = NULL;
-               spin_unlock(&kcp->kcp_lock);
+       if (kcd) {
+               for (i = 0; i < objs; i++) {
+                       if (kcd[i] != NULL)
+                               kmem_cache_free(kcp->kcp_cache, kcd[i]);
+               }
+
+               kmem_free(kcd, sizeof (kmem_cache_data_t *) * objs);
        }
 
        if (kcp->kcp_cache)
@@ -622,7 +704,7 @@ out_free:
 
        splat_kmem_cache_test_kcp_free(kcp);
 
-       return rc;
+       return (rc);
 }
 
 static int
@@ -635,7 +717,7 @@ splat_kmem_cache_thread_test(struct file *file, void *arg, char *name,
        char cache_name[32];
        int i, rc = 0;
 
-       kcp = splat_kmem_cache_test_kcp_alloc(file, name, size, 0, alloc, 0);
+       kcp = splat_kmem_cache_test_kcp_alloc(file, name, size, 0, alloc);
        if (!kcp) {
                splat_vprint(file, name, "Unable to create '%s'\n", "kcp");
                return -ENOMEM;
@@ -655,12 +737,12 @@ splat_kmem_cache_thread_test(struct file *file, void *arg, char *name,
                goto out_kcp;
        }
 
-       start = current_kernel_time();
+       getnstimeofday(&start);
 
        for (i = 0; i < SPLAT_KMEM_THREADS; i++) {
                thr = thread_create(NULL, 0,
                                    splat_kmem_cache_test_thread,
-                                   kcp, 0, &p0, TS_RUN, minclsyspri);
+                                   kcp, 0, &p0, TS_RUN, defclsyspri);
                if (thr == NULL) {
                        rc = -ESRCH;
                        goto out_cache;
@@ -680,7 +762,7 @@ splat_kmem_cache_thread_test(struct file *file, void *arg, char *name,
        /* Sleep until all thread have finished */
        wait_event(kcp->kcp_ctl_waitq, splat_kmem_cache_test_threads(kcp, 0));
 
-       stop = current_kernel_time();
+       getnstimeofday(&stop);
        delta = timespec_sub(stop, start);
 
        splat_vprint(file, name,
@@ -716,46 +798,129 @@ static int
 splat_kmem_test5(struct file *file, void *arg)
 {
        char *name = SPLAT_KMEM_TEST5_NAME;
-       int rc;
+       int i, rc = 0;
+
+       /* Randomly pick small object sizes and alignments. */
+       for (i = 0; i < 100; i++) {
+               int size, align, flags = 0;
+               uint32_t rnd;
+
+               /* Evenly distribute tests over all value cache types */
+               get_random_bytes((void *)&rnd, sizeof (uint32_t));
+               switch (rnd & 0x03) {
+               default:
+               case 0x00:
+                       flags = 0;
+                       break;
+               case 0x01:
+                       flags = KMC_KMEM;
+                       break;
+               case 0x02:
+                       flags = KMC_VMEM;
+                       break;
+               case 0x03:
+                       flags = KMC_SLAB;
+                       break;
+               }
 
-       rc = splat_kmem_cache_test(file, arg, name, 128, 0, 0);
-       if (rc)
-               return rc;
+               /* The following flags are set with a 1/10 chance */
+               flags |= ((((rnd >> 8) % 10) == 0) ? KMC_OFFSLAB : 0);
+               flags |= ((((rnd >> 16) % 10) == 0) ? KMC_NOEMERGENCY : 0);
 
-       rc = splat_kmem_cache_test(file, arg, name, 128, 0, KMC_KMEM);
-       if (rc)
-               return rc;
+               /* 32b - PAGE_SIZE */
+               get_random_bytes((void *)&rnd, sizeof (uint32_t));
+               size = MAX(rnd % (PAGE_SIZE + 1), 32);
 
-       return splat_kmem_cache_test(file, arg, name, 128, 0, KMC_VMEM);
+               /* 2^N where (3 <= N <= PAGE_SHIFT) */
+               get_random_bytes((void *)&rnd, sizeof (uint32_t));
+               align = (1 << MAX(3, rnd % (PAGE_SHIFT + 1)));
+
+               rc = splat_kmem_cache_test(file, arg, name, size, align, flags);
+               if (rc)
+                       return (rc);
+       }
+
+       return (rc);
 }
 
-/* Validate large object cache behavior for dynamic/kmem/vmem caches */
+/*
+ * Validate large object cache behavior for dynamic/kmem/vmem caches
+ */
 static int
 splat_kmem_test6(struct file *file, void *arg)
 {
        char *name = SPLAT_KMEM_TEST6_NAME;
-       int rc;
+       int i, max_size, rc = 0;
+
+       /* Randomly pick large object sizes and alignments. */
+       for (i = 0; i < 100; i++) {
+               int size, align, flags = 0;
+               uint32_t rnd;
+
+               /* Evenly distribute tests over all value cache types */
+               get_random_bytes((void *)&rnd, sizeof (uint32_t));
+               switch (rnd & 0x03) {
+               default:
+               case 0x00:
+                       flags = 0;
+                       max_size = (SPL_KMEM_CACHE_MAX_SIZE * 1024 * 1024) / 2;
+                       break;
+               case 0x01:
+                       flags = KMC_KMEM;
+                       max_size = (SPL_MAX_ORDER_NR_PAGES - 2) * PAGE_SIZE;
+                       break;
+               case 0x02:
+                       flags = KMC_VMEM;
+                       max_size = (SPL_KMEM_CACHE_MAX_SIZE * 1024 * 1024) / 2;
+                       break;
+               case 0x03:
+                       flags = KMC_SLAB;
+                       max_size = SPL_MAX_KMEM_ORDER_NR_PAGES * PAGE_SIZE;
+                       break;
+               }
+
+               /* The following flags are set with a 1/10 chance */
+               flags |= ((((rnd >> 8) % 10) == 0) ? KMC_OFFSLAB : 0);
+               flags |= ((((rnd >> 16) % 10) == 0) ? KMC_NOEMERGENCY : 0);
 
-       rc = splat_kmem_cache_test(file, arg, name, 256*1024, 0, 0);
-       if (rc)
-               return rc;
+               /* PAGE_SIZE - max_size */
+               get_random_bytes((void *)&rnd, sizeof (uint32_t));
+               size = MAX(rnd % (max_size + 1), PAGE_SIZE),
 
-       rc = splat_kmem_cache_test(file, arg, name, 64*1024, 0, KMC_KMEM);
-       if (rc)
-               return rc;
+               /* 2^N where (3 <= N <= PAGE_SHIFT) */
+               get_random_bytes((void *)&rnd, sizeof (uint32_t));
+               align = (1 << MAX(3, rnd % (PAGE_SHIFT + 1)));
 
-       return splat_kmem_cache_test(file, arg, name, 1024*1024, 0, KMC_VMEM);
+               rc = splat_kmem_cache_test(file, arg, name, size, align, flags);
+               if (rc)
+                       return (rc);
+       }
+
+       return (rc);
 }
 
-/* Validate object alignment cache behavior for caches */
+/*
+ * Validate object alignment cache behavior for caches
+ */
 static int
 splat_kmem_test7(struct file *file, void *arg)
 {
        char *name = SPLAT_KMEM_TEST7_NAME;
+       int max_size = (SPL_KMEM_CACHE_MAX_SIZE * 1024 * 1024) / 2;
        int i, rc;
 
        for (i = SPL_KMEM_CACHE_ALIGN; i <= PAGE_SIZE; i *= 2) {
-               rc = splat_kmem_cache_test(file, arg, name, 157, i, 0);
+               uint32_t size;
+
+               get_random_bytes((void *)&size, sizeof (uint32_t));
+               size = MAX(size % (max_size + 1), 32);
+
+               rc = splat_kmem_cache_test(file, arg, name, size, i, 0);
+               if (rc)
+                       return rc;
+
+               rc = splat_kmem_cache_test(file, arg, name, size, i,
+                   KMC_OFFSLAB);
                if (rc)
                        return rc;
        }
@@ -763,19 +928,36 @@ splat_kmem_test7(struct file *file, void *arg)
        return rc;
 }
 
+/*
+ * Validate kmem_cache_reap() by requesting the slab cache free any objects
+ * it can.  For a few reasons this may not immediately result in more free
+ * memory even if objects are freed.  First off, due to fragmentation we
+ * may not be able to reclaim any slabs.  Secondly, even if we do we fully
+ * clear some slabs we will not want to immediately reclaim all of them
+ * because we may contend with cache allocations and thrash.  What we want
+ * to see is the slab size decrease more gradually as it becomes clear they
+ * will not be needed.  This should be achievable in less than a minute.
+ * If it takes longer than this something has gone wrong.
+ */
 static int
 splat_kmem_test8(struct file *file, void *arg)
 {
        kmem_cache_priv_t *kcp;
-       kmem_cache_data_t *kcd;
-       int i, j, rc = 0;
+       kmem_cache_thread_t *kct;
+       unsigned int spl_kmem_cache_expire_old;
+       int i, rc = 0;
+
+       /* Enable cache aging just for this test if it is disabled */
+       spl_kmem_cache_expire_old = spl_kmem_cache_expire;
+       spl_kmem_cache_expire = KMC_EXPIRE_AGE;
 
        kcp = splat_kmem_cache_test_kcp_alloc(file, SPLAT_KMEM_TEST8_NAME,
-                                             256, 0, 0, SPLAT_KMEM_OBJ_COUNT);
+                                             256, 0, 0);
        if (!kcp) {
                splat_vprint(file, SPLAT_KMEM_TEST8_NAME,
                             "Unable to create '%s'\n", "kcp");
-               return -ENOMEM;
+               rc = -ENOMEM;
+               goto out;
        }
 
        kcp->kcp_cache =
@@ -785,59 +967,40 @@ splat_kmem_test8(struct file *file, void *arg)
                                  splat_kmem_cache_test_reclaim,
                                  kcp, NULL, 0);
        if (!kcp->kcp_cache) {
-               splat_kmem_cache_test_kcp_free(kcp);
                splat_vprint(file, SPLAT_KMEM_TEST8_NAME,
                           "Unable to create '%s'\n", SPLAT_KMEM_CACHE_NAME);
-               return -ENOMEM;
+               rc = -ENOMEM;
+               goto out_kcp;
        }
 
-       for (i = 0; i < SPLAT_KMEM_OBJ_COUNT; i++) {
-               kcd = kmem_cache_alloc(kcp->kcp_cache, KM_SLEEP);
-               spin_lock(&kcp->kcp_lock);
-               kcp->kcp_kcd[i] = kcd;
-               spin_unlock(&kcp->kcp_lock);
-               if (!kcd) {
-                       splat_vprint(file, SPLAT_KMEM_TEST8_NAME,
-                                  "Unable to allocate from '%s'\n",
-                                  SPLAT_KMEM_CACHE_NAME);
-               }
+       kct = splat_kmem_cache_test_kct_alloc(kcp, 0);
+       if (!kct) {
+               splat_vprint(file, SPLAT_KMEM_TEST8_NAME,
+                            "Unable to create '%s'\n", "kct");
+               rc = -ENOMEM;
+               goto out_cache;
        }
 
-       /* Request the slab cache free any objects it can.  For a few reasons
-        * this may not immediately result in more free memory even if objects
-        * are freed.  First off, due to fragmentation we may not be able to
-        * reclaim any slabs.  Secondly, even if we do we fully clear some
-        * slabs we will not want to immedately reclaim all of them because
-        * we may contend with cache allocs and thrash.  What we want to see
-        * is the slab size decrease more gradually as it becomes clear they
-        * will not be needed.  This should be acheivable in less than minute
-        * if it takes longer than this something has gone wrong.
-        */
-       for (i = 0; i < 60; i++) {
-               kmem_cache_reap_now(kcp->kcp_cache);
-               splat_vprint(file, SPLAT_KMEM_TEST8_NAME,
-                            "%s cache objects %d, slabs %u/%u objs %u/%u mags ",
-                            SPLAT_KMEM_CACHE_NAME, kcp->kcp_count,
-                           (unsigned)kcp->kcp_cache->skc_slab_alloc,
-                           (unsigned)kcp->kcp_cache->skc_slab_total,
-                           (unsigned)kcp->kcp_cache->skc_obj_alloc,
-                           (unsigned)kcp->kcp_cache->skc_obj_total);
-
-               for_each_online_cpu(j)
-                       splat_print(file, "%u/%u ",
-                                    kcp->kcp_cache->skc_mag[j]->skm_avail,
-                                    kcp->kcp_cache->skc_mag[j]->skm_size);
+       rc = splat_kmem_cache_test_kcd_alloc(kcp, kct, SPLAT_KMEM_OBJ_COUNT);
+       if (rc) {
+               splat_vprint(file, SPLAT_KMEM_TEST8_NAME, "Unable to "
+                            "allocate from '%s'\n", SPLAT_KMEM_CACHE_NAME);
+               goto out_kct;
+       }
 
-               splat_print(file, "%s\n", "");
+       /* Force reclaim every 1/10 a second for 60 seconds. */
+       for (i = 0; i < 600; i++) {
+               kmem_cache_reap_now(kcp->kcp_cache);
+               splat_kmem_cache_test_debug(file, SPLAT_KMEM_TEST8_NAME, kcp);
 
-               if (kcp->kcp_cache->skc_obj_total == 0)
+               if (kcp->kcp_count == 0)
                        break;
 
                set_current_state(TASK_INTERRUPTIBLE);
-               schedule_timeout(HZ);
+               schedule_timeout(HZ / 10);
        }
 
-       if (kcp->kcp_cache->skc_obj_total == 0) {
+       if (kcp->kcp_count == 0) {
                splat_vprint(file, SPLAT_KMEM_TEST8_NAME,
                        "Successfully created %d objects "
                        "in cache %s and reclaimed them\n",
@@ -845,37 +1008,52 @@ splat_kmem_test8(struct file *file, void *arg)
        } else {
                splat_vprint(file, SPLAT_KMEM_TEST8_NAME,
                        "Failed to reclaim %u/%d objects from cache %s\n",
-                       (unsigned)kcp->kcp_cache->skc_obj_total,
+                       (unsigned)kcp->kcp_count,
                        SPLAT_KMEM_OBJ_COUNT, SPLAT_KMEM_CACHE_NAME);
                rc = -ENOMEM;
        }
 
        /* Cleanup our mess (for failure case of time expiring) */
-       spin_lock(&kcp->kcp_lock);
-       for (i = 0; i < SPLAT_KMEM_OBJ_COUNT; i++)
-               if (kcp->kcp_kcd[i])
-                       kmem_cache_free(kcp->kcp_cache, kcp->kcp_kcd[i]);
-       spin_unlock(&kcp->kcp_lock);
-
+       splat_kmem_cache_test_kcd_free(kcp, kct);
+out_kct:
+       splat_kmem_cache_test_kct_free(kcp, kct);
+out_cache:
        kmem_cache_destroy(kcp->kcp_cache);
+out_kcp:
        splat_kmem_cache_test_kcp_free(kcp);
+out:
+       spl_kmem_cache_expire = spl_kmem_cache_expire_old;
 
        return rc;
 }
 
+/* Test cache aging, we have allocated a large number of objects thus
+ * creating a large number of slabs and then free'd them all.  However,
+ * since there should be little memory pressure at the moment those
+ * slabs have not been freed.  What we want to see is the slab size
+ * decrease gradually as it becomes clear they will not be be needed.
+ * This should be achievable in less than minute.  If it takes longer
+ * than this something has gone wrong.
+ */
 static int
 splat_kmem_test9(struct file *file, void *arg)
 {
        kmem_cache_priv_t *kcp;
-       kmem_cache_data_t *kcd;
-       int i, j, rc = 0, count = SPLAT_KMEM_OBJ_COUNT * 128;
+       kmem_cache_thread_t *kct;
+       unsigned int spl_kmem_cache_expire_old;
+       int i, rc = 0, count = SPLAT_KMEM_OBJ_COUNT * 128;
+
+       /* Enable cache aging just for this test if it is disabled */
+       spl_kmem_cache_expire_old = spl_kmem_cache_expire;
+       spl_kmem_cache_expire = KMC_EXPIRE_AGE;
 
        kcp = splat_kmem_cache_test_kcp_alloc(file, SPLAT_KMEM_TEST9_NAME,
-                                             256, 0, 0, count);
+                                             256, 0, 0);
        if (!kcp) {
                splat_vprint(file, SPLAT_KMEM_TEST9_NAME,
                             "Unable to create '%s'\n", "kcp");
-               return -ENOMEM;
+               rc = -ENOMEM;
+               goto out;
        }
 
        kcp->kcp_cache =
@@ -884,62 +1062,40 @@ splat_kmem_test9(struct file *file, void *arg)
                                  splat_kmem_cache_test_destructor,
                                  NULL, kcp, NULL, 0);
        if (!kcp->kcp_cache) {
-               splat_kmem_cache_test_kcp_free(kcp);
                splat_vprint(file, SPLAT_KMEM_TEST9_NAME,
                           "Unable to create '%s'\n", SPLAT_KMEM_CACHE_NAME);
-               return -ENOMEM;
+               rc = -ENOMEM;
+               goto out_kcp;
        }
 
-       for (i = 0; i < count; i++) {
-               kcd = kmem_cache_alloc(kcp->kcp_cache, KM_SLEEP);
-               spin_lock(&kcp->kcp_lock);
-               kcp->kcp_kcd[i] = kcd;
-               spin_unlock(&kcp->kcp_lock);
-               if (!kcd) {
-                       splat_vprint(file, SPLAT_KMEM_TEST9_NAME,
-                                  "Unable to allocate from '%s'\n",
-                                  SPLAT_KMEM_CACHE_NAME);
-               }
+       kct = splat_kmem_cache_test_kct_alloc(kcp, 0);
+       if (!kct) {
+               splat_vprint(file, SPLAT_KMEM_TEST8_NAME,
+                            "Unable to create '%s'\n", "kct");
+               rc = -ENOMEM;
+               goto out_cache;
        }
 
-       spin_lock(&kcp->kcp_lock);
-       for (i = 0; i < count; i++)
-               if (kcp->kcp_kcd[i])
-                       kmem_cache_free(kcp->kcp_cache, kcp->kcp_kcd[i]);
-       spin_unlock(&kcp->kcp_lock);
+       rc = splat_kmem_cache_test_kcd_alloc(kcp, kct, count);
+       if (rc) {
+               splat_vprint(file, SPLAT_KMEM_TEST9_NAME, "Unable to "
+                            "allocate from '%s'\n", SPLAT_KMEM_CACHE_NAME);
+               goto out_kct;
+       }
 
-       /* We have allocated a large number of objects thus creating a
-        * large number of slabs and then free'd them all.  However since
-        * there should be little memory pressure at the moment those
-        * slabs have not been freed.  What we want to see is the slab
-        * size decrease gradually as it becomes clear they will not be
-        * be needed.  This should be acheivable in less than minute
-        * if it takes longer than this something has gone wrong.
-        */
-       for (i = 0; i < 60; i++) {
-               splat_vprint(file, SPLAT_KMEM_TEST9_NAME,
-                            "%s cache objects %d, slabs %u/%u objs %u/%u mags ",
-                            SPLAT_KMEM_CACHE_NAME, kcp->kcp_count,
-                           (unsigned)kcp->kcp_cache->skc_slab_alloc,
-                           (unsigned)kcp->kcp_cache->skc_slab_total,
-                           (unsigned)kcp->kcp_cache->skc_obj_alloc,
-                           (unsigned)kcp->kcp_cache->skc_obj_total);
-
-               for_each_online_cpu(j)
-                       splat_print(file, "%u/%u ",
-                                    kcp->kcp_cache->skc_mag[j]->skm_avail,
-                                    kcp->kcp_cache->skc_mag[j]->skm_size);
+       splat_kmem_cache_test_kcd_free(kcp, kct);
 
-               splat_print(file, "%s\n", "");
+       for (i = 0; i < 60; i++) {
+               splat_kmem_cache_test_debug(file, SPLAT_KMEM_TEST9_NAME, kcp);
 
-               if (kcp->kcp_cache->skc_obj_total == 0)
+               if (kcp->kcp_count == 0)
                        break;
 
                set_current_state(TASK_INTERRUPTIBLE);
                schedule_timeout(HZ);
        }
 
-       if (kcp->kcp_cache->skc_obj_total == 0) {
+       if (kcp->kcp_count == 0) {
                splat_vprint(file, SPLAT_KMEM_TEST9_NAME,
                        "Successfully created %d objects "
                        "in cache %s and reclaimed them\n",
@@ -947,13 +1103,19 @@ splat_kmem_test9(struct file *file, void *arg)
        } else {
                splat_vprint(file, SPLAT_KMEM_TEST9_NAME,
                        "Failed to reclaim %u/%d objects from cache %s\n",
-                       (unsigned)kcp->kcp_cache->skc_obj_total, count,
+                       (unsigned)kcp->kcp_count, count,
                        SPLAT_KMEM_CACHE_NAME);
                rc = -ENOMEM;
        }
 
+out_kct:
+       splat_kmem_cache_test_kct_free(kcp, kct);
+out_cache:
        kmem_cache_destroy(kcp->kcp_cache);
+out_kcp:
        splat_kmem_cache_test_kcp_free(kcp);
+out:
+       spl_kmem_cache_expire = spl_kmem_cache_expire_old;
 
        return rc;
 }
@@ -969,9 +1131,15 @@ splat_kmem_test9(struct file *file, void *arg)
 static int
 splat_kmem_test10(struct file *file, void *arg)
 {
-       uint64_t size, alloc, rc = 0;
+       uint64_t size, alloc, maxsize, limit, rc = 0;
 
-       for (size = 16; size <= 1024*1024; size *= 2) {
+#if defined(CONFIG_64BIT)
+       maxsize = (1024 * 1024);
+#else
+       maxsize = (128 * 1024);
+#endif
+
+       for (size = 32; size <= maxsize; size *= 2) {
 
                splat_vprint(file, SPLAT_KMEM_TEST10_NAME, "%-22s  %s", "name",
                             "time (sec)\tslabs       \tobjs    \thash\n");
@@ -980,9 +1148,10 @@ splat_kmem_test10(struct file *file, void *arg)
 
                for (alloc = 1; alloc <= 1024; alloc *= 2) {
 
-                       /* Skip tests which exceed available memory.  We
-                        * leverage availrmem here for some extra testing */
-                       if (size * alloc * SPLAT_KMEM_THREADS > availrmem / 2)
+                       /* Skip tests which exceed 1/2 of memory. */
+                       limit = MIN(physmem * PAGE_SIZE,
+                           vmem_size(NULL, VMEM_ALLOC | VMEM_FREE)) / 2;
+                       if (size * alloc * SPLAT_KMEM_THREADS > limit)
                                continue;
 
                        rc = splat_kmem_cache_thread_test(file, arg,
@@ -995,7 +1164,7 @@ splat_kmem_test10(struct file *file, void *arg)
        return rc;
 }
 
-#ifdef _LP64
+#if 0
 /*
  * This test creates N threads with a shared kmem cache which overcommits
  * memory by 4x.  This makes it impossible for the slab to satify the
@@ -1013,7 +1182,7 @@ splat_kmem_test11(struct file *file, void *arg)
 {
        uint64_t size, alloc, rc;
 
-       size = 256*1024;
+       size = 8 * 1024;
        alloc = ((4 * physmem * PAGE_SIZE) / size) / SPLAT_KMEM_THREADS;
 
        splat_vprint(file, SPLAT_KMEM_TEST11_NAME, "%-22s  %s", "name",
@@ -1026,84 +1195,144 @@ splat_kmem_test11(struct file *file, void *arg)
 
        return rc;
 }
-#endif /* _LP64 */
+#endif
+
+typedef struct dummy_page {
+       struct list_head dp_list;
+       char             dp_pad[PAGE_SIZE - sizeof(struct list_head)];
+} dummy_page_t;
 
 /*
- * Check vmem_size() behavior by acquiring the alloc/free/total vmem
- * space, then allocate a known buffer size from vmem space.  We can
- * then check that vmem_size() values were updated properly with in
- * a fairly small tolerence.  The tolerance is important because we
- * are not the only vmem consumer on the system.  Other unrelated
- * allocations might occur during the small test window.  The vmem
- * allocation itself may also add in a little extra private space to
- * the buffer.  Finally, verify total space always remains unchanged.
+ * This test is designed to verify that direct reclaim is functioning as
+ * expected.  We allocate a large number of objects thus creating a large
+ * number of slabs.  We then apply memory pressure and expect that the
+ * direct reclaim path can easily recover those slabs.  The registered
+ * reclaim function will free the objects and the slab shrinker will call
+ * it repeatedly until at least a single slab can be freed.
+ *
+ * Note it may not be possible to reclaim every last slab via direct reclaim
+ * without a failure because the shrinker_rwsem may be contended.  For this
+ * reason, quickly reclaiming 3/4 of the slabs is considered a success.
+ *
+ * This should all be possible within 10 seconds.  For reference, on a
+ * system with 2G of memory this test takes roughly 0.2 seconds to run.
+ * It may take longer on larger memory systems but should still easily
+ * complete in the alloted 10 seconds.
  */
 static int
-splat_kmem_test12(struct file *file, void *arg)
+splat_kmem_test13(struct file *file, void *arg)
 {
-       size_t alloc1, free1, total1;
-       size_t alloc2, free2, total2;
-       int size = 8*1024*1024;
-       void *ptr;
-
-       alloc1 = vmem_size(NULL, VMEM_ALLOC);
-       free1  = vmem_size(NULL, VMEM_FREE);
-       total1 = vmem_size(NULL, VMEM_ALLOC | VMEM_FREE);
-       splat_vprint(file, SPLAT_KMEM_TEST12_NAME, "Vmem alloc=%lu "
-                    "free=%lu total=%lu\n", (unsigned long)alloc1,
-                    (unsigned long)free1, (unsigned long)total1);
-
-       splat_vprint(file, SPLAT_KMEM_TEST12_NAME, "Alloc %d bytes\n", size);
-       ptr = vmem_alloc(size, KM_SLEEP);
-       if (!ptr) {
-               splat_vprint(file, SPLAT_KMEM_TEST12_NAME,
-                            "Failed to alloc %d bytes\n", size);
-               return -ENOMEM;
+       kmem_cache_priv_t *kcp;
+       kmem_cache_thread_t *kct;
+       dummy_page_t *dp;
+       struct list_head list;
+       struct timespec start, stop, delta = { 0, 0 };
+       int size, count, slabs, fails = 0;
+       int i, rc = 0, max_time = 10;
+
+       size = 128 * 1024;
+       count = MIN(physmem * PAGE_SIZE, vmem_size(NULL,
+           VMEM_ALLOC | VMEM_FREE)) / 4 / size;
+
+       kcp = splat_kmem_cache_test_kcp_alloc(file, SPLAT_KMEM_TEST13_NAME,
+                                             size, 0, 0);
+       if (!kcp) {
+               splat_vprint(file, SPLAT_KMEM_TEST13_NAME,
+                            "Unable to create '%s'\n", "kcp");
+               rc = -ENOMEM;
+               goto out;
        }
 
-       alloc2 = vmem_size(NULL, VMEM_ALLOC);
-       free2  = vmem_size(NULL, VMEM_FREE);
-       total2 = vmem_size(NULL, VMEM_ALLOC | VMEM_FREE);
-       splat_vprint(file, SPLAT_KMEM_TEST12_NAME, "Vmem alloc=%lu "
-                    "free=%lu total=%lu\n", (unsigned long)alloc2,
-                    (unsigned long)free2, (unsigned long)total2);
-
-       splat_vprint(file, SPLAT_KMEM_TEST12_NAME, "Free %d bytes\n", size);
-       vmem_free(ptr, size);
-       if (alloc2 < (alloc1 + size - (size / 100)) ||
-           alloc2 > (alloc1 + size + (size / 100))) {
-               splat_vprint(file, SPLAT_KMEM_TEST12_NAME, "Failed "
-                            "VMEM_ALLOC size: %lu != %lu+%d (+/- 1%%)\n",
-                            (unsigned long)alloc2,(unsigned long)alloc1,size);
-               return -ERANGE;
+       kcp->kcp_cache =
+               kmem_cache_create(SPLAT_KMEM_CACHE_NAME, kcp->kcp_size, 0,
+                                 splat_kmem_cache_test_constructor,
+                                 splat_kmem_cache_test_destructor,
+                                 splat_kmem_cache_test_reclaim,
+                                 kcp, NULL, 0);
+       if (!kcp->kcp_cache) {
+               splat_vprint(file, SPLAT_KMEM_TEST13_NAME,
+                            "Unable to create '%s'\n", SPLAT_KMEM_CACHE_NAME);
+               rc = -ENOMEM;
+               goto out_kcp;
        }
 
-       if (free2 < (free1 - size - (size / 100)) ||
-           free2 > (free1 - size + (size / 100))) {
-               splat_vprint(file, SPLAT_KMEM_TEST12_NAME, "Failed "
-                            "VMEM_FREE size: %lu != %lu-%d (+/- 1%%)\n",
-                            (unsigned long)free2, (unsigned long)free1, size);
-               return -ERANGE;
+       kct = splat_kmem_cache_test_kct_alloc(kcp, 0);
+       if (!kct) {
+               splat_vprint(file, SPLAT_KMEM_TEST13_NAME,
+                            "Unable to create '%s'\n", "kct");
+               rc = -ENOMEM;
+               goto out_cache;
        }
 
-       if (total1 != total2) {
-               splat_vprint(file, SPLAT_KMEM_TEST12_NAME, "Failed "
-                            "VMEM_ALLOC | VMEM_FREE not constant: "
-                            "%lu != %lu\n", (unsigned long)total2,
-                            (unsigned long)total1);
-               return -ERANGE;
+       rc = splat_kmem_cache_test_kcd_alloc(kcp, kct, count);
+       if (rc) {
+               splat_vprint(file, SPLAT_KMEM_TEST13_NAME, "Unable to "
+                            "allocate from '%s'\n", SPLAT_KMEM_CACHE_NAME);
+               goto out_kct;
        }
 
-       splat_vprint(file, SPLAT_KMEM_TEST12_NAME,
-                    "VMEM_ALLOC within tolerance: ~%ld%% (%ld/%d)\n",
-                    (long)abs(alloc1 + (long)size - alloc2) * 100 / (long)size,
-                    (long)abs(alloc1 + (long)size - alloc2), size);
-       splat_vprint(file, SPLAT_KMEM_TEST12_NAME,
-                    "VMEM_FREE within tolerance:  ~%ld%% (%ld/%d)\n",
-                    (long)abs((free1 - (long)size) - free2) * 100 / (long)size,
-                    (long)abs((free1 - (long)size) - free2), size);
+       i = 0;
+       slabs = kcp->kcp_cache->skc_slab_total;
+       INIT_LIST_HEAD(&list);
+       getnstimeofday(&start);
+
+       /* Apply memory pressure */
+       while (kcp->kcp_cache->skc_slab_total > (slabs >> 2)) {
+
+               if ((i % 10000) == 0)
+                       splat_kmem_cache_test_debug(
+                           file, SPLAT_KMEM_TEST13_NAME, kcp);
+
+               getnstimeofday(&stop);
+               delta = timespec_sub(stop, start);
+               if (delta.tv_sec >= max_time) {
+                       splat_vprint(file, SPLAT_KMEM_TEST13_NAME,
+                                    "Failed to reclaim 3/4 of cache in %ds, "
+                                    "%u/%u slabs remain\n", max_time,
+                                    (unsigned)kcp->kcp_cache->skc_slab_total,
+                                    slabs);
+                       rc = -ETIME;
+                       break;
+               }
 
-       return 0;
+               dp = (dummy_page_t *)__get_free_page(GFP_KERNEL);
+               if (!dp) {
+                       fails++;
+                       splat_vprint(file, SPLAT_KMEM_TEST13_NAME,
+                                    "Failed (%d) to allocate page with %u "
+                                    "slabs still in the cache\n", fails,
+                                    (unsigned)kcp->kcp_cache->skc_slab_total);
+                       continue;
+               }
+
+               list_add(&dp->dp_list, &list);
+               i++;
+       }
+
+       if (rc == 0)
+               splat_vprint(file, SPLAT_KMEM_TEST13_NAME,
+                            "Successfully created %u slabs and with %d alloc "
+                            "failures reclaimed 3/4 of them in %d.%03ds\n",
+                            slabs, fails,
+                            (int)delta.tv_sec, (int)delta.tv_nsec / 1000000);
+
+       /* Release memory pressure pages */
+       while (!list_empty(&list)) {
+               dp = list_entry(list.next, dummy_page_t, dp_list);
+               list_del_init(&dp->dp_list);
+               free_page((unsigned long)dp);
+       }
+
+       /* Release remaining kmem cache objects */
+       splat_kmem_cache_test_kcd_free(kcp, kct);
+out_kct:
+       splat_kmem_cache_test_kct_free(kcp, kct);
+out_cache:
+       kmem_cache_destroy(kcp->kcp_cache);
+out_kcp:
+       splat_kmem_cache_test_kcp_free(kcp);
+out:
+       return rc;
 }
 
 splat_subsystem_t *
@@ -1143,12 +1372,12 @@ splat_kmem_init(void)
                        SPLAT_KMEM_TEST9_ID, splat_kmem_test9);
        SPLAT_TEST_INIT(sub, SPLAT_KMEM_TEST10_NAME, SPLAT_KMEM_TEST10_DESC,
                        SPLAT_KMEM_TEST10_ID, splat_kmem_test10);
-#ifdef _LP64
+#if 0
        SPLAT_TEST_INIT(sub, SPLAT_KMEM_TEST11_NAME, SPLAT_KMEM_TEST11_DESC,
                        SPLAT_KMEM_TEST11_ID, splat_kmem_test11);
-#endif /* _LP64 */
-       SPLAT_TEST_INIT(sub, SPLAT_KMEM_TEST12_NAME, SPLAT_KMEM_TEST12_DESC,
-                       SPLAT_KMEM_TEST12_ID, splat_kmem_test12);
+#endif
+       SPLAT_TEST_INIT(sub, SPLAT_KMEM_TEST13_NAME, SPLAT_KMEM_TEST13_DESC,
+                       SPLAT_KMEM_TEST13_ID, splat_kmem_test13);
 
        return sub;
 }
@@ -1157,10 +1386,10 @@ void
 splat_kmem_fini(splat_subsystem_t *sub)
 {
        ASSERT(sub);
-       SPLAT_TEST_FINI(sub, SPLAT_KMEM_TEST12_ID);
-#ifdef _LP64
+       SPLAT_TEST_FINI(sub, SPLAT_KMEM_TEST13_ID);
+#if 0
        SPLAT_TEST_FINI(sub, SPLAT_KMEM_TEST11_ID);
-#endif /* _LP64 */
+#endif
        SPLAT_TEST_FINI(sub, SPLAT_KMEM_TEST10_ID);
        SPLAT_TEST_FINI(sub, SPLAT_KMEM_TEST9_ID);
        SPLAT_TEST_FINI(sub, SPLAT_KMEM_TEST8_ID);