Skip to contentSkip to conversation

OPAL fixups

PATCH In review Christoph Hellwig 4 participants 12 messages Feb 17, 2017

Subsystems (3)
lore
Share & tools
lore

Christoph Hellwig

author maintainer
#0 lore raw

Hi all,

this contains a few more OPAL-related fixups. It tones down warnings a bit, allocates the OPAL-?pecific data structure in a separate dynamic allocation, checks for support of Security Send/Receive in NVMe before using them, and makes sure we re-discovery the security capabilities after each reset.

author maintainer
#1 lore raw

PATCH 1/4 block/sed-opal: tone down not supported warnings

Not having OPAL or a sub-feature supported is an entirely normal condition for many drives, so don't warn about it. Keep the messages, but tone them down to debug only.

Signed-off-by Christoph Hellwig <hch at lst.de>
Reviewed-by Scott Bauer <scott.bauer at intel.com>
---
 block/sed-opal.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
block/sed-opal.c +4 -4
index e95b8a57053d..bcdd5b6d02e8 100644
@@ -387,16 +387,16 @@ static int opal_discovery0_end(struct opal_dev *dev)
 	}
 
 	if (!supported) {
-		pr_err("This device is not Opal enabled. Not Supported!\n");
+		pr_debug("This device is not Opal enabled. Not Supported!\n");
 		return -EOPNOTSUPP;
 	}
 
 	if (!single_user)
-		pr_warn("Device doesn't support single user mode\n");
+		pr_debug("Device doesn't support single user mode\n");
 
 
 	if (!found_com_id) {
-		pr_warn("Could not find OPAL comid for device. Returning early\n");
+		pr_debug("Could not find OPAL comid for device. Returning early\n");
 		return -EOPNOTSUPP;;
 	}
 
@@ -1951,7 +1951,7 @@ void init_opal_dev(struct opal_dev *opal_dev, sec_send_recv *send_recv)
 	mutex_init(&opal_dev->dev_lock);
 	opal_dev->send_recv = send_recv;
 	if (check_opal_support(opal_dev) < 0)
-		pr_warn("Opal is not supported on this device\n");
+		pr_debug("Opal is not supported on this device\n");
 	opal_dev->initialized = true;
 }
 EXPORT_SYMBOL(init_opal_dev);
Signature · 1 line
2.11.0
author maintainer
#2 lore raw

PATCH 2/4 block/sed-opal: allocate struct opal_dev dynamically

In reply to Christoph Hellwig

Insted of bloating the containing structure with it all the time this allocates struct opal_dev dynamically. Additionally this allows moving the definition of struct opal_dev into sed-opal.c. For this a new private data field is added to it that is passed to the send/receive callback. After that a lot of internals can be made private as well.

Signed-off-by Christoph Hellwig <hch at lst.de>
Tested-by Scott Bauer <scott.bauer at intel.com>
Reviewed-by Scott Bauer <scott.bauer at intel.com>
---
 block/opal_proto.h       |  23 ++++++++++
 block/sed-opal.c         | 101 +++++++++++++++++++++++++++++++++++++----
 drivers/nvme/host/core.c |   9 ++--
 drivers/nvme/host/nvme.h |  14 ++----
 drivers/nvme/host/pci.c  |   8 +++-
 include/linux/sed-opal.h | 116 ++---------------------------------------------
 6 files changed, 131 insertions(+), 140 deletions(-)
block/opal_proto.h +23 -0
index af9abc56c157..f40c9acf8895 100644
@@ -19,6 +19,29 @@
 #ifndef _OPAL_PROTO_H
 #define _OPAL_PROTO_H
 
+/*
+ * These constant values come from:
+ * SPC-4 section
+ * 6.30 SECURITY PROTOCOL IN command / table 265.
+ */
+enum {
+	TCG_SECP_00 = 0,
+	TCG_SECP_01,
+};
+
+/*
+ * Token defs derived from:
+ * TCG_Storage_Architecture_Core_Spec_v2.01_r1.00
+ * 3.2.2 Data Stream Encoding
+ */
+enum opal_response_token {
+	OPAL_DTA_TOKENID_BYTESTRING = 0xe0,
+	OPAL_DTA_TOKENID_SINT = 0xe1,
+	OPAL_DTA_TOKENID_UINT = 0xe2,
+	OPAL_DTA_TOKENID_TOKEN = 0xe3, /* actual token is returned */
+	OPAL_DTA_TOKENID_INVALID = 0X0
+};
+
 #define DTAERROR_NO_METHOD_STATUS 0x89
 #define GENERIC_HOST_SESSION_NUM 0x41
 
