From cd0373b9b71ba61204326f54d500b878befb3408 Mon Sep 17 00:00:00 2001
From: Alessandro Rubini <rubini@gnudd.com>
Date: Tue, 31 Jan 2012 19:07:44 +0100
Subject: [PATCH] core: allocate control structures using an own kmem_cache

Testing /dev/urandom versus zzero-0-1-data (the random device)
shows this increases performance by 0.1 usecs per block. We
are now just over 0.7 usecs per block on my development PC.

This also introduces zio-core.c with generic stuff. We agree that zio-sys
will become zio-sysfs over time.

Signed-off-by: Alessandro Rubini <rubini@gnudd.com>
Acked-by: Federico Vaga <federico.vaga@gmail.com>

Conflicts:

	Makefile
---
 Makefile                   |  2 +-
 include/linux/zio-buffer.h | 28 +++++---------------
 zio-core.c                 | 52 ++++++++++++++++++++++++++++++++++++++
 zio-sys.c                  |  5 ++++
 4 files changed, 65 insertions(+), 22 deletions(-)
 create mode 100644 zio-core.c

diff --git a/Makefile b/Makefile
index 234df62..4fd87a5 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
 LINUX ?= /lib/modules/$(shell uname -r)/build
 
-zio-objs := zio-cdev.o zio-sys.o
+zio-objs := zio-core.o zio-cdev.o zio-sys.o
 zio-objs += buffers/zio-buf-kmalloc.o triggers/zio-trig-user.o
 
 obj-m = zio.o
diff --git a/include/linux/zio-buffer.h b/include/linux/zio-buffer.h
index 69d495e..28acf06 100644
--- a/include/linux/zio-buffer.h
+++ b/include/linux/zio-buffer.h
@@ -16,27 +16,6 @@
 
 #define ZIO_DEFAULT_BUFFER "kmalloc" /* For devices with no own buffer type */
 
-/* FIXME: use a kmem_cache and real functions for control alloc/free */
-static inline struct zio_control *zio_alloc_control(gfp_t gfp)
-{
-	struct zio_control *ctrl;
-
-	ctrl = kzalloc(sizeof(*ctrl), gfp);
-	if (!ctrl)
-		return NULL;
-	ctrl->major_version = ZIO_MAJOR_VERSION;
-	ctrl->minor_version = ZIO_MINOR_VERSION;
-	if (ntohl(1) == 1)
-		ctrl->flags |= ZIO_CONTROL_BIG_ENDIAN;
-	else
-		ctrl->flags |= ZIO_CONTROL_LITTLE_ENDIAN;
-	return ctrl;
-}
-static inline void zio_free_control(struct zio_control *ctrl)
-{
-	kfree(ctrl);
-}
-
 /*
  * The following structure defines a buffer type, with methods.
  * An instance is created for each channel using it
@@ -75,6 +54,13 @@ int __must_check zio_register_buf(struct zio_buffer_type *zbuf,
 				  const char *name);
 void zio_unregister_buf(struct zio_buffer_type *zbuf);
 
+/* We have our own kmem_cache (a.k.a. slab) for control structures */
+int zio_slab_init(void);
+void zio_slab_exit(void);
+struct zio_control *zio_alloc_control(gfp_t gfp);
+void zio_free_control(struct zio_control *ctrl);
+
+
 struct zio_bi {
 	struct zio_obj_head	head;
 	struct list_head	list;		/* instance list */
diff --git a/zio-core.c b/zio-core.c
new file mode 100644
index 0000000..1e382d3
--- /dev/null
+++ b/zio-core.c
@@ -0,0 +1,52 @@
+/* Alessandro Rubini, Federico Vaga for CERN, 2011, GNU GPLv2 or later */
+
+/*
+ * This file includes functions that build up the zio core, but are not
+ * strictly related to sysfs and configuration. Those are in zio-sysfs.c
+ */
+
+#include <linux/kernel.h>
+#include <linux/slab.h>
+#include <linux/zio.h>
+#include <linux/zio-buffer.h>
+
+/*
+ * We use a local slab for control structures.
+ */
+static struct kmem_cache *zio_ctrl_slab;
+
+struct zio_control *zio_alloc_control(gfp_t gfp)
+{
+	struct zio_control *ctrl;
+
+	ctrl = kmem_cache_alloc(zio_ctrl_slab, gfp);
+	if (!ctrl)
+		return NULL;
+	ctrl->major_version = ZIO_MAJOR_VERSION;
+	ctrl->minor_version = ZIO_MINOR_VERSION;
+	if (ntohl(1) == 1)
+		ctrl->flags |= ZIO_CONTROL_BIG_ENDIAN;
+	else
+		ctrl->flags |= ZIO_CONTROL_LITTLE_ENDIAN;
+	return ctrl;
+}
+
+void zio_free_control(struct zio_control *ctrl)
+{
+	kmem_cache_free(zio_ctrl_slab, ctrl);
+}
+
+int __init zio_slab_init(void)
+{
+	zio_ctrl_slab = KMEM_CACHE(zio_control, 0);
+	if (!zio_ctrl_slab)
+		return -ENOMEM;
+	return 0;
+}
+
+void zio_slab_exit(void) /* not __exit: called from zio_init on failures */
+{
+	if (zio_ctrl_slab)
+		kmem_cache_destroy(zio_ctrl_slab);
+	return;
+}
diff --git a/zio-sys.c b/zio-sys.c
index 2572e38..df08163 100644
--- a/zio-sys.c
+++ b/zio-sys.c
@@ -1745,6 +1745,9 @@ static int __init zio_init(void)
 	BUILD_BUG_ON(ZATTR_STD_NUM_ZBUF != ARRAY_SIZE(zio_zbuf_attr_names));
 	BUILD_BUG_ON(ZATTR_STD_NUM_TRIG != ARRAY_SIZE(zio_trig_attr_names));
 
+	err = zio_slab_init();
+	if (err)
+		return err;
 	/* Initialize char device */
 	err = __zio_register_cdev();
 	if (err)
@@ -1774,6 +1777,7 @@ static int __init zio_init(void)
 out_kobj:
 	__zio_unregister_cdev();
 out_cdev:
+	zio_slab_exit();
 	return err;
 }
 
@@ -1792,6 +1796,7 @@ static void __exit zio_exit(void)
 
 	/* Remove char device */
 	__zio_unregister_cdev();
+	zio_slab_exit();
 
 	pr_info("zio-core had been unloaded\n");
 	return;
-- 
GitLab