diff mbox series

[V3,3/5] ublk_drv: add SET_PARAM/GET_PARAM control command

Message ID 20220729072954.1070514-4-ming.lei@redhat.com (mailing list archive)
State New, archived
Headers show
Series ublk_drv: add generic mechanism to get/set parameters | expand

Commit Message

Ming Lei July 29, 2022, 7:29 a.m. UTC
Add two commands to set/get parameters generically.

One important goal of ublk is to provide generic framework for making
block device by userspace flexibly.

As one generic block device, there are still lots of block parameters,
such as max_sectors, write_cache/fua, discard related limits,
zoned parameters, ...., so this patch starts to add mechanism for set/get
device parameters.

Both generic block parameters(all kinds of queue settings) and ublk feature
related parameters can be covered with this way, then it becomes quite easy
to extend in future.

The parameter passed from userspace is added to one array, and the type is
used as index of the array. The following patch will add two parameter
types: basic(covers basic queue setting and misc settings which can't be grouped
easily) and discard. Also segment, zoned[1], userspace recovery[2] and
userspace backing buffer(in my todo list) parameters will be added in near
future.

This way provides mechanism to simulate any kind of generic block device from
userspace easily, from both block queue setting viewpoint or ublk feature
viewpoint.

[1] https://lore.kernel.org/qemu-devel/fa7de750-8def-c532-8c86-64c7505608e0@opensource.wdc.com/T/#u
[2] https://lore.kernel.org/linux-block/60bc9e53-605e-ee1d-9bd2-020693768339@linux.alibaba.com/

Signed-off-by: Ming Lei <ming.lei@redhat.com>
---
 drivers/block/ublk_drv.c      | 111 ++++++++++++++++++++++++++++++++++
 include/uapi/linux/ublk_cmd.h |  14 +++++
 2 files changed, 125 insertions(+)

Comments

