Skip to contentSkip to conversation

NVMe: Do not take nsid while a passthrough IO command is being issued via a block device file descriptor

PATCH In review Yan Liu 3 participants 3 messages Jan 23, 2015 – Jan 26, 2015

Subsystems (2)
lore
Share & tools
lore

Yan Liu

author
#0 lore raw

PATCH 1/1 NVMe: Do not take nsid while a passthrough IO command is being issued via a block device file descriptor

When a passthrough IO command is issued with a specific block device file descriptor. It should be applied at the namespace which is associated with that block device file descriptor. This patch makes such passthrough command ignore nsid in nvme_passthru_cmd structure. Instead it takes the namespace ID asscoiated with the block device descriptor.

Signed-off-by Yan Liu <yan at purestorage.com>
---
 drivers/block/nvme-core.c | 30 ++++++++++++++++++++++++++----
 1 file changed, 26 insertions(+), 4 deletions(-)
drivers/block/nvme-core.c +26 -4
index cb529e9..73c3c97 100644
@@ -1682,6 +1682,27 @@ static int nvme_submit_io(struct nvme_ns *ns, struct nvme_user_io __user *uio)
 	return status;
 }
 
+static int nvme_namespace_sel(struct nvme_dev *dev, struct nvme_ns **ns,
+			struct nvme_passthru_cmd __user *ucmd)
+{
+	struct nvme_ns *ns_ptr;
+	struct nvme_passthru_cmd cmd;
+
+	if (list_empty(&dev->namespaces))
+		return -ENOTTY;
+
+	if (copy_from_user(&cmd, ucmd, sizeof(cmd)))
+		return -EFAULT;
+
+	list_for_each_entry(ns_ptr, &dev->namespaces, list) {
+		if (ns_ptr->ns_id == cmd.nsid) {
+			*ns = ns_ptr;
+			return 0;
+		}
+	}
+	return -EINVAL;
+}
+
 static int nvme_user_cmd(struct nvme_dev *dev, struct nvme_ns *ns,
 			struct nvme_passthru_cmd __user *ucmd)
 {
@@ -1699,7 +1720,7 @@ static int nvme_user_cmd(struct nvme_dev *dev, struct nvme_ns *ns,
 	memset(&c, 0, sizeof(c));
 	c.common.opcode = cmd.opcode;
 	c.common.flags = cmd.flags;
-	c.common.nsid = cpu_to_le32(cmd.nsid);
+	c.common.nsid = ns ? cpu_to_le32(ns->ns_id) : cpu_to_le32(cmd.nsid);
 	c.common.cdw2[0] = cpu_to_le32(cmd.cdw2);
 	c.common.cdw2[1] = cpu_to_le32(cmd.cdw3);
 	c.common.cdw10[0] = cpu_to_le32(cmd.cdw10);
@@ -2590,14 +2611,15 @@ static long nvme_dev_ioctl(struct file *f, unsigned int cmd, unsigned long arg)
 {
 	struct nvme_dev *dev = f->private_data;
 	struct nvme_ns *ns;
+	int ret;
 
 	switch (cmd) {
 	case NVME_IOCTL_ADMIN_CMD:
 		return nvme_user_cmd(dev, NULL, (void __user *)arg);
 	case NVME_IOCTL_IO_CMD:
-		if (list_empty(&dev->namespaces))
-			return -ENOTTY;
-		ns = list_first_entry(&dev->namespaces, struct nvme_ns, list);
+		ret = nvme_namespace_sel(dev, &ns, (void __user *)arg);
+		if (ret)
+			return ret;
 		return nvme_user_cmd(dev, ns, (void __user *)arg);
 	default:
 		return -ENOTTY;
Signature · 1 line
1.9.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 4 lines · Yan Liu

When a passthrough IO command is issued with a specific block device file descriptor. It should be applied at the namespace which is associated with that block device file descriptor. This patch makes such passthrough command ignore nsid in nvme_passthru_cmd structure. Instead it takes the namespace ID asscoiated with the block device descriptor.

This looks good to me. If Keith can point to a use case for magic or hidden nsids we'll have to find a bypass for them, but this certainly is the right short term fix:

Reviewed-by Christoph Hellwig <hch at lst.de>
Quoted 7 lines

{

drivers/block/nvme-core.c +1 -0
 	struct nvme_dev *dev = f->private_data;
 	struct nvme_ns *ns;
+	int ret;
 
 	switch (cmd) {
 	case NVME_IOCTL_ADMIN_CMD:

drivers/block/nvme-core.c static long nvme_dev_ioctl(struct file *f, unsigned int cmd, unsigned long arg) · line 2624

Quoted 10 lines
drivers/block/nvme-core.c +3 -3
 		return nvme_user_cmd(dev, NULL, (void __user *)arg);
 	case NVME_IOCTL_IO_CMD:
-		if (list_empty(&dev->namespaces))
-			return -ENOTTY;
-		ns = list_first_entry(&dev->namespaces, struct nvme_ns, list);
+		ret = nvme_namespace_sel(dev, &ns, (void __user *)arg);
+		if (ret)
+			return ret;
 		return nvme_user_cmd(dev, ns, (void __user *)arg);
 	default:

This function would benefit from a local variable ala:

	void __user *argp = (void __user *)arg;

but that might be done in a follow up patch, or postponed for now.

#2 lore raw
Quoted 9 lines · Christoph Hellwig
Quoted 4 lines · Yan Liu

When a passthrough IO command is issued with a specific block device file descriptor. It should be applied at the namespace which is associated with that block device file descriptor. This patch makes such passthrough command ignore nsid in nvme_passthru_cmd structure. Instead it takes the namespace ID asscoiated with the block device descriptor.

This looks good to me. If Keith can point to a use case for magic or hidden nsids we'll have to find a bypass for them, but this certainly is the right short term fix:

'Flush' is the only command from the spec where specifying all namespaces might be desirable. Otherwise, I can't disclose vendor specific behavior to gain concensus on operating within spec, silly as this desire may seem. Why empower the driver to make a feature unreachable?