block/sed-opal.c +91 -10

133 lines not on this page · raw message

drivers/nvme/host/core.c +4 -5
index 04c48e75ff16..8aeb4a623b65 100644
@@ -811,7 +811,7 @@ static int nvme_ioctl(struct block_device *bdev, fmode_t mode,
 			return nvme_nvm_ioctl(ns, cmd, arg);
 #endif
 		if (is_sed_ioctl(cmd))
-			return sed_ioctl(&ns->ctrl->opal_dev, cmd,
+			return sed_ioctl(ns->ctrl->opal_dev, cmd,
 					 (void __user *) arg);
 		return -ENOTTY;
 	}
@@ -1085,18 +1085,17 @@ static const struct pr_ops nvme_pr_ops = {
 };
 
 #ifdef CONFIG_BLK_SED_OPAL
-int nvme_sec_submit(struct opal_dev *dev, u16 spsp, u8 secp,
-		    void *buffer, size_t len, bool send)
+int nvme_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len,
+		bool send)
 {
+	struct nvme_ctrl *ctrl = data;
 	struct nvme_command cmd;
-	struct nvme_ctrl *ctrl = NULL;
 
 	memset(&cmd, 0, sizeof(cmd));
 	if (send)
 		cmd.common.opcode = nvme_admin_security_send;
 	else
 		cmd.common.opcode = nvme_admin_security_recv;
-	ctrl = container_of(dev, struct nvme_ctrl, opal_dev);
 	cmd.common.nsid = 0;
 	cmd.common.cdw10[0] = cpu_to_le32(((u32)secp) << 24 | ((u32)spsp) << 8);
 	cmd.common.cdw10[1] = cpu_to_le32(len);
drivers/nvme/host/nvme.h +3 -11
index 569cba14cede..5126c4bbee1a 100644
@@ -126,7 +126,7 @@ struct nvme_ctrl {
 	struct list_head node;
 	struct ida ns_ida;
 
-	struct opal_dev opal_dev;
+	struct opal_dev *opal_dev;
 
 	char name[12];
 	char serial[20];
@@ -270,16 +270,8 @@ int nvme_init_identify(struct nvme_ctrl *ctrl);
 void nvme_queue_scan(struct nvme_ctrl *ctrl);
 void nvme_remove_namespaces(struct nvme_ctrl *ctrl);
 
-#ifdef CONFIG_BLK_SED_OPAL
-int nvme_sec_submit(struct opal_dev *dev, u16 spsp, u8 secp,
-		    void *buffer, size_t len, bool send);
-#else
-static inline int nvme_sec_submit(struct opal_dev *dev, u16 spsp, u8 secp,
-				  void *buffer, size_t len, bool send)
-{
-	return 0;
-}
-#endif /* CONFIG_BLK_DEV_SED_OPAL */
+int nvme_sec_submit(void *data, u16 spsp, u8 secp, void *buffer, size_t len,
+		bool send);
 
 #define NVME_NR_AERS	1
 void nvme_complete_async_event(struct nvme_ctrl *ctrl, __le16 status,
drivers/nvme/host/pci.c +6 -2
index e25d632bd9f2..5db8a38a8b43 100644
@@ -1739,6 +1739,7 @@ static void nvme_pci_free_ctrl(struct nvme_ctrl *ctrl)
 	if (dev->ctrl.admin_q)
 		blk_put_queue(dev->ctrl.admin_q);
 	kfree(dev->queues);
+	kfree(dev->ctrl.opal_dev);
 	kfree(dev);
 }
 
@@ -1788,10 +1789,13 @@ static void nvme_reset_work(struct work_struct *work)
 	if (result)
 		goto out;
 
-	init_opal_dev(&dev->ctrl.opal_dev, &nvme_sec_submit);
+	if (!dev->ctrl.opal_dev) {
+		dev->ctrl.opal_dev =
+			init_opal_dev(&dev->ctrl, &nvme_sec_submit);
+	}
 
 	if (was_suspend)
-		opal_unlock_from_suspend(&dev->ctrl.opal_dev);
+		opal_unlock_from_suspend(dev->ctrl.opal_dev);
 
 	result = nvme_setup_io_queues(dev);
 	if (result)
include/linux/sed-opal.h +4 -112

132 lines not on this page · raw message

Signature · 1 line
2.11.0
author maintainer
#3 lore raw

PATCH 3/4 nvme: Check for Security send/recv support before issuing commands.

In reply to Christoph Hellwig

From: Scott Bauer <[email protected]>

We need to verify that the controller supports the security commands before actually trying to issue them.

Signed-off-by: Scott Bauer <scott.bauer at intel.com> [hch: moved the check so that we don't call into the OPAL code if not
supported]
Signed-off-by: Christoph Hellwig <hch at lst.de>

---
 drivers/nvme/host/core.c | 1 +
 drivers/nvme/host/nvme.h | 1 +
 drivers/nvme/host/pci.c  | 2 +-
 include/linux/nvme.h     | 1 +
 4 files changed, 4 insertions(+), 1 deletion(-)
drivers/nvme/host/core.c +1 -0
index 8aeb4a623b65..44a1a257e0b5 100644
@@ -1285,6 +1285,7 @@ int nvme_init_identify(struct nvme_ctrl *ctrl)
 		return -EIO;
 	}
 
+	ctrl->oacs = le16_to_cpu(id->oacs);
 	ctrl->vid = le16_to_cpu(id->vid);
 	ctrl->oncs = le16_to_cpup(&id->oncs);
 	atomic_set(&ctrl->abort_limit, id->acl + 1);
drivers/nvme/host/nvme.h +1 -0
index 5126c4bbee1a..14cfc6f7facb 100644
@@ -140,6 +140,7 @@ struct nvme_ctrl {
 	u32 max_hw_sectors;
 	u16 oncs;
 	u16 vid;
+	u16 oacs;
 	atomic_t abort_limit;
 	u8 event_limit;
 	u8 vwc;
drivers/nvme/host/pci.c +1 -1
index 5db8a38a8b43..ddc51adb594d 100644
@@ -1789,7 +1789,7 @@ static void nvme_reset_work(struct work_struct *work)
 	if (result)
 		goto out;
 
-	if (!dev->ctrl.opal_dev) {
+	if ((dev->ctrl.oacs & NVME_CTRL_OACS_SEC_SUPP) && !dev->ctrl.opal_dev) {
 		dev->ctrl.opal_dev =
 			init_opal_dev(&dev->ctrl, &nvme_sec_submit);
 	}
include/linux/nvme.h +1 -0
index 3e2ed49c3ad8..0b676a02cf3e 100644
@@ -244,6 +244,7 @@ enum {
 	NVME_CTRL_ONCS_DSM			= 1 << 2,
 	NVME_CTRL_ONCS_WRITE_ZEROES		= 1 << 3,
 	NVME_CTRL_VWC_PRESENT			= 1 << 0,
+	NVME_CTRL_OACS_SEC_SUPP                 = 1 << 0,
 };
 
 struct nvme_lbaf {
Signature · 1 line
2.11.0
author maintainer
#4 lore raw

PATCH 4/4 nvme: re-check security protocol support after reset

In reply to Christoph Hellwig

A device may change capabilities after each reset, e.g. due to a firmware upgrade. We should thus check for Security Send/Receive and OPAL support after each reset.

Signed-off-by Christoph Hellwig <hch at lst.de>
---
 drivers/nvme/host/pci.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/nvme/host/pci.c +2 -1
index ddc51adb594d..c5986850f88b 100644
@@ -1789,7 +1789,8 @@ static void nvme_reset_work(struct work_struct *work)
 	if (result)
 		goto out;
 
-	if ((dev->ctrl.oacs & NVME_CTRL_OACS_SEC_SUPP) && !dev->ctrl.opal_dev) {
+	kfree(dev->ctrl.opal_dev);
+	if (dev->ctrl.oacs & NVME_CTRL_OACS_SEC_SUPP) {
 		dev->ctrl.opal_dev =
 			init_opal_dev(&dev->ctrl, &nvme_sec_submit);
 	}
Signature · 1 line
2.11.0
#5 lore raw

Re: [PATCH 4/4] nvme: re-check security protocol support after reset

Review static void nvme_reset_work(struct work_struct *work) · line 1796

Quoted 10 lines · Christoph Hellwig
(quoted diff) +2 -1
@@ -1789,7 +1789,8 @@ static void nvme_reset_work(struct work_struct *work)
 	if (result)
 		goto out;
 
-	if ((dev->ctrl.oacs & NVME_CTRL_OACS_SEC_SUPP) && !dev->ctrl.opal_dev) {
+	kfree(dev->ctrl.opal_dev);
+	if (dev->ctrl.oacs & NVME_CTRL_OACS_SEC_SUPP) {
 		dev->ctrl.opal_dev =
 			init_opal_dev(&dev->ctrl, &nvme_sec_submit);
 	}

A couple things.

This has a use-after-free in opal_unlock_from_suspend if the nvme device had an opal_dev before, but no longer support the capability after resume. So you'd want to set ctrl.opal_dev to NULL after the free.

But we don't want to unconditionally free it anyway during resume since opal_unlock_from_suspend requires the exisiting opal_dev state information saved in the 'unlk_list'.

Something like this instead:

---

drivers/nvme/host/pci.c +9 -5
index ddc51ad..8fa6be9 100644
@@ -1789,13 +1789,17 @@ static void nvme_reset_work(struct work_struct *work)
 	if (result)
 		goto out;
 
-	if ((dev->ctrl.oacs & NVME_CTRL_OACS_SEC_SUPP) && !dev->ctrl.opal_dev) {
-		dev->ctrl.opal_dev =
-			init_opal_dev(&dev->ctrl, &nvme_sec_submit);
+	if (dev->ctrl.oacs & NVME_CTRL_OACS_SEC_SUPP)
+		if (was_suspend && dev->ctrl.opal_dev)
+			opal_unlock_from_suspend(dev->ctrl.opal_dev);
+		else if (!dev->ctrl.opal_dev)
+			dev->ctrl.opal_dev =
+				init_opal_dev(&dev->ctrl, &nvme_sec_submit);
+	} else {
+		kfree(dev->ctrl.opal_dev);
+		dev->ctrl.opal_dev = NULL;
 	}
 
-	if (was_suspend)
-		opal_unlock_from_suspend(dev->ctrl.opal_dev);
 
 	result = nvme_setup_io_queues(dev);
 	if (result)

--

#6 lore raw

Re: [PATCH 4/4] nvme: re-check security protocol support after reset

Quoted 35 lines · Keith Busch

drivers/nvme/host/pci.c static void nvme_reset_work(struct work_struct *work) · line 1796

Quoted 10 lines · Christoph Hellwig
drivers/nvme/host/pci.c +2 -1
@@ -1789,7 +1789,8 @@ static void nvme_reset_work(struct work_struct *work)
 	if (result)
 		goto out;
 
-	if ((dev->ctrl.oacs & NVME_CTRL_OACS_SEC_SUPP) && !dev->ctrl.opal_dev) {
+	kfree(dev->ctrl.opal_dev);
+	if (dev->ctrl.oacs & NVME_CTRL_OACS_SEC_SUPP) {
 		dev->ctrl.opal_dev =
 			init_opal_dev(&dev->ctrl, &nvme_sec_submit);
 	}

A couple things.

This has a use-after-free in opal_unlock_from_suspend if the nvme device had an opal_dev before, but no longer support the capability after resume. So you'd want to set ctrl.opal_dev to NULL after the free.

But we don't want to unconditionally free it anyway during resume since opal_unlock_from_suspend requires the exisiting opal_dev state information saved in the 'unlk_list'.

Something like this instead:

---

drivers/nvme/host/pci.c +0 -2
index ddc51ad..8fa6be9 100644
@@ -1789,13 +1789,17 @@ static void nvme_reset_work(struct work_struct *work)
 	if (result)
 		goto out;
 
-	if ((dev->ctrl.oacs & NVME_CTRL_OACS_SEC_SUPP) && !dev->ctrl.opal_dev) {
-		dev->ctrl.opal_dev =

drivers/nvme/host/pci.c static void nvme_reset_work(struct work_struct *work) · line 1800

Quoted 10 lines
drivers/nvme/host/pci.c +9 -1
-			init_opal_dev(&dev->ctrl, &nvme_sec_submit);
+	if (dev->ctrl.oacs & NVME_CTRL_OACS_SEC_SUPP)
+		if (was_suspend && dev->ctrl.opal_dev)
+			opal_unlock_from_suspend(dev->ctrl.opal_dev);
+		else if (!dev->ctrl.opal_dev)
+			dev->ctrl.opal_dev =
+				init_opal_dev(&dev->ctrl, &nvme_sec_submit);
+	} else {
+		kfree(dev->ctrl.opal_dev);
+		dev->ctrl.opal_dev = NULL;

Keith's comments made me realize something even deeper as well. Assuming the firmware changed and we no longer support security commands we need to free the opal_dev structure like we're doing but there is a possiblity that there were saved ranges in the structure that we need to free as well. If the user had previously told the kernel to unlock 5 ranges coming out of a suspend there are 5 structures we need to free inside the opal_dev before we free the opal dev. We'll need to re-introduce free_opal_dev() in the opal code like we had a while back.

author maintainer
#7 lore raw

Re: [PATCH 4/4] nvme: re-check security protocol support after reset

In reply to Keith Busch
Quoted 18 lines · Keith Busch

drivers/nvme/host/pci.c static void nvme_reset_work(struct work_struct *work) · line 1796

Quoted 10 lines · Christoph Hellwig
drivers/nvme/host/pci.c +2 -1
@@ -1789,7 +1789,8 @@ static void nvme_reset_work(struct work_struct *work)
 	if (result)
 		goto out;
 
-	if ((dev->ctrl.oacs & NVME_CTRL_OACS_SEC_SUPP) && !dev->ctrl.opal_dev) {
+	kfree(dev->ctrl.opal_dev);
+	if (dev->ctrl.oacs & NVME_CTRL_OACS_SEC_SUPP) {
 		dev->ctrl.opal_dev =
 			init_opal_dev(&dev->ctrl, &nvme_sec_submit);
 	}

A couple things.

This has a use-after-free in opal_unlock_from_suspend if the nvme device had an opal_dev before, but no longer support the capability after resume. So you'd want to set ctrl.opal_dev to NULL after the free.

Quoted 5 lines

But we don't want to unconditionally free it anyway during resume since opal_unlock_from_suspend requires the exisiting opal_dev state information saved in the 'unlk_list'.

Something like this instead:

Yes, that looks fine to me. We'll probably also need the additional fixup Scott pointed out.

#8 lore raw

Re: [PATCH 4/4] nvme: re-check security protocol support after reset

Quoted 19 lines · Christoph Hellwig
Quoted 18 lines · Keith Busch

drivers/nvme/host/pci.c static void nvme_reset_work(struct work_struct *work) · line 1796

Quoted 10 lines · Christoph Hellwig
drivers/nvme/host/pci.c +2 -1
@@ -1789,7 +1789,8 @@ static void nvme_reset_work(struct work_struct *work)
 	if (result)
 		goto out;
 
-	if ((dev->ctrl.oacs & NVME_CTRL_OACS_SEC_SUPP) && !dev->ctrl.opal_dev) {
+	kfree(dev->ctrl.opal_dev);
+	if (dev->ctrl.oacs & NVME_CTRL_OACS_SEC_SUPP) {
 		dev->ctrl.opal_dev =
 			init_opal_dev(&dev->ctrl, &nvme_sec_submit);
 	}

A couple things.

This has a use-after-free in opal_unlock_from_suspend if the nvme device had an opal_dev before, but no longer support the capability after resume. So you'd want to set ctrl.opal_dev to NULL after the free.

Quoted 8 lines
Quoted 5 lines

But we don't want to unconditionally free it anyway during resume since opal_unlock_from_suspend requires the exisiting opal_dev state information saved in the 'unlk_list'.

Something like this instead:

Yes, that looks fine to me. We'll probably also need the additional fixup Scott pointed out.

I'm working on it now. Do you want a diff like Keith did or a separate patch?

author maintainer
#9 lore raw

Re: [PATCH 4/4] nvme: re-check security protocol support after reset

Quoted 1 line · Scott Bauer

I'm working on it now. Do you want a diff like Keith did or a separate patch?

Please make it a proper patch that applies on top of my patches 1-3 (where 3 really is yours anyway). I'll respin 4 with the updates from Keith on top of 1-3 + your patch then.

#10 lore raw
In reply to Christoph Hellwig
Quoted 6 lines · Christoph Hellwig

Hi all,

this contains a few more OPAL-related fixups. It tones down warnings a bit, allocates the OPAL-ѕpecific data structure in a separate dynamic allocation, checks for support of Security Send/Receive in NVMe before using them, and makes sure we re-discovery the security capabilities after each reset.

Patches 1-3 look good to me.

#11 lore raw
In reply to Christoph Hellwig
Quoted 6 lines · Christoph Hellwig

Hi all,

this contains a few more OPAL-related fixups. It tones down warnings a bit, allocates the OPAL-?pecific data structure in a separate dynamic allocation, checks for support of Security Send/Receive in NVMe before using them, and makes sure we re-discovery the security capabilities after each reset.

Applied 1-3, thanks.

Signature · 1 line
Jens Axboe