Christoph Hellwig July 29, 2022, 2:22 p.m. UTC | #1
On Fri, Jul 29, 2022 at 03:29:52PM +0800, Ming Lei wrote:
> The parameter passed from userspace is added to one array, and the type is
> used as index of the array. The following patch will add two parameter
> types: basic(covers basic queue setting and misc settings which can't be grouped

A bunch of overly long lines here.

But I still think this is the wrong design.  The number of potential
parameter is very limited, so splitting them over multiple ioctls
and data structure for no good reason is really a de-optimization
that makes the code more complex and slower.
Ming Lei July 29, 2022, 2:59 p.m. UTC | #2
On Fri, Jul 29, 2022 at 04:22:26PM +0200, Christoph Hellwig wrote:
> On Fri, Jul 29, 2022 at 03:29:52PM +0800, Ming Lei wrote:
> > The parameter passed from userspace is added to one array, and the type is
> > used as index of the array. The following patch will add two parameter
> > types: basic(covers basic queue setting and misc settings which can't be grouped
> 
> A bunch of overly long lines here.
> 
> But I still think this is the wrong design.  The number of potential
> parameter is very limited, so splitting them over multiple ioctls

So far, at least the following 6 parameters are to be added:

- basic
- discard
- segment
- zoned
- user space crash recovery parameter
- userspace backing buffer parameter

And each one is basically not related with others.

And ublk is really one generic userspace driver framework, it is very
likely to have more parameters added in future.

> and data structure for no good reason is really a de-optimization
> that makes the code more complex and slower.

Fine, I don't have time to argue which one is better.

For the timing's reason, I switch to the single parameter way. If turns
outs single parameter can't work well enough in future, I will restart
to add set/get parameter command.


Thanks,
Ming
diff mbox series

Patch

diff --git a/drivers/block/ublk_drv.c b/drivers/block/ublk_drv.c
index 00b04f395de0..f949c0d96360 100644
--- a/drivers/block/ublk_drv.c
+++ b/drivers/block/ublk_drv.c
@@ -137,6 +137,8 @@  struct ublk_device {
 	spinlock_t		mm_lock;
 	struct mm_struct	*mm;
 
+	struct ublk_param_header *params[UBLK_PARAM_TYPE_LAST];
+
 	struct completion	completion;
 	unsigned int		nr_queues_ready;
 	atomic_t		nr_aborted_queues;
@@ -160,6 +162,28 @@  static DEFINE_MUTEX(ublk_ctl_mutex);
 
 static struct miscdevice ublk_misc;
 
+static int ublk_validate_param_header(const struct ublk_device *ub,
+		const struct ublk_param_header *h)
+{
+	if (h->type >= UBLK_PARAM_TYPE_LAST)
+		return -EINVAL;
+	if (h->len > UBLK_MAX_PARAM_LEN)
+		return -EINVAL;
+	return 0;
+}
+
+/* Old parameter with same type will be overridden */
+static int ublk_install_param(struct ublk_device *ub,
+		struct ublk_param_header *h)
+{
+	struct ublk_param_header *old = ub->params[h->type];
+
+	kfree(old);
+	ub->params[h->type] = h;
+
+	return 0;
+}
+
 static inline bool ublk_can_use_task_work(const struct ublk_queue *ubq)
 {
 	if (IS_BUILTIN(CONFIG_BLK_DEV_UBLK) &&
@@ -1447,6 +1471,87 @@  static int ublk_ctrl_get_dev_info(struct io_uring_cmd *cmd)
 	return ret;
 }
 
+static int ublk_ctrl_get_param(struct io_uring_cmd *cmd)
+{
+	struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd;
+	void __user *argp = (void __user *)(unsigned long)header->addr;
+	struct ublk_device *ub;
+	struct ublk_param_header ph;
+	struct ublk_param_header *param = NULL;
+	int ret = 0;
+
+	if (header->len <= sizeof(ph) || !header->addr)
+		return -EINVAL;
+
+	ub = ublk_get_device_from_id(header->dev_id);
+	if (!ub)
+		return -EINVAL;
+
+	ret = -EFAULT;
+	if (copy_from_user(&ph, argp, sizeof(ph)))
+		goto out_put;
+
+	ret = ublk_validate_param_header(ub, &ph);
+	if (ret)
+		goto out_put;
+
+	mutex_lock(&ub->mutex);
+	param = ub->params[ph.type];
+	mutex_unlock(&ub->mutex);
+	if (!param)
+		ret = -EINVAL;
+	else if (copy_to_user(argp, param, ph.len))
+		ret = -EFAULT;
+out_put:
+	ublk_put_device(ub);
+	return ret;
+}
+
+static int ublk_ctrl_set_param(struct io_uring_cmd *cmd)
+{
+	struct ublksrv_ctrl_cmd *header = (struct ublksrv_ctrl_cmd *)cmd->cmd;
+	void __user *argp = (void __user *)(unsigned long)header->addr;
+	struct ublk_device *ub;
+	struct ublk_param_header ph;
+	struct ublk_param_header *param = NULL;
+	int ret = -EFAULT;
+
+	if (header->len <= sizeof(ph) || !header->addr)
+		return -EINVAL;
+
+	ub = ublk_get_device_from_id(header->dev_id);
+	if (!ub)
+		return -EINVAL;
+
+	if (copy_from_user(&ph, argp, sizeof(ph)))
+		goto out_put;
+
+	ret = ublk_validate_param_header(ub, &ph);
+	if (ret)
+		goto out_put;
+
+	param = kmalloc(ph.len, GFP_KERNEL);
+	if (!param) {
+		ret = -ENOMEM;
+	} else if (copy_from_user(param, argp, ph.len)) {
+		ret = -EFAULT;
+	} else {
+		/* parameters can only be changed when device isn't live */
+		mutex_lock(&ub->mutex);
+		if (ub->dev_info.state != UBLK_S_DEV_LIVE)
+			ret = ublk_install_param(ub, param);
+		else
+			ret = -EACCES;
+		mutex_unlock(&ub->mutex);
+	}
+out_put:
+	ublk_put_device(ub);
+	if (ret)
+		kfree(param);
+
+	return ret;
+}
+
 static int ublk_ctrl_uring_cmd(struct io_uring_cmd *cmd,
 		unsigned int issue_flags)
 {
@@ -1482,6 +1587,12 @@  static int ublk_ctrl_uring_cmd(struct io_uring_cmd *cmd,
 	case UBLK_CMD_GET_QUEUE_AFFINITY:
 		ret = ublk_ctrl_get_queue_affinity(cmd);
 		break;
+	case UBLK_CMD_GET_PARAM:
+		ret = ublk_ctrl_get_param(cmd);
+		break;
+	case UBLK_CMD_SET_PARAM:
+		ret = ublk_ctrl_set_param(cmd);
+		break;
 	default:
 		break;
 	}
diff --git a/include/uapi/linux/ublk_cmd.h b/include/uapi/linux/ublk_cmd.h
index ca33092354ab..f6433bb1f5f6 100644
--- a/include/uapi/linux/ublk_cmd.h
+++ b/include/uapi/linux/ublk_cmd.h
@@ -15,6 +15,8 @@ 
 #define	UBLK_CMD_DEL_DEV		0x05
 #define	UBLK_CMD_START_DEV	0x06
 #define	UBLK_CMD_STOP_DEV	0x07
+#define	UBLK_CMD_SET_PARAM	0x08
+#define	UBLK_CMD_GET_PARAM	0x09
 
 /*
  * IO commands, issued by ublk server, and handled by ublk driver.
@@ -158,4 +160,16 @@  struct ublksrv_io_cmd {
 	__u64	addr;
 };
 
+/* 512 is big enough for single parameter */
+#define UBLK_MAX_PARAM_LEN	512
+
+enum {
+	UBLK_PARAM_TYPE_LAST,
+};
+
+struct ublk_param_header {
+	__u16 type;
+	__u16 len;
+};
+
 #endif