[PATCH 4/8] zram: add dynamic device add/remove functionality

Sergey Senozhatsky
2015-02-26T14:10:56Z

Introduce /dev/zram-control interface which lets user-space to add a new zram
device (ZRAM_CTL_ADD ioctl) or remove the existing one (ZRAM_CTL_REMOVE ioctl).

This patch adds only two ioctl operations: add device and remove device. There
is no support for `automatic' device_id generation in this patch (will come up
later), so correct device_id must be provided.

Corresponding user-space visible defines (ZRAM_CTL_ADD/ZRAM_CTL_REMOVE) are
located in a new header file: include/uapi/linux/zram.h. Well, we have `private'
zram_drv.h and `public' zram.h headers from now on. I don't really want to make
zram_drv.h public, that would require moving of all private defines and structs
to zram_drv.c (100+ lines of code) and zram_drv.c is already big and a bit
complicated. On the other hand, zram uses struct block_device_operations that
has a per-device ->ioctl callback (not used by zram as of now). Ideally, we can
drop some of the existing zram_stats sysfs device ATTRs and use ioctl
(f.e. ZRAM_CTL_INFO) to copy out zram->stats to user-space. That will require
zram_info structure being visible to user-space via zram.h, so this new header
file can be of use.

Interface usage example (pretty much like LOOP ctl interface):

#include <linux/zram.h>

cfd = open("/dev/zram-control", O_RDWR);

/* add a new specific zram device */
err = ioctl(cfd, ZRAM_CTL_ADD, devnr);

/* remove a specific zram device */
err = ioctl(cfd, ZRAM_CTL_REMOVE, devnr);

NOTE, zram provides /dev/zram-control alias and zram module will be loaded
automatically when /dev/zram-control is access the first time. However, there
might be users who already depend on the fact that at least zram0 device gets
always created by zram_init(). Thus, due to compatibility reasons, along with
requested device_id (f.e. ZRAM_CTL_ADD 5) zram0 will also be created.

Signed-off-by: Sergey Senozhatsky <sergey.senozhatsky@gmail.com>
---
drivers/block/zram/zram_drv.c | 99 ++++++++++++++++++++++++++++++++++++++++++-
include/linux/miscdevice.h | 1 +
include/uapi/linux/Kbuild | 1 +
include/uapi/linux/zram.h | 17 ++++++++
4 files changed, 117 insertions(+), 1 deletion(-)
create mode 100644 include/uapi/linux/zram.h

diff --git a/drivers/block/zram/zram_drv.c b/drivers/block/zram/zram_drv.c
index 92087da..4f87d1e 100644
--- a/drivers/block/zram/zram_drv.c
+++ b/drivers/block/zram/zram_drv.c
@@ -33,10 +33,15 @@
#include <linux/vmalloc.h>
#include <linux/err.h>
#include <linux/idr.h>
+#include <linux/miscdevice.h>
+#include <linux/zram.h>

#include "zram_drv.h"

static DEFINE_IDR(zram_index_idr);
+/* idr index must be protected */
+static DEFINE_MUTEX(zram_index_mutex);
+
static int zram_major;
static const char *default_compressor = "lzo";

@@ -1060,6 +1065,19 @@ static struct attribute_group zram_disk_attr_group = {
.attrs = zram_disk_attrs,
};

+/* lookup if there is any device pointer that match to the
+ * given device_id. return device pointer if so, or ERR_PTR()
+ * otherwise. */
+static struct zram *zram_lookup(int dev_id)
+{
+ struct zram *zram;
+
+ zram = idr_find(&zram_index_idr, dev_id);
+ if (zram)
+ return zram;
+ return ERR_PTR(-ENODEV);
+}
+
static int zram_add(int device_id)
{
struct zram *zram;
@@ -1174,6 +1192,76 @@ static void zram_remove(struct zram *zram)
kfree(zram);
}

+static long zram_control_ioctl(struct file *file, unsigned int cmd,
+ unsigned long parm)
+{
+ struct zram *zram;
+ int ret = -ENOSYS, err;
+
+ mutex_lock(&zram_index_mutex);
+ zram = zram_lookup(parm);
+
+ switch (cmd) {
+ case ZRAM_CTL_ADD:
+ if (!IS_ERR(zram)) {
+ ret = -EEXIST;
+ break;
+ }
+ ret = zram_add(parm);
+ break;
+ case ZRAM_CTL_REMOVE:
+ if (IS_ERR(zram)) {
+ ret = PTR_ERR(zram);
+ break;
+ }
+
+ /* First, make ->disksize device attr RO, closing
+ * ZRAM_CTL_REMOVE vs disksize_store() race window */
+ ret = sysfs_chmod_file(&disk_to_dev(zram->disk)->kobj,
+ &dev_attr_disksize.attr, S_IRUGO);
+ if (ret)
+ break;
+
+ ret = zram_reset_device(zram);
+ if (ret == 0) {
+ /* ->disksize is RO and there are no ->bd_openers */
+ zram_remove(zram);
+ break;
+ }
+
+ /* If there are still device bd_openers, try to make ->disksize
+ * RW again and return. even if we fail to make ->disksize RW,
+ * user still has RW ->reset attr. so it's possible to destroy
+ * that device */
+ err = sysfs_chmod_file(&disk_to_dev(zram->disk)->kobj,
+ &dev_attr_disksize.attr,
+ S_IWUSR | S_IRUGO);
+ if (err)
+ ret = err;
+ break;
+ }
+ mutex_unlock(&zram_index_mutex);
+
+ return ret;
+}
+
+static const struct file_operations zram_ctl_fops = {
+ .open = nonseekable_open,
+ .unlocked_ioctl = zram_control_ioctl,
+ .compat_ioctl = zram_control_ioctl,
+ .owner = THIS_MODULE,
+ .llseek = noop_llseek,
+};
+
+static struct miscdevice zram_misc = {
+ .minor = ZRAM_CTRL_MINOR,
+ .name = "zram-control",
+ .fops = &zram_ctl_fops,
+};
+
+MODULE_ALIAS_MISCDEV(ZRAM_CTRL_MINOR);
+MODULE_ALIAS("devname:zram-control");
+
static int zram_exit_cb(int id, void *ptr, void *data)
{
zram_remove(ptr);
@@ -1182,6 +1270,7 @@ static int zram_exit_cb(int id, void *ptr, void *data)

static void destroy_devices(void)
{
+ misc_deregister(&zram_misc);
idr_for_each(&zram_index_idr, &zram_exit_cb, NULL);
idr_destroy(&zram_index_idr);
unregister_blkdev(zram_major, "zram");
@@ -1198,6 +1287,12 @@ static int __init zram_init(void)
return -EINVAL;
}

+ ret = misc_register(&zram_misc);
+ if (ret < 0) {
+ pr_warn("Unable to register zram-control\n");
+ return ret;
+ }
+
zram_major = register_blkdev(0, "zram");
if (zram_major <= 0) {
pr_warn("Unable to get major number\n");
@@ -1205,7 +1300,9 @@ static int __init zram_init(void)
}

for (dev_id = 0; dev_id < num_devices; dev_id++) {
+ mutex_lock(&zram_index_mutex);
ret = zram_add(dev_id);
+ mutex_unlock(&zram_index_mutex);
if (ret != 0)
goto out_error;
}
@@ -1227,7 +1324,7 @@ module_init(zram_init);
module_exit(zram_exit);

module_param(num_devices, uint, 0);
-MODULE_PARM_DESC(num_devices, "Number of zram devices");
+MODULE_PARM_DESC(num_devices, "Number of pre-created zram devices");

MODULE_LICENSE("Dual BSD/GPL");
MODULE_AUTHOR("Nitin Gupta <ngupta@vflare.org>");
diff --git a/include/linux/miscdevice.h b/include/linux/miscdevice.h
index ee80dd7..11a14e3 100644
--- a/include/linux/miscdevice.h
+++ b/include/linux/miscdevice.h
@@ -49,6 +49,7 @@
#define LOOP_CTRL_MINOR 237
#define VHOST_NET_MINOR 238
#define UHID_MINOR 239
+#define ZRAM_CTRL_MINOR 240
#define MISC_DYNAMIC_MINOR 255

struct device;
diff --git a/include/uapi/linux/Kbuild b/include/uapi/linux/Kbuild
index 68ceb97..9cabde9 100644
--- a/include/uapi/linux/Kbuild
+++ b/include/uapi/linux/Kbuild
@@ -448,3 +448,4 @@ header-y += xattr.h
header-y += xfrm.h
header-y += zorro.h
header-y += zorro_ids.h
+header-y += zram.h
diff --git a/include/uapi/linux/zram.h b/include/uapi/linux/zram.h
new file mode 100644
index 0000000..7b84532
--- /dev/null
+++ b/include/uapi/linux/zram.h
@@ -0,0 +1,17 @@
+/*
+ * include/linux/zram.h
+ *
+ * Copyright (C) 2015 Sergey Senozhatsky.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+#ifndef _UAPI_LINUX_ZRAM_H
+#define _UAPI_LINUX_ZRAM_H
+
+/* /dev/zram-control interface */
+#define ZRAM_CTL_ADD 0x5C80
+#define ZRAM_CTL_REMOVE 0x5C81
+
+#endif /* _UAPI_LINUX_ZRAM_H */
--
2.3.1.167.g7f4ba4b