Skip to contentSkip to conversation

cxl: Support device sanitation

PATCH Superseded Davidlohr Bueso 5 participants 19 messages May 26, 2023 – Jun 1, 2023 CXL

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Davidlohr Bueso

author maintainer
#0 lore raw

v5 cover cxl: Support device sanitation

Hi,

Changes from v4 (https://lore.kernel.org/linux-cxl/[email protected]/):
o Added patch 1 'security/state' sysfs file which will be the placeholder for userspace to know about pmem security or an on-going sanitation op.
o Renamed some of the sanitation polling variables.
o Picked up review tags for mock device test patches.

This adds the sanitation part of the background command handling. Some noteworthy items:

o Treating Sanitation as such a special beast can make the code a bit invasive, but couldn't find a decent alternative. For example I realize that this is really ad-hoc code in __cxl_pci_mbox_send_cmd(). A lot of this also comes from the fact that polling for sanitize is supported, so sw still needs to keep up and serialize.

o Nothing depends explicitly on CPU cacheline management

o All sysfs files/attributes in the security directory are visible.

o Continue to use __ATTR() macros for sysfs attributes instead of the requested DEVICE_ATTR_*() ones because of the naming the security directory, otherwise names don't match.

Patch 1 adds a new security/state file.
Patch 2 paves the required sanitation handling code before actually using it. Patch 3,4 wires up sanitation + unit test.
Patch 5,6 wires up secure erase + unit test.

Testing.
========

o There are the mock device tests for Sanitize and Secure Erase.

o The latest (v2) qemu bg/sanitize support series is posted here:

	https://lore.kernel.org/linux-cxl/[email protected]/

(1) Window where driver is out of sync with hw (Sanitation async polling).

[root@fedora ~]# echo 1 > /sys/bus/cxl/devices/mem0/security/sanitize

[  159.297482] cxl_pci:__cxl_pci_mbox_send_cmd:243: cxl_pci 0000:37:00.0: Sending command: 0x4400
[  159.298648] cxl_pci:cxl_pci_mbox_wait_for_doorbell:73: cxl_pci 0000:37:00.0: Doorbell wait took 0ms
[  159.299908] cxl_pci:__cxl_pci_mbox_send_cmd:295: cxl_pci 0000:37:00.0: Sanitation operation started
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

qemu informs sanitation is done <<<<<

[root@fedora ~]# echo 1 > /sys/bus/cxl/devices/mem0/security/sanitize

[  165.897345] cxl_pci 0000:37:00.0: Failed to sanitize device : -16
[  171.692050] cxl_pci:cxl_mbox_sanitize_work:147: cxl_pci 0000:37:00.0: Sanitation operation ended
[root@fedora ~]# echo 1 > /sys/bus/cxl/devices/mem0/security/sanitize
[  173.373337] cxl_pci:__cxl_pci_mbox_send_cmd:243: cxl_pci 0000:37:00.0: Sending command: 0x4400
[  173.374498] cxl_pci:cxl_pci_mbox_wait_for_doorbell:73: cxl_pci 0000:37:00.0: Doorbell wait took 0ms
[  173.375727] cxl_pci:__cxl_pci_mbox_send_cmd:295: cxl_pci 0000:37:00.0: Sanitation operation started

(2) Perform sanitation of more than one memdev at a time (Sanitation async polling).

[root@fedora ~]# echo 1 > /sys/bus/cxl/devices/mem1/security/sanitize

[  351.287129] cxl_pci:__cxl_pci_mbox_send_cmd:243: cxl_pci 0000:36:00.0: Sending command: 0x4400
[  351.288403] cxl_pci:cxl_pci_mbox_wait_for_doorbell:73: cxl_pci 0000:36:00.0: Doorbell wait took 0ms
[  351.289706] cxl_pci:__cxl_pci_mbox_send_cmd:295: cxl_pci 0000:36:00.0: Sanitation operation started
[root@fedora ~]# echo 1 > /sys/bus/cxl/devices/mem0/security/sanitize
[  353.058614] cxl_pci:__cxl_pci_mbox_send_cmd:243: cxl_pci 0000:37:00.0: Sending command: 0x4400
[  353.059854] cxl_pci:cxl_pci_mbox_wait_for_doorbell:73: cxl_pci 0000:37:00.0: Doorbell wait took 0ms
[  353.061126] cxl_pci:__cxl_pci_mbox_send_cmd:295: cxl_pci 0000:37:00.0: Sanitation operation started
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

qemu informs sanitation is done <<<<<
qemu informs sanitation is done <<<<<

[  363.692138] cxl_pci:cxl_mbox_sanitize_work:147: cxl_pci 0000:36:00.0: Sanitation operation ended
[  365.227416] cxl_pci:cxl_mbox_sanitize_work:147: cxl_pci 0000:37:00.0: Sanitation operation ended

(3) Perform sanitation of more than one memdev at a time (Sanitation async irq).

[root@fedora ~]# echo 1 > /sys/bus/cxl/devices/mem1/security/sanitize

[  193.729821] cxl_pci:__cxl_pci_mbox_send_cmd:243: cxl_pci 0000:c1:00.0: Sending command: 0x4400
[  193.731071] cxl_pci:cxl_pci_mbox_wait_for_doorbell:73: cxl_pci 0000:c1:00.0: Doorbell wait took 0ms
[  193.732360] cxl_pci:__cxl_pci_mbox_send_cmd:295: cxl_pci 0000:c1:00.0: Sanitation operation started
[root@fedora ~]# echo 1 > /sys/bus/cxl/devices/mem0/security/sanitize
[  197.001466] cxl_pci:__cxl_pci_mbox_send_cmd:243: cxl_pci 0000:36:00.0: Sending command: 0x4400
[  197.002694] cxl_pci:cxl_pci_mbox_wait_for_doorbell:73: cxl_pci 0000:36:00.0: Doorbell wait took 0ms
[  197.003956] cxl_pci:__cxl_pci_mbox_send_cmd:295: cxl_pci 0000:36:00.0: Sanitation operation started
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

qemu says sanitation is done <<<<

[  197.731473] cxl_pci:cxl_pci_mbox_irq:119: cxl_pci 0000:c1:00.0: Sanitation operation ended
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

qemu says sanitation is done <<<<

[  201.003258] cxl_pci:cxl_pci_mbox_irq:119: cxl_pci 0000:36:00.0: Sanitation operation ended

(4) Forbid new sanitation while one is in progress (Sanitation async irq).

[root@fedora ~]# cat /sys/bus/cxl/devices/mem0/security/state disabled
[root@fedora ~]# echo 1 > /sys/bus/cxl/devices/mem0/security/sanitize

[   39.284258] cxl_pci:__cxl_pci_mbox_send_cmd:243: cxl_pci 0000:36:00.0: Sending command: 0x4400
[   39.285459] cxl_pci:cxl_pci_mbox_wait_for_doorbell:73: cxl_pci 0000:36:00.0: Doorbell wait took 0ms
[   39.286723] cxl_pci:__cxl_pci_mbox_send_cmd:295: cxl_pci 0000:36:00.0: Sanitation operation started
[root@fedora ~]# cat /sys/bus/cxl/devices/mem0/security/state
sanitize
[root@fedora ~]# echo 1 > /sys/bus/cxl/devices/mem0/security/sanitize
[   42.697129] cxl_pci:__cxl_pci_mbox_send_cmd:243: cxl_pci 0000:36:00.0: Sending command: 0x4400
[   42.698323] cxl_pci:cxl_pci_mbox_wait_for_doorbell:73: cxl_pci 0000:36:00.0: Doorbell wait took 0ms
[   42.699525] cxl_pci:__cxl_pci_mbox_send_cmd:335: cxl_pci 0000:36:00.0: Mailbox operation had an error: ongoing background operation
[   42.701119] cxl_pci 0000:36:00.0: Failed to sanitize device : -6
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

qemu says sanitation is done <<<<

[   43.285334] cxl_pci:cxl_pci_mbox_irq:119: cxl_pci 0000:36:00.0: Sanitation operation ended

Applies against 'for-6.5/cxl-background' from cxl.git.

Please consider for v6.5.

Thanks!

Davidlohr Bueso (6):
cxl/mem: Introduce security state sysfs file
cxl/mbox: Add sanitation handling machinery
cxl/mem: Wire up Sanitation support
cxl/test: Add Sanitize opcode support
cxl/mem: Support Secure Erase
cxl/test: Add Secure Erase opcode support

 Documentation/ABI/testing/sysfs-bus-cxl |  37 +++++++
 drivers/cxl/core/mbox.c                 |  59 ++++++++++
 drivers/cxl/core/memdev.c               | 139 ++++++++++++++++++++++++
 drivers/cxl/cxlmem.h                    |  26 +++++
 drivers/cxl/pci.c                       |  88 ++++++++++++++-
 drivers/cxl/security.c                  |   3 +
 tools/testing/cxl/test/mem.c            |  52 +++++++++
 7 files changed, 400 insertions(+), 4 deletions(-)

--
2.40.1

author maintainer
#1 lore raw

PATCH v5 1/6 cxl/mem: Introduce security state sysfs file

Add a read-only sysfs file to display the security state of a device (currently only pmem):

/sys/bus/cxl/devices/memX/security/state

This introduces a cxl_security_state structure that is to be the placeholder for common CXL security features.

---
 Documentation/ABI/testing/sysfs-bus-cxl | 10 ++++++
 drivers/cxl/core/memdev.c               | 46 +++++++++++++++++++++++++
 drivers/cxl/cxlmem.h                    | 11 ++++++
 drivers/cxl/security.c                  |  3 ++
 4 files changed, 70 insertions(+)
Documentation/ABI/testing/sysfs-bus-cxl +10 -0
index 48ac0d911801..721a44d8a482 100644
@@ -58,6 +58,16 @@ Description:
 		affinity for this device.
 
 
+What:		/sys/bus/cxl/devices/memX/security/state
+Date:		June, 2023
+KernelVersion:	v6.5
+Contact:	[email protected]
+Description:
+		(RO) Reading this file will display the CXL security state for
+		that device. Such states can be: 'disabled', or those available
+		only for persistent memory: 'locked', 'unlocked' or 'frozen'.
+
+
 What:		/sys/bus/cxl/devices/*/devtype
 Date:		June, 2021
 KernelVersion:	v5.14
drivers/cxl/core/memdev.c +46 -0

80 lines not on this page · raw message

drivers/cxl/cxlmem.h +11 -0
index 1d8e81c87c6a..5329274b0076 100644
@@ -260,6 +260,15 @@ struct cxl_poison_state {
 	struct mutex lock;  /* Protect reads of poison list */
 };
 
+/**
+ * struct cxl_security_state - Device security state
+ *
+ * @state: state of last security operation
+ */
+struct cxl_security_state {
+	unsigned long state;
+};
+
 /**
  * struct cxl_dev_state - The driver device state
  *
@@ -297,6 +306,7 @@ struct cxl_poison_state {
  * @serial: PCIe Device Serial Number
  * @event: event log driver state
  * @poison: poison driver state info
+ * @security: device security state
  * @mbox_send: @dev specific transport for transmitting mailbox commands
  *
  * See section 8.2.9.5.2 Capacity Configuration and Label Storage for
@@ -336,6 +346,7 @@ struct cxl_dev_state {
 
 	struct cxl_event_state event;
 	struct cxl_poison_state poison;
+	struct cxl_security_state security;
 
 	struct rcuwait mbox_wait;
 	int (*mbox_send)(struct cxl_dev_state *cxlds, struct cxl_mbox_cmd *cmd);
drivers/cxl/security.c +3 -0
index 4ad4bda2d18e..9da6785dfd31 100644
@@ -34,6 +34,9 @@ static unsigned long cxl_pmem_get_security_flags(struct nvdimm *nvdimm,
 		return 0;
 
 	sec_out = le32_to_cpu(out.flags);
+	/* cache security state */
+	cxlds->security.state = sec_out;
+
 	if (ptype == NVDIMM_MASTER) {
 		if (sec_out & CXL_PMEM_SEC_STATE_MASTER_PASS_SET)
 			set_bit(NVDIMM_SECURITY_UNLOCKED, &security_flags);
Signature · 1 line
2.40.1
maintainer
#2 lore raw

Re: [PATCH 1/6] cxl/mem: Introduce security state sysfs file

Quoted 100 lines · Davidlohr Bueso

Add a read-only sysfs file to display the security state of a device (currently only pmem):

/sys/bus/cxl/devices/memX/security/state

This introduces a cxl_security_state structure that is to be the placeholder for common CXL security features.

---
  Documentation/ABI/testing/sysfs-bus-cxl | 10 ++++++
  drivers/cxl/core/memdev.c               | 46 +++++++++++++++++++++++++
  drivers/cxl/cxlmem.h                    | 11 ++++++
  drivers/cxl/security.c                  |  3 ++
  4 files changed, 70 insertions(+)
Documentation/ABI/testing/sysfs-bus-cxl +10 -0
index 48ac0d911801..721a44d8a482 100644
@@ -58,6 +58,16 @@ Description:
  		affinity for this device.
 
 
+What:		/sys/bus/cxl/devices/memX/security/state
+Date:		June, 2023
+KernelVersion:	v6.5
+Contact:	[email protected]
+Description:
+		(RO) Reading this file will display the CXL security state for
+		that device. Such states can be: 'disabled', or those available
+		only for persistent memory: 'locked', 'unlocked' or 'frozen'.
+
+
  What:		/sys/bus/cxl/devices/*/devtype
  Date:		June, 2021
  KernelVersion:	v5.14
drivers/cxl/core/memdev.c +33 -0
index 057a43267290..6e1d7d3610a2 100644
@@ -107,6 +107,28 @@ static ssize_t numa_node_show(struct device *dev, struct device_attribute *attr,
  }
  static DEVICE_ATTR_RO(numa_node);
 
+static ssize_t security_state_show(struct device *dev,
+				   struct device_attribute *attr,
+				   char *buf)
+{
+	struct cxl_memdev *cxlmd = to_cxl_memdev(dev);
+	struct cxl_dev_state *cxlds = cxlmd->cxlds;
+	unsigned long state = cxlds->security.state;
+
+	if (!(state & CXL_PMEM_SEC_STATE_USER_PASS_SET))
+		return sysfs_emit(buf, "disabled\n");
+	if (state & CXL_PMEM_SEC_STATE_FROZEN ||
+	    state & CXL_PMEM_SEC_STATE_MASTER_PLIMIT ||
+	    state & CXL_PMEM_SEC_STATE_USER_PLIMIT)
+		return sysfs_emit(buf, "frozen\n");
+	if (state & CXL_PMEM_SEC_STATE_LOCKED)
+		return sysfs_emit(buf, "locked\n");
+	else
+		return sysfs_emit(buf, "unlocked\n");
+}
+static struct device_attribute dev_attr_security_state =
+	__ATTR(state, 0444, security_state_show, NULL);
+
  static int cxl_get_poison_by_memdev(struct cxl_memdev *cxlmd)
  {
  	struct cxl_dev_state *cxlds = cxlmd->cxlds;
@@ -352,6 +374,11 @@ static struct attribute *cxl_memdev_ram_attributes[] = {
  	NULL,
  };
 
+static struct attribute *cxl_memdev_security_attributes[] = {
+	&dev_attr_security_state.attr,
+	NULL,
+};
+
  static umode_t cxl_memdev_visible(struct kobject *kobj, struct attribute *a,
  				  int n)
  {
@@ -375,10 +402,16 @@ static struct attribute_group cxl_memdev_pmem_attribute_group = {
  	.attrs = cxl_memdev_pmem_attributes,
  };
 
+static struct attribute_group cxl_memdev_security_attribute_group = {
+	.name = "security",
+	.attrs = cxl_memdev_security_attributes,
+};
+
  static const struct attribute_group *cxl_memdev_attribute_groups[] = {
  	&cxl_memdev_attribute_group,
  	&cxl_memdev_ram_attribute_group,
  	&cxl_memdev_pmem_attribute_group,
+	&cxl_memdev_security_attribute_group,
  	NULL,
  };
 

drivers/cxl/core/memdev.c static const struct file_operations cxl_memdev_fops = { · line 591

Quoted 8 lines
drivers/cxl/core/memdev.c +5 -0
@@ -551,6 +584,15 @@ static const struct file_operations cxl_memdev_fops = {
  	.llseek = noop_llseek,
  };
 
+static int cxl_memdev_security_init(struct cxl_memdev *cxlmd)
+{
+	struct cxl_dev_state *cxlds = cxlmd->cxlds;
+
+	cxlds->security.state = 0;

This is not necessary with cxlds allocated with devm_kzalloc()?

Quoted 68 lines
drivers/cxl/core/memdev.c +8 -0
+
+	return 0;
+}
+
  struct cxl_memdev *devm_cxl_add_memdev(struct cxl_dev_state *cxlds)
  {
  	struct cxl_memdev *cxlmd;
@@ -579,6 +621,10 @@ struct cxl_memdev *devm_cxl_add_memdev(struct cxl_dev_state *cxlds)
  	if (rc)
  		goto err;
 
+	rc = cxl_memdev_security_init(cxlmd);
+	if (rc)
+		goto err;
+
  	rc = devm_add_action_or_reset(cxlds->dev, cxl_memdev_unregister, cxlmd);
  	if (rc)
  		return ERR_PTR(rc);
drivers/cxl/cxlmem.h +11 -0
index 1d8e81c87c6a..5329274b0076 100644
@@ -260,6 +260,15 @@ struct cxl_poison_state {
  	struct mutex lock;  /* Protect reads of poison list */
  };
 
+/**
+ * struct cxl_security_state - Device security state
+ *
+ * @state: state of last security operation
+ */
+struct cxl_security_state {
+	unsigned long state;
+};
+
  /**
   * struct cxl_dev_state - The driver device state
   *
@@ -297,6 +306,7 @@ struct cxl_poison_state {
   * @serial: PCIe Device Serial Number
   * @event: event log driver state
   * @poison: poison driver state info
+ * @security: device security state
   * @mbox_send: @dev specific transport for transmitting mailbox commands
   *
   * See section 8.2.9.5.2 Capacity Configuration and Label Storage for
@@ -336,6 +346,7 @@ struct cxl_dev_state {
 
  	struct cxl_event_state event;
  	struct cxl_poison_state poison;
+	struct cxl_security_state security;
 
  	struct rcuwait mbox_wait;
  	int (*mbox_send)(struct cxl_dev_state *cxlds, struct cxl_mbox_cmd *cmd);
drivers/cxl/security.c +3 -0
index 4ad4bda2d18e..9da6785dfd31 100644
@@ -34,6 +34,9 @@ static unsigned long cxl_pmem_get_security_flags(struct nvdimm *nvdimm,
  		return 0;
 
  	sec_out = le32_to_cpu(out.flags);
+	/* cache security state */
+	cxlds->security.state = sec_out;
+
  	if (ptype == NVDIMM_MASTER) {
  		if (sec_out & CXL_PMEM_SEC_STATE_MASTER_PASS_SET)
  			set_bit(NVDIMM_SECURITY_UNLOCKED, &security_flags);
Reviewed-by on v5 1/6
#3 lore raw

Re: [PATCH 1/6] cxl/mem: Introduce security state sysfs file

In reply to Davidlohr Bueso
Quoted 9 lines · On Thu, 25 May 2023 20:33:39 -0700 Davidlohr Bueso <[email protected]> wrote:

Add a read-only sysfs file to display the security state of a device (currently only pmem):

/sys/bus/cxl/devices/memX/security/state

This introduces a cxl_security_state structure that is to be the placeholder for common CXL security features.

Nothing to add to Dave's review. Given comment is minor, either way...

Quoted 167 lines
---
 Documentation/ABI/testing/sysfs-bus-cxl | 10 ++++++
 drivers/cxl/core/memdev.c               | 46 +++++++++++++++++++++++++
 drivers/cxl/cxlmem.h                    | 11 ++++++
 drivers/cxl/security.c                  |  3 ++
 4 files changed, 70 insertions(+)
Documentation/ABI/testing/sysfs-bus-cxl +10 -0
index 48ac0d911801..721a44d8a482 100644
@@ -58,6 +58,16 @@ Description:
 		affinity for this device.
 
 
+What:		/sys/bus/cxl/devices/memX/security/state
+Date:		June, 2023
+KernelVersion:	v6.5
+Contact:	[email protected]
+Description:
+		(RO) Reading this file will display the CXL security state for
+		that device. Such states can be: 'disabled', or those available
+		only for persistent memory: 'locked', 'unlocked' or 'frozen'.
+
+
 What:		/sys/bus/cxl/devices/*/devtype
 Date:		June, 2021
 KernelVersion:	v5.14
drivers/cxl/core/memdev.c +46 -0
index 057a43267290..6e1d7d3610a2 100644
@@ -107,6 +107,28 @@ static ssize_t numa_node_show(struct device *dev, struct device_attribute *attr,
 }
 static DEVICE_ATTR_RO(numa_node);
 
+static ssize_t security_state_show(struct device *dev,
+				   struct device_attribute *attr,
+				   char *buf)
+{
+	struct cxl_memdev *cxlmd = to_cxl_memdev(dev);
+	struct cxl_dev_state *cxlds = cxlmd->cxlds;
+	unsigned long state = cxlds->security.state;
+
+	if (!(state & CXL_PMEM_SEC_STATE_USER_PASS_SET))
+		return sysfs_emit(buf, "disabled\n");
+	if (state & CXL_PMEM_SEC_STATE_FROZEN ||
+	    state & CXL_PMEM_SEC_STATE_MASTER_PLIMIT ||
+	    state & CXL_PMEM_SEC_STATE_USER_PLIMIT)
+		return sysfs_emit(buf, "frozen\n");
+	if (state & CXL_PMEM_SEC_STATE_LOCKED)
+		return sysfs_emit(buf, "locked\n");
+	else
+		return sysfs_emit(buf, "unlocked\n");
+}
+static struct device_attribute dev_attr_security_state =
+	__ATTR(state, 0444, security_state_show, NULL);
+
 static int cxl_get_poison_by_memdev(struct cxl_memdev *cxlmd)
 {
 	struct cxl_dev_state *cxlds = cxlmd->cxlds;
@@ -352,6 +374,11 @@ static struct attribute *cxl_memdev_ram_attributes[] = {
 	NULL,
 };
 
+static struct attribute *cxl_memdev_security_attributes[] = {
+	&dev_attr_security_state.attr,
+	NULL,
+};
+
 static umode_t cxl_memdev_visible(struct kobject *kobj, struct attribute *a,
 				  int n)
 {
@@ -375,10 +402,16 @@ static struct attribute_group cxl_memdev_pmem_attribute_group = {
 	.attrs = cxl_memdev_pmem_attributes,
 };
 
+static struct attribute_group cxl_memdev_security_attribute_group = {
+	.name = "security",
+	.attrs = cxl_memdev_security_attributes,
+};
+
 static const struct attribute_group *cxl_memdev_attribute_groups[] = {
 	&cxl_memdev_attribute_group,
 	&cxl_memdev_ram_attribute_group,
 	&cxl_memdev_pmem_attribute_group,
+	&cxl_memdev_security_attribute_group,
 	NULL,
 };
 
@@ -551,6 +584,15 @@ static const struct file_operations cxl_memdev_fops = {
 	.llseek = noop_llseek,
 };
 
+static int cxl_memdev_security_init(struct cxl_memdev *cxlmd)
+{
+	struct cxl_dev_state *cxlds = cxlmd->cxlds;
+
+	cxlds->security.state = 0;
+
+	return 0;
+}
+
 struct cxl_memdev *devm_cxl_add_memdev(struct cxl_dev_state *cxlds)
 {
 	struct cxl_memdev *cxlmd;
@@ -579,6 +621,10 @@ struct cxl_memdev *devm_cxl_add_memdev(struct cxl_dev_state *cxlds)
 	if (rc)
 		goto err;
 
+	rc = cxl_memdev_security_init(cxlmd);
+	if (rc)
+		goto err;
+
 	rc = devm_add_action_or_reset(cxlds->dev, cxl_memdev_unregister, cxlmd);
 	if (rc)
 		return ERR_PTR(rc);
drivers/cxl/cxlmem.h +11 -0
index 1d8e81c87c6a..5329274b0076 100644
@@ -260,6 +260,15 @@ struct cxl_poison_state {
 	struct mutex lock;  /* Protect reads of poison list */
 };
 
+/**
+ * struct cxl_security_state - Device security state
+ *
+ * @state: state of last security operation
+ */
+struct cxl_security_state {
+	unsigned long state;
+};
+
 /**
  * struct cxl_dev_state - The driver device state
  *
@@ -297,6 +306,7 @@ struct cxl_poison_state {
  * @serial: PCIe Device Serial Number
  * @event: event log driver state
  * @poison: poison driver state info
+ * @security: device security state
  * @mbox_send: @dev specific transport for transmitting mailbox commands
  *
  * See section 8.2.9.5.2 Capacity Configuration and Label Storage for
@@ -336,6 +346,7 @@ struct cxl_dev_state {
 
 	struct cxl_event_state event;
 	struct cxl_poison_state poison;
+	struct cxl_security_state security;
 
 	struct rcuwait mbox_wait;
 	int (*mbox_send)(struct cxl_dev_state *cxlds, struct cxl_mbox_cmd *cmd);
drivers/cxl/security.c +3 -0
index 4ad4bda2d18e..9da6785dfd31 100644
@@ -34,6 +34,9 @@ static unsigned long cxl_pmem_get_security_flags(struct nvdimm *nvdimm,
 		return 0;
 
 	sec_out = le32_to_cpu(out.flags);
+	/* cache security state */
+	cxlds->security.state = sec_out;
+
 	if (ptype == NVDIMM_MASTER) {
 		if (sec_out & CXL_PMEM_SEC_STATE_MASTER_PASS_SET)
 			set_bit(NVDIMM_SECURITY_UNLOCKED, &security_flags);
Reviewed-by on v5 1/6
#4 lore raw

Re: [PATCH 1/6] cxl/mem: Introduce security state sysfs file

In reply to Davidlohr Bueso
Quoted 10 lines · Davidlohr Bueso

Add a read-only sysfs file to display the security state of a device (currently only pmem):

/sys/bus/cxl/devices/memX/security/state

This introduces a cxl_security_state structure that is to be the placeholder for common CXL security features.

---

Quoted 170 lines
 Documentation/ABI/testing/sysfs-bus-cxl | 10 ++++++
 drivers/cxl/core/memdev.c               | 46 +++++++++++++++++++++++++
 drivers/cxl/cxlmem.h                    | 11 ++++++
 drivers/cxl/security.c                  |  3 ++
 4 files changed, 70 insertions(+)
Documentation/ABI/testing/sysfs-bus-cxl +10 -0
index 48ac0d911801..721a44d8a482 100644
@@ -58,6 +58,16 @@ Description:
 		affinity for this device.
 
 
+What:		/sys/bus/cxl/devices/memX/security/state
+Date:		June, 2023
+KernelVersion:	v6.5
+Contact:	[email protected]
+Description:
+		(RO) Reading this file will display the CXL security state for
+		that device. Such states can be: 'disabled', or those available
+		only for persistent memory: 'locked', 'unlocked' or 'frozen'.
+
+
 What:		/sys/bus/cxl/devices/*/devtype
 Date:		June, 2021
 KernelVersion:	v5.14
drivers/cxl/core/memdev.c +46 -0
index 057a43267290..6e1d7d3610a2 100644
@@ -107,6 +107,28 @@ static ssize_t numa_node_show(struct device *dev, struct device_attribute *attr,
 }
 static DEVICE_ATTR_RO(numa_node);
 
+static ssize_t security_state_show(struct device *dev,
+				   struct device_attribute *attr,
+				   char *buf)
+{
+	struct cxl_memdev *cxlmd = to_cxl_memdev(dev);
+	struct cxl_dev_state *cxlds = cxlmd->cxlds;
+	unsigned long state = cxlds->security.state;
+
+	if (!(state & CXL_PMEM_SEC_STATE_USER_PASS_SET))
+		return sysfs_emit(buf, "disabled\n");
+	if (state & CXL_PMEM_SEC_STATE_FROZEN ||
+	    state & CXL_PMEM_SEC_STATE_MASTER_PLIMIT ||
+	    state & CXL_PMEM_SEC_STATE_USER_PLIMIT)
+		return sysfs_emit(buf, "frozen\n");
+	if (state & CXL_PMEM_SEC_STATE_LOCKED)
+		return sysfs_emit(buf, "locked\n");
+	else
+		return sysfs_emit(buf, "unlocked\n");
+}
+static struct device_attribute dev_attr_security_state =
+	__ATTR(state, 0444, security_state_show, NULL);
+
 static int cxl_get_poison_by_memdev(struct cxl_memdev *cxlmd)
 {
 	struct cxl_dev_state *cxlds = cxlmd->cxlds;
@@ -352,6 +374,11 @@ static struct attribute *cxl_memdev_ram_attributes[] = {
 	NULL,
 };
 
+static struct attribute *cxl_memdev_security_attributes[] = {
+	&dev_attr_security_state.attr,
+	NULL,
+};
+
 static umode_t cxl_memdev_visible(struct kobject *kobj, struct attribute *a,
 				  int n)
 {
@@ -375,10 +402,16 @@ static struct attribute_group cxl_memdev_pmem_attribute_group = {
 	.attrs = cxl_memdev_pmem_attributes,
 };
 
+static struct attribute_group cxl_memdev_security_attribute_group = {
+	.name = "security",
+	.attrs = cxl_memdev_security_attributes,
+};
+
 static const struct attribute_group *cxl_memdev_attribute_groups[] = {
 	&cxl_memdev_attribute_group,
 	&cxl_memdev_ram_attribute_group,
 	&cxl_memdev_pmem_attribute_group,
+	&cxl_memdev_security_attribute_group,
 	NULL,
 };
 
@@ -551,6 +584,15 @@ static const struct file_operations cxl_memdev_fops = {
 	.llseek = noop_llseek,
 };
 
+static int cxl_memdev_security_init(struct cxl_memdev *cxlmd)
+{
+	struct cxl_dev_state *cxlds = cxlmd->cxlds;
+
+	cxlds->security.state = 0;
+
+	return 0;
+}
+
 struct cxl_memdev *devm_cxl_add_memdev(struct cxl_dev_state *cxlds)
 {
 	struct cxl_memdev *cxlmd;
@@ -579,6 +621,10 @@ struct cxl_memdev *devm_cxl_add_memdev(struct cxl_dev_state *cxlds)
 	if (rc)
 		goto err;
 
+	rc = cxl_memdev_security_init(cxlmd);
+	if (rc)
+		goto err;
+
 	rc = devm_add_action_or_reset(cxlds->dev, cxl_memdev_unregister, cxlmd);
 	if (rc)
 		return ERR_PTR(rc);
drivers/cxl/cxlmem.h +11 -0
index 1d8e81c87c6a..5329274b0076 100644
@@ -260,6 +260,15 @@ struct cxl_poison_state {
 	struct mutex lock;  /* Protect reads of poison list */
 };
 
+/**
+ * struct cxl_security_state - Device security state
+ *
+ * @state: state of last security operation
+ */
+struct cxl_security_state {
+	unsigned long state;
+};
+
 /**
  * struct cxl_dev_state - The driver device state
  *
@@ -297,6 +306,7 @@ struct cxl_poison_state {
  * @serial: PCIe Device Serial Number
  * @event: event log driver state
  * @poison: poison driver state info
+ * @security: device security state
  * @mbox_send: @dev specific transport for transmitting mailbox commands
  *
  * See section 8.2.9.5.2 Capacity Configuration and Label Storage for
@@ -336,6 +346,7 @@ struct cxl_dev_state {
 
 	struct cxl_event_state event;
 	struct cxl_poison_state poison;
+	struct cxl_security_state security;
 
 	struct rcuwait mbox_wait;
 	int (*mbox_send)(struct cxl_dev_state *cxlds, struct cxl_mbox_cmd *cmd);
drivers/cxl/security.c +3 -0
index 4ad4bda2d18e..9da6785dfd31 100644
@@ -34,6 +34,9 @@ static unsigned long cxl_pmem_get_security_flags(struct nvdimm *nvdimm,
 		return 0;
 
 	sec_out = le32_to_cpu(out.flags);
+	/* cache security state */
+	cxlds->security.state = sec_out;
+
 	if (ptype == NVDIMM_MASTER) {
 		if (sec_out & CXL_PMEM_SEC_STATE_MASTER_PASS_SET)
 			set_bit(NVDIMM_SECURITY_UNLOCKED, &security_flags);
Signature · 1 line
2.40.1
author maintainer
#5 lore raw

PATCH v5 2/6 cxl/mbox: Add sanitation handling machinery

In reply to Davidlohr Bueso

Sanitation is by definition a device-monopolizing operation, and thus the timeslicing rules for other background commands do not apply. As such handle this special case asynchronously and return immediately. Subsequent changes will allow completion to be pollable from userspace via a sysfs file interface.

For devices that don't support interrupts for notifying background command completion, self-poll with the caveat that the poller can be out of sync with the ready hardware, and therefore care must be taken to not allow any new commands to go through until the poller sees the hw completion. The poller takes the mbox_mutex to stabilize the flagging, minimizing any runtime overhead in the send path to check for 'sanitize_tmo' for uncommon poll scenarios. This flag also serves for sanitation (the only user of async polling) to know when to queue work or simply rely on irqs.

The irq case is much simpler as hardware will serialize/error appropriately.

---
 drivers/cxl/core/memdev.c | 10 +++++
 drivers/cxl/cxlmem.h      | 10 +++++
 drivers/cxl/pci.c         | 83 +++++++++++++++++++++++++++++++++++++--
 3 files changed, 99 insertions(+), 4 deletions(-)
drivers/cxl/core/memdev.c +10 -0
index 6e1d7d3610a2..02763e83545c 100644
@@ -460,11 +460,21 @@ void clear_exclusive_cxl_commands(struct cxl_dev_state *cxlds, unsigned long *cm
 }
 EXPORT_SYMBOL_NS_GPL(clear_exclusive_cxl_commands, CXL);
 
+static void cxl_memdev_security_shutdown(struct device *dev)
+{
+	struct cxl_memdev *cxlmd = to_cxl_memdev(dev);
+	struct cxl_dev_state *cxlds = cxlmd->cxlds;
+
+	if (cxlds->security.poll_tmo_secs != -1)
+		cancel_delayed_work_sync(&cxlds->security.poll_dwork);
+}
+
 static void cxl_memdev_shutdown(struct device *dev)
 {
 	struct cxl_memdev *cxlmd = to_cxl_memdev(dev);
 
 	down_write(&cxl_memdev_rwsem);
+	cxl_memdev_security_shutdown(dev);
 	cxlmd->cxlds = NULL;
 	up_write(&cxl_memdev_rwsem);
 }
drivers/cxl/cxlmem.h +10 -0
index 5329274b0076..02ec68f97de2 100644
@@ -264,9 +264,18 @@ struct cxl_poison_state {
  * struct cxl_security_state - Device security state
  *
  * @state: state of last security operation
+ * @poll_tmo_secs: polling timeout
+ * @poll_dwork: polling work item
+ *
+ * Polling (sanitation) is only used when device mbox irqs are not
+ * supported. As such, @poll_tmo_secs == -1 indicates that polling
+ * is disabled. Otherwise, when enabled, @poll_tmo_secs is maxed
+ * at 15 minutes and serialized by the mbox_mutex.
  */
 struct cxl_security_state {
 	unsigned long state;
+	int poll_tmo_secs;
+	struct delayed_work poll_dwork;
 };
 
 /**
@@ -380,6 +389,7 @@ enum cxl_opcode {
 	CXL_MBOX_OP_GET_SCAN_MEDIA_CAPS	= 0x4303,
 	CXL_MBOX_OP_SCAN_MEDIA		= 0x4304,
 	CXL_MBOX_OP_GET_SCAN_MEDIA	= 0x4305,
+	CXL_MBOX_OP_SANITIZE		= 0x4400,
 	CXL_MBOX_OP_GET_SECURITY_STATE	= 0x4500,
 	CXL_MBOX_OP_SET_PASSPHRASE	= 0x4501,
 	CXL_MBOX_OP_DISABLE_PASSPHRASE	= 0x4502,
drivers/cxl/pci.c +79 -4

128 lines not on this page · raw message

Signature · 1 line
2.40.1
maintainer Reviewed-by on v5 2/6
#6 lore raw

Re: [PATCH 2/6] cxl/mbox: Add sanitation handling machinery

Quoted 16 lines · Davidlohr Bueso

Sanitation is by definition a device-monopolizing operation, and thus the timeslicing rules for other background commands do not apply. As such handle this special case asynchronously and return immediately. Subsequent changes will allow completion to be pollable from userspace via a sysfs file interface.

For devices that don't support interrupts for notifying background command completion, self-poll with the caveat that the poller can be out of sync with the ready hardware, and therefore care must be taken to not allow any new commands to go through until the poller sees the hw completion. The poller takes the mbox_mutex to stabilize the flagging, minimizing any runtime overhead in the send path to check for 'sanitize_tmo' for uncommon poll scenarios. This flag also serves for sanitation (the only user of async polling) to know when to queue work or simply rely on irqs.

Quoted 4 lines

The irq case is much simpler as hardware will serialize/error appropriately.

Just a minor nit below, otherwise

Quoted 185 lines
---
  drivers/cxl/core/memdev.c | 10 +++++
  drivers/cxl/cxlmem.h      | 10 +++++
  drivers/cxl/pci.c         | 83 +++++++++++++++++++++++++++++++++++++--
  3 files changed, 99 insertions(+), 4 deletions(-)
drivers/cxl/core/memdev.c +10 -0
index 6e1d7d3610a2..02763e83545c 100644
@@ -460,11 +460,21 @@ void clear_exclusive_cxl_commands(struct cxl_dev_state *cxlds, unsigned long *cm
  }
  EXPORT_SYMBOL_NS_GPL(clear_exclusive_cxl_commands, CXL);
 
+static void cxl_memdev_security_shutdown(struct device *dev)
+{
+	struct cxl_memdev *cxlmd = to_cxl_memdev(dev);
+	struct cxl_dev_state *cxlds = cxlmd->cxlds;
+
+	if (cxlds->security.poll_tmo_secs != -1)
+		cancel_delayed_work_sync(&cxlds->security.poll_dwork);
+}
+
  static void cxl_memdev_shutdown(struct device *dev)
  {
  	struct cxl_memdev *cxlmd = to_cxl_memdev(dev);
 
  	down_write(&cxl_memdev_rwsem);
+	cxl_memdev_security_shutdown(dev);
  	cxlmd->cxlds = NULL;
  	up_write(&cxl_memdev_rwsem);
  }
drivers/cxl/cxlmem.h +10 -0
index 5329274b0076..02ec68f97de2 100644
@@ -264,9 +264,18 @@ struct cxl_poison_state {
   * struct cxl_security_state - Device security state
   *
   * @state: state of last security operation
+ * @poll_tmo_secs: polling timeout
+ * @poll_dwork: polling work item
+ *
+ * Polling (sanitation) is only used when device mbox irqs are not
+ * supported. As such, @poll_tmo_secs == -1 indicates that polling
+ * is disabled. Otherwise, when enabled, @poll_tmo_secs is maxed
+ * at 15 minutes and serialized by the mbox_mutex.
   */
  struct cxl_security_state {
  	unsigned long state;
+	int poll_tmo_secs;
+	struct delayed_work poll_dwork;
  };
 
  /**
@@ -380,6 +389,7 @@ enum cxl_opcode {
  	CXL_MBOX_OP_GET_SCAN_MEDIA_CAPS	= 0x4303,
  	CXL_MBOX_OP_SCAN_MEDIA		= 0x4304,
  	CXL_MBOX_OP_GET_SCAN_MEDIA	= 0x4305,
+	CXL_MBOX_OP_SANITIZE		= 0x4400,
  	CXL_MBOX_OP_GET_SECURITY_STATE	= 0x4500,
  	CXL_MBOX_OP_SET_PASSPHRASE	= 0x4501,
  	CXL_MBOX_OP_DISABLE_PASSPHRASE	= 0x4502,
drivers/cxl/pci.c +74 -4
index a78e40e6d0e0..a0d93719ab18 100644
@@ -115,16 +115,52 @@ static bool cxl_mbox_background_complete(struct cxl_dev_state *cxlds)
 
  static irqreturn_t cxl_pci_mbox_irq(int irq, void *id)
  {
+	u64 reg;
+	u16 opcode;
  	struct cxl_dev_id *dev_id = id;
  	struct cxl_dev_state *cxlds = dev_id->cxlds;
 
-	/* short-circuit the wait in __cxl_pci_mbox_send_cmd() */
-	if (cxl_mbox_background_complete(cxlds))
-		rcuwait_wake_up(&cxlds->mbox_wait);
+	if (!cxl_mbox_background_complete(cxlds))
+		goto done;
 
+	reg = readq(cxlds->regs.mbox + CXLDEV_MBOX_BG_CMD_STATUS_OFFSET);
+	opcode = FIELD_GET(CXLDEV_MBOX_BG_CMD_COMMAND_OPCODE_MASK, reg);
+	if (opcode == CXL_MBOX_OP_SANITIZE) {
+		dev_dbg(cxlds->dev, "Sanitation operation ended\n");
+	} else {
+		/* short-circuit the wait in __cxl_pci_mbox_send_cmd() */
+		rcuwait_wake_up(&cxlds->mbox_wait);
+	}
+done:
  	return IRQ_HANDLED;
  }
 
+/*
+ * Sanitation operation polling mode.
+ */
+static void cxl_mbox_sanitize_work(struct work_struct *work)
+{
+	struct cxl_dev_state *cxlds;
+
+	cxlds = container_of(work,
+			     struct cxl_dev_state, security.poll_dwork.work);
+
+	mutex_lock(&cxlds->mbox_mutex);
+	if (cxl_mbox_background_complete(cxlds)) {
+		cxlds->security.poll_tmo_secs = 0;
+		put_device(cxlds->dev);
+
+		dev_dbg(cxlds->dev, "Sanitation operation ended\n");
+	} else {
+		int timeout = cxlds->security.poll_tmo_secs + 10;
+
+		cxlds->security.poll_tmo_secs = min(15 * 60, timeout);
+		queue_delayed_work(system_wq, &cxlds->security.poll_dwork,
+				   timeout * HZ);
+	}
+	mutex_unlock(&cxlds->mbox_mutex);
+}
+
  /**
   * __cxl_pci_mbox_send_cmd() - Execute a mailbox command
   * @cxlds: The device state to communicate with.
@@ -185,6 +221,16 @@ static int __cxl_pci_mbox_send_cmd(struct cxl_dev_state *cxlds,
  		return -EBUSY;
  	}
 
+	/*
+	 * With sanitize polling, hardware might be done and the poller still
+	 * not be in sync. Ensure no new command comes in until so. Keep the
+	 * hardware semantics and only allow device health status.
+	 */
+	if (unlikely(cxlds->security.poll_tmo_secs > 0)) {
+		if (mbox_cmd->opcode != CXL_MBOX_OP_GET_HEALTH_INFO)
+			return -EBUSY;
+	}
+
  	cmd_reg = FIELD_PREP(CXLDEV_MBOX_CMD_COMMAND_OPCODE_MASK,
  			     mbox_cmd->opcode);
  	if (mbox_cmd->size_in) {
@@ -233,11 +279,34 @@ static int __cxl_pci_mbox_send_cmd(struct cxl_dev_state *cxlds,
  	 */
  	if (mbox_cmd->return_code == CXL_MBOX_CMD_RC_BACKGROUND) {
  		u64 bg_status_reg;
-		int i, timeout = mbox_cmd->poll_interval_ms;
+		int i, timeout;
+
+		/*
+		 * Sanitation is a special case which monopolizes the device
+		 * and cannot be timesliced. Handle asynchronously instead,
+		 * and allow userspace to poll(2) for completion.
+		 */
+		if (mbox_cmd->opcode == CXL_MBOX_OP_SANITIZE) {
+			if (cxlds->security.poll_tmo_secs != -1) {
+				/* hold the device throughout */
+				get_device(cxlds->dev);
+
+				/* give first timeout a second */
+				timeout = 1;
+				cxlds->security.poll_tmo_secs = timeout;
+				queue_delayed_work(system_wq,
+						   &cxlds->security.poll_dwork,
+						   timeout * HZ);
+			}
+
+			dev_dbg(dev, "Sanitation operation started\n");
+			goto success;
+		}
 
  		dev_dbg(dev, "Mailbox background operation (0x%04x) started\n",
  			mbox_cmd->opcode);
 
+		timeout = mbox_cmd->poll_interval_ms;
  		for (i = 0; i < mbox_cmd->poll_count; i++) {
  			if (rcuwait_wait_event_timeout(&cxlds->mbox_wait,
  				       cxl_mbox_background_complete(cxlds),
@@ -268,6 +337,7 @@ static int __cxl_pci_mbox_send_cmd(struct cxl_dev_state *cxlds,
  		return 0; /* completed but caller must check return_code */
  	}
 
+success:
  	/* #7 */
  	cmd_reg = readq(cxlds->regs.mbox + CXLDEV_MBOX_CMD_OFFSET);
  	out_len = FIELD_GET(CXLDEV_MBOX_CMD_PAYLOAD_LENGTH_MASK, cmd_reg);

drivers/cxl/pci.c static int cxl_pci_setup_mailbox(struct cxl_dev_state *cxlds) · line 450

Quoted 5 lines
drivers/cxl/pci.c +2 -0
@@ -376,10 +446,15 @@ static int cxl_pci_setup_mailbox(struct cxl_dev_state *cxlds)
  		ctrl |= CXLDEV_MBOX_CTRL_BG_CMD_IRQ;
  		writel(ctrl, cxlds->regs.mbox + CXLDEV_MBOX_CTRL_OFFSET);
 
+		/* flag that irqs are enabled */
+		cxlds->security.poll_tmo_secs = -1;

Use a #define instead of -1 magic number? CXL_CMD_TIMEOUT_INVALID perhaps? Would also apply to all the checking of poll_tmo_secs in this patch.

Quoted 10 lines
drivers/cxl/pci.c +3 -0
  		return 0;
  	}
 
  mbox_poll:
+	cxlds->security.poll_tmo_secs = 0;
+	INIT_DELAYED_WORK(&cxlds->security.poll_dwork,
+			  cxl_mbox_sanitize_work);
  	dev_dbg(cxlds->dev, "Mailbox interrupts are unsupported");
  	return 0;
  }
#7 lore raw

Re: [PATCH 2/6] cxl/mbox: Add sanitation handling machinery

Quoted 215 lines · On Tue, 30 May 2023 16:36:21 -0700 Dave Jiang <[email protected]> wrote:
Quoted 16 lines · Davidlohr Bueso

Sanitation is by definition a device-monopolizing operation, and thus the timeslicing rules for other background commands do not apply. As such handle this special case asynchronously and return immediately. Subsequent changes will allow completion to be pollable from userspace via a sysfs file interface.

For devices that don't support interrupts for notifying background command completion, self-poll with the caveat that the poller can be out of sync with the ready hardware, and therefore care must be taken to not allow any new commands to go through until the poller sees the hw completion. The poller takes the mbox_mutex to stabilize the flagging, minimizing any runtime overhead in the send path to check for 'sanitize_tmo' for uncommon poll scenarios. This flag also serves for sanitation (the only user of async polling) to know when to queue work or simply rely on irqs.

Quoted 4 lines

The irq case is much simpler as hardware will serialize/error appropriately.

Just a minor nit below, otherwise

Quoted 188 lines
---
  drivers/cxl/core/memdev.c | 10 +++++
  drivers/cxl/cxlmem.h      | 10 +++++
  drivers/cxl/pci.c         | 83 +++++++++++++++++++++++++++++++++++++--
  3 files changed, 99 insertions(+), 4 deletions(-)
drivers/cxl/core/memdev.c +10 -0
index 6e1d7d3610a2..02763e83545c 100644
@@ -460,11 +460,21 @@ void clear_exclusive_cxl_commands(struct cxl_dev_state *cxlds, unsigned long *cm
  }
  EXPORT_SYMBOL_NS_GPL(clear_exclusive_cxl_commands, CXL);
 
+static void cxl_memdev_security_shutdown(struct device *dev)
+{
+	struct cxl_memdev *cxlmd = to_cxl_memdev(dev);
+	struct cxl_dev_state *cxlds = cxlmd->cxlds;
+
+	if (cxlds->security.poll_tmo_secs != -1)
+		cancel_delayed_work_sync(&cxlds->security.poll_dwork);
+}
+
  static void cxl_memdev_shutdown(struct device *dev)
  {
  	struct cxl_memdev *cxlmd = to_cxl_memdev(dev);
 
  	down_write(&cxl_memdev_rwsem);
+	cxl_memdev_security_shutdown(dev);
  	cxlmd->cxlds = NULL;
  	up_write(&cxl_memdev_rwsem);
  }
drivers/cxl/cxlmem.h +10 -0
index 5329274b0076..02ec68f97de2 100644
@@ -264,9 +264,18 @@ struct cxl_poison_state {
   * struct cxl_security_state - Device security state
   *
   * @state: state of last security operation
+ * @poll_tmo_secs: polling timeout
+ * @poll_dwork: polling work item
+ *
+ * Polling (sanitation) is only used when device mbox irqs are not
+ * supported. As such, @poll_tmo_secs == -1 indicates that polling
+ * is disabled. Otherwise, when enabled, @poll_tmo_secs is maxed
+ * at 15 minutes and serialized by the mbox_mutex.
   */
  struct cxl_security_state {
  	unsigned long state;
+	int poll_tmo_secs;
+	struct delayed_work poll_dwork;
  };
 
  /**
@@ -380,6 +389,7 @@ enum cxl_opcode {
  	CXL_MBOX_OP_GET_SCAN_MEDIA_CAPS	= 0x4303,
  	CXL_MBOX_OP_SCAN_MEDIA		= 0x4304,
  	CXL_MBOX_OP_GET_SCAN_MEDIA	= 0x4305,
+	CXL_MBOX_OP_SANITIZE		= 0x4400,
  	CXL_MBOX_OP_GET_SECURITY_STATE	= 0x4500,
  	CXL_MBOX_OP_SET_PASSPHRASE	= 0x4501,
  	CXL_MBOX_OP_DISABLE_PASSPHRASE	= 0x4502,
drivers/cxl/pci.c +74 -4
index a78e40e6d0e0..a0d93719ab18 100644
@@ -115,16 +115,52 @@ static bool cxl_mbox_background_complete(struct cxl_dev_state *cxlds)
 
  static irqreturn_t cxl_pci_mbox_irq(int irq, void *id)
  {
+	u64 reg;
+	u16 opcode;
  	struct cxl_dev_id *dev_id = id;
  	struct cxl_dev_state *cxlds = dev_id->cxlds;
 
-	/* short-circuit the wait in __cxl_pci_mbox_send_cmd() */
-	if (cxl_mbox_background_complete(cxlds))
-		rcuwait_wake_up(&cxlds->mbox_wait);
+	if (!cxl_mbox_background_complete(cxlds))
+		goto done;
 
+	reg = readq(cxlds->regs.mbox + CXLDEV_MBOX_BG_CMD_STATUS_OFFSET);
+	opcode = FIELD_GET(CXLDEV_MBOX_BG_CMD_COMMAND_OPCODE_MASK, reg);
+	if (opcode == CXL_MBOX_OP_SANITIZE) {
+		dev_dbg(cxlds->dev, "Sanitation operation ended\n");
+	} else {
+		/* short-circuit the wait in __cxl_pci_mbox_send_cmd() */
+		rcuwait_wake_up(&cxlds->mbox_wait);
+	}
+done:
  	return IRQ_HANDLED;
  }
 
+/*
+ * Sanitation operation polling mode.
+ */
+static void cxl_mbox_sanitize_work(struct work_struct *work)
+{
+	struct cxl_dev_state *cxlds;
+
+	cxlds = container_of(work,
+			     struct cxl_dev_state, security.poll_dwork.work);
+
+	mutex_lock(&cxlds->mbox_mutex);
+	if (cxl_mbox_background_complete(cxlds)) {
+		cxlds->security.poll_tmo_secs = 0;
+		put_device(cxlds->dev);
+
+		dev_dbg(cxlds->dev, "Sanitation operation ended\n");
+	} else {
+		int timeout = cxlds->security.poll_tmo_secs + 10;
+
+		cxlds->security.poll_tmo_secs = min(15 * 60, timeout);
+		queue_delayed_work(system_wq, &cxlds->security.poll_dwork,
+				   timeout * HZ);
+	}
+	mutex_unlock(&cxlds->mbox_mutex);
+}
+
  /**
   * __cxl_pci_mbox_send_cmd() - Execute a mailbox command
   * @cxlds: The device state to communicate with.
@@ -185,6 +221,16 @@ static int __cxl_pci_mbox_send_cmd(struct cxl_dev_state *cxlds,
  		return -EBUSY;
  	}
 
+	/*
+	 * With sanitize polling, hardware might be done and the poller still
+	 * not be in sync. Ensure no new command comes in until so. Keep the
+	 * hardware semantics and only allow device health status.
+	 */
+	if (unlikely(cxlds->security.poll_tmo_secs > 0)) {
+		if (mbox_cmd->opcode != CXL_MBOX_OP_GET_HEALTH_INFO)
+			return -EBUSY;
+	}
+
  	cmd_reg = FIELD_PREP(CXLDEV_MBOX_CMD_COMMAND_OPCODE_MASK,
  			     mbox_cmd->opcode);
  	if (mbox_cmd->size_in) {
@@ -233,11 +279,34 @@ static int __cxl_pci_mbox_send_cmd(struct cxl_dev_state *cxlds,
  	 */
  	if (mbox_cmd->return_code == CXL_MBOX_CMD_RC_BACKGROUND) {
  		u64 bg_status_reg;
-		int i, timeout = mbox_cmd->poll_interval_ms;
+		int i, timeout;
+
+		/*
+		 * Sanitation is a special case which monopolizes the device
+		 * and cannot be timesliced. Handle asynchronously instead,
+		 * and allow userspace to poll(2) for completion.
+		 */
+		if (mbox_cmd->opcode == CXL_MBOX_OP_SANITIZE) {
+			if (cxlds->security.poll_tmo_secs != -1) {
+				/* hold the device throughout */
+				get_device(cxlds->dev);
+
+				/* give first timeout a second */
+				timeout = 1;
+				cxlds->security.poll_tmo_secs = timeout;
+				queue_delayed_work(system_wq,
+						   &cxlds->security.poll_dwork,
+						   timeout * HZ);
+			}
+
+			dev_dbg(dev, "Sanitation operation started\n");
+			goto success;
+		}
 
  		dev_dbg(dev, "Mailbox background operation (0x%04x) started\n",
  			mbox_cmd->opcode);
 
+		timeout = mbox_cmd->poll_interval_ms;
  		for (i = 0; i < mbox_cmd->poll_count; i++) {
  			if (rcuwait_wait_event_timeout(&cxlds->mbox_wait,
  				       cxl_mbox_background_complete(cxlds),
@@ -268,6 +337,7 @@ static int __cxl_pci_mbox_send_cmd(struct cxl_dev_state *cxlds,
  		return 0; /* completed but caller must check return_code */
  	}
 
+success:
  	/* #7 */
  	cmd_reg = readq(cxlds->regs.mbox + CXLDEV_MBOX_CMD_OFFSET);
  	out_len = FIELD_GET(CXLDEV_MBOX_CMD_PAYLOAD_LENGTH_MASK, cmd_reg);
@@ -376,10 +446,15 @@ static int cxl_pci_setup_mailbox(struct cxl_dev_state *cxlds)
  		ctrl |= CXLDEV_MBOX_CTRL_BG_CMD_IRQ;
  		writel(ctrl, cxlds->regs.mbox + CXLDEV_MBOX_CTRL_OFFSET);
 
Quoted 6 lines

drivers/cxl/pci.c static int cxl_pci_setup_mailbox(struct cxl_dev_state *cxlds) · line 450

Quoted 2 lines
drivers/cxl/pci.c +2 -0
+		/* flag that irqs are enabled */
+		cxlds->security.poll_tmo_secs = -1;  

Use a #define instead of -1 magic number? CXL_CMD_TIMEOUT_INVALID perhaps? Would also apply to all the checking of poll_tmo_secs in this patch.

If we can avoid this use of magic numbers entirely it would be more readable. Either a nicely named boolean, or querying it directly from the hardware / other cached state (which looks fiddly).

Quoted 13 lines
Quoted 12 lines
drivers/cxl/pci.c +3 -0
  		return 0;
  	}
 
  mbox_poll:
+	cxlds->security.poll_tmo_secs = 0;
+	INIT_DELAYED_WORK(&cxlds->security.poll_dwork,
+			  cxl_mbox_sanitize_work);
  	dev_dbg(cxlds->dev, "Mailbox interrupts are unsupported");
  	return 0;
  }  
#8 lore raw

Re: [PATCH 2/6] cxl/mbox: Add sanitation handling machinery

In reply to Davidlohr Bueso
Quoted 20 lines · On Thu, 25 May 2023 20:33:40 -0700 Davidlohr Bueso <[email protected]> wrote:

Sanitation is by definition a device-monopolizing operation, and thus the timeslicing rules for other background commands do not apply. As such handle this special case asynchronously and return immediately. Subsequent changes will allow completion to be pollable from userspace via a sysfs file interface.

For devices that don't support interrupts for notifying background command completion, self-poll with the caveat that the poller can be out of sync with the ready hardware, and therefore care must be taken to not allow any new commands to go through until the poller sees the hw completion. The poller takes the mbox_mutex to stabilize the flagging, minimizing any runtime overhead in the send path to check for 'sanitize_tmo' for uncommon poll scenarios. This flag also serves for sanitation (the only user of async polling) to know when to queue work or simply rely on irqs.

The irq case is much simpler as hardware will serialize/error appropriately.

...

Quoted 5 lines
drivers/cxl/cxlmem.h +0 -0
index 5329274b0076..02ec68f97de2 100644

drivers/cxl/cxlmem.h struct cxl_poison_state { · line 273

Quoted 10 lines
drivers/cxl/cxlmem.h +7 -0
@@ -264,9 +264,18 @@ struct cxl_poison_state {
  * struct cxl_security_state - Device security state
  *
  * @state: state of last security operation
+ * @poll_tmo_secs: polling timeout
+ * @poll_dwork: polling work item
+ *
+ * Polling (sanitation) is only used when device mbox irqs are not
+ * supported. As such, @poll_tmo_secs == -1 indicates that polling
+ * is disabled. Otherwise, when enabled, @poll_tmo_secs is maxed
+ * at 15 minutes and serialized by the mbox_mutex.

Long comment to avoid a bool :)

Quoted 14 lines
drivers/cxl/cxlmem.h +2 -0
  */
 struct cxl_security_state {
 	unsigned long state;
+	int poll_tmo_secs;
+	struct delayed_work poll_dwork;
 };
 
drivers/cxl/pci.c +0 -0
index a78e40e6d0e0..a0d93719ab18 100644
@@ -115,16 +115,52 @@ static bool cxl_mbox_background_complete(struct cxl_dev_state *cxlds)
 
 static irqreturn_t cxl_pci_mbox_irq(int irq, void *id)

drivers/cxl/pci.c static bool cxl_mbox_background_complete(struct cxl_dev_state *cxlds) · line 123

Quoted 10 lines
drivers/cxl/pci.c +3 -3
 {
+	u64 reg;
+	u16 opcode;
 	struct cxl_dev_id *dev_id = id;
 	struct cxl_dev_state *cxlds = dev_id->cxlds;
 
-	/* short-circuit the wait in __cxl_pci_mbox_send_cmd() */
-	if (cxl_mbox_background_complete(cxlds))
-		rcuwait_wake_up(&cxlds->mbox_wait);
+	if (!cxl_mbox_background_complete(cxlds))

If we hit this path, does it mean it wasn't our interrupt? Or an we get here via a race as well - but if so there should be a comment on why this isn't returning IRQ_NONE. So either a comment on the race or IRQ_NONE return.

Quoted 14 lines
drivers/cxl/pci.c +10 -0
+		goto done;
 
+	reg = readq(cxlds->regs.mbox + CXLDEV_MBOX_BG_CMD_STATUS_OFFSET);
+	opcode = FIELD_GET(CXLDEV_MBOX_BG_CMD_COMMAND_OPCODE_MASK, reg);
+	if (opcode == CXL_MBOX_OP_SANITIZE) {
+		dev_dbg(cxlds->dev, "Sanitation operation ended\n");
+	} else {
+		/* short-circuit the wait in __cxl_pci_mbox_send_cmd() */
+		rcuwait_wake_up(&cxlds->mbox_wait);
+	}
+done:
 	return IRQ_HANDLED;
 }
 
author maintainer
#9 lore raw

PATCH v5 3/6 cxl/mem: Wire up Sanitation support

In reply to Davidlohr Bueso

Implement support for CXL 3.0 8.2.9.8.5.1 Sanitize. This is done by adding a security/sanitize' memdev sysfs file, which is poll(2)-capable for completion. Unlike all other background commands, this is the only operation that is special and monopolizes the device for long periods of time.

In addition to the traditional pmem security requirements, all regions must also be offline in order to perform the operation. This permits avoiding explicit global CPU cache management, relying instead on attach_target() setting CXL_REGION_F_INCOHERENT upon reconnect.

The expectation is that userspace can use it such as:

cxl disable-memdev memX
echo 1 > /sys/bus/cxl/devices/memX/security/sanitize cxl wait-sanitize memX
cxl enable-memdev memX

---
 Documentation/ABI/testing/sysfs-bus-cxl | 21 ++++++++-
 drivers/cxl/core/mbox.c                 | 55 ++++++++++++++++++++++++
 drivers/cxl/core/memdev.c               | 57 ++++++++++++++++++++++++-
 drivers/cxl/cxlmem.h                    |  4 ++
 drivers/cxl/pci.c                       |  5 +++
 5 files changed, 139 insertions(+), 3 deletions(-)
Documentation/ABI/testing/sysfs-bus-cxl +19 -2
index 721a44d8a482..5753cba98692 100644
@@ -64,8 +64,25 @@ KernelVersion:	v6.5
 Contact:	[email protected]
 Description:
 		(RO) Reading this file will display the CXL security state for
-		that device. Such states can be: 'disabled', or those available
-		only for persistent memory: 'locked', 'unlocked' or 'frozen'.
+		that device. Such states can be: 'disabled', 'sanitize', when
+		a sanitation is currently underway; or those available only
+		for persistent memory: 'locked', 'unlocked' or 'frozen'. This
+		sysfs entry is select/poll capable from userspace to notify
+		upon completion of a sanitize operation.
+
+
+What:           /sys/bus/cxl/devices/memX/security/sanitize
+Date:           June, 2023
+KernelVersion:  v6.5
+Contact:        [email protected]
+Description:
+		(WO) Write a boolean 'true' string value to this attribute to
+		sanitize the device to securely re-purpose or decommission it.
+		This is done by ensuring that all user data and meta-data,
+		whether it resides in persistent capacity, volatile capacity,
+		or the LSA, is made permanently unavailable by whatever means
+		is appropriate for the media type. This functionality requires
+		the device to be not be actively decoding any HPA ranges.
 
 
 What:		/sys/bus/cxl/devices/*/devtype
drivers/cxl/core/mbox.c +55 -0

61 lines not on this page · raw message

drivers/cxl/core/memdev.c +56 -1

93 lines not on this page · raw message

drivers/cxl/cxlmem.h +4 -0
index 02ec68f97de2..408ec33c8480 100644
@@ -266,6 +266,7 @@ struct cxl_poison_state {
  * @state: state of last security operation
  * @poll_tmo_secs: polling timeout
  * @poll_dwork: polling work item
+ * @sanitize_node: sanitation sysfs file to notify
  *
  * Polling (sanitation) is only used when device mbox irqs are not
  * supported. As such, @poll_tmo_secs == -1 indicates that polling
@@ -276,6 +277,7 @@ struct cxl_security_state {
 	unsigned long state;
 	int poll_tmo_secs;
 	struct delayed_work poll_dwork;
+	struct kernfs_node *sanitize_node;
 };
 
 /**
@@ -750,6 +752,8 @@ static inline void cxl_mem_active_dec(void)
 }
 #endif
 
+int cxl_mem_sanitize(struct cxl_dev_state *cxlds, u16 cmd);
+
 struct cxl_hdm {
 	struct cxl_component_regs regs;
 	unsigned int decoder_count;
drivers/cxl/pci.c +5 -0
index a0d93719ab18..195c4267d3db 100644
@@ -126,6 +126,9 @@ static irqreturn_t cxl_pci_mbox_irq(int irq, void *id)
 	reg = readq(cxlds->regs.mbox + CXLDEV_MBOX_BG_CMD_STATUS_OFFSET);
 	opcode = FIELD_GET(CXLDEV_MBOX_BG_CMD_COMMAND_OPCODE_MASK, reg);
 	if (opcode == CXL_MBOX_OP_SANITIZE) {
+		if (cxlds->security.sanitize_node)
+			sysfs_notify_dirent(cxlds->security.sanitize_node);
+
 		dev_dbg(cxlds->dev, "Sanitation operation ended\n");
 	} else {
 		/* short-circuit the wait in __cxl_pci_mbox_send_cmd() */
@@ -149,6 +152,8 @@ static void cxl_mbox_sanitize_work(struct work_struct *work)
 	if (cxl_mbox_background_complete(cxlds)) {
 		cxlds->security.poll_tmo_secs = 0;
 		put_device(cxlds->dev);
+		if (cxlds->security.sanitize_node)
+			sysfs_notify_dirent(cxlds->security.sanitize_node);
 
 		dev_dbg(cxlds->dev, "Sanitation operation ended\n");
 	} else {
Signature · 1 line
2.40.1
author maintainer
#10 lore raw

Re: [PATCH 3/6] cxl/mem: Wire up Sanitation support

Quoted 5 lines · Davidlohr Bueso

Implement support for CXL 3.0 8.2.9.8.5.1 Sanitize. This is done by adding a security/sanitize' memdev sysfs file, which is poll(2)-capable for completion. Unlike all other background commands, this is the only operation that is special and monopolizes the device for long periods of time.

As becomes obvious in the code, this paragraph actually should have been updated: the pollable/read file for the status of sanitation is the security/state.

Thanks,
Davidlohr

maintainer Reviewed-by
#11 lore raw

Re: [PATCH 3/6] cxl/mem: Wire up Sanitation support

Quoted 11 lines · Davidlohr Bueso
Quoted 5 lines · Davidlohr Bueso

Implement support for CXL 3.0 8.2.9.8.5.1 Sanitize. This is done by adding a security/sanitize' memdev sysfs file, which is poll(2)-capable for completion. Unlike all other background commands, this is the only operation that is special and monopolizes the device for long periods of time.

As becomes obvious in the code, this paragraph actually should have been updated: the pollable/read file for the status of sanitation is the security/state.

Quoted 3 lines

Thanks,
Davidlohr

Reviewed-by
#12 lore raw

Re: [PATCH 3/6] cxl/mem: Wire up Sanitation support

Quoted 9 lines · On Tue, 30 May 2023 16:53:25 -0700 Dave Jiang <[email protected]> wrote:
Quoted 8 lines · Davidlohr Bueso
Quoted 5 lines · Davidlohr Bueso

Implement support for CXL 3.0 8.2.9.8.5.1 Sanitize. This is done by adding a security/sanitize' memdev sysfs file, which is poll(2)-capable for completion. Unlike all other background commands, this is the only operation that is special and monopolizes the device for long periods of time.

Quoted 6 lines
Quoted 3 lines

As becomes obvious in the code, this paragraph actually should have been updated: the pollable/read file for the status of sanitation is the security/state.

LGTM as well

Quoted 8 lines
Quoted 7 lines

Thanks,
Davidlohr

author maintainer
#13 lore raw

PATCH v5 4/6 cxl/test: Add Sanitize opcode support

In reply to Davidlohr Bueso

Add support to emulate the "Sanitize" operation, without incurring in the background.

---
 tools/testing/cxl/test/mem.c | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
tools/testing/cxl/test/mem.c +25 -0
index 34b48027b3de..faa484ea5b0b 100644
@@ -535,6 +535,28 @@ static int mock_partition_info(struct cxl_dev_state *cxlds,
 	return 0;
 }
 
+static int mock_sanitize(struct cxl_dev_state *cxlds, struct cxl_mbox_cmd *cmd)
+{
+	struct cxl_mockmem_data *mdata = dev_get_drvdata(cxlds->dev);
+
+	if (cmd->size_in != 0)
+		return -EINVAL;
+
+	if (cmd->size_out != 0)
+		return -EINVAL;
+
+	if (mdata->security_state & CXL_PMEM_SEC_STATE_USER_PASS_SET) {
+		cmd->return_code = CXL_MBOX_CMD_RC_SECURITY;
+		return -ENXIO;
+	}
+	if (mdata->security_state & CXL_PMEM_SEC_STATE_LOCKED) {
+		cmd->return_code = CXL_MBOX_CMD_RC_SECURITY;
+		return -ENXIO;
+	}
+
+	return 0; /* assume less than 2 secs, no bg */
+}
+
 static int mock_get_security_state(struct cxl_dev_state *cxlds,
 				   struct cxl_mbox_cmd *cmd)
 {
@@ -1153,6 +1175,9 @@ static int cxl_mock_mbox_send(struct cxl_dev_state *cxlds, struct cxl_mbox_cmd *
 	case CXL_MBOX_OP_GET_HEALTH_INFO:
 		rc = mock_health_info(cxlds, cmd);
 		break;
+	case CXL_MBOX_OP_SANITIZE:
+		rc = mock_sanitize(cxlds, cmd);
+		break;
 	case CXL_MBOX_OP_GET_SECURITY_STATE:
 		rc = mock_get_security_state(cxlds, cmd);
 		break;
Signature · 1 line
2.40.1
author maintainer
#14 lore raw

PATCH v5 5/6 cxl/mem: Support Secure Erase

In reply to Davidlohr Bueso

Implement support for the non-pmem exclusive secure erase, per CXL specs. Create a write-only 'security/erase' sysfs file to perform the requested operation.

As with the sanitation this requires the device being offline and thus no active HPA-DPA decoding.

The expectation is that userspace can use it such as:

cxl disable-memdev memX
echo 1 > /sys/bus/cxl/devices/memX/security/erase cxl enable-memdev memX

---
 Documentation/ABI/testing/sysfs-bus-cxl | 10 +++++++++
 drivers/cxl/core/mbox.c                 |  6 +++++-
 drivers/cxl/core/memdev.c               | 28 +++++++++++++++++++++++++
 drivers/cxl/cxlmem.h                    |  1 +
 4 files changed, 44 insertions(+), 1 deletion(-)
Documentation/ABI/testing/sysfs-bus-cxl +10 -0
index 5753cba98692..f224c1215f22 100644
@@ -85,6 +85,16 @@ Description:
 		the device to be not be actively decoding any HPA ranges.
 
 
+What            /sys/bus/cxl/devices/memX/security/erase
+Date:           June, 2023
+KernelVersion:  v6.5
+Contact:        [email protected]
+Description:
+		(WO) Write a boolean 'true' string value to this attribute to
+		secure erase user data by changing the media encryption keys for
+		all user data areas of the device.
+
+
 What:		/sys/bus/cxl/devices/*/devtype
 Date:		June, 2021
 KernelVersion:	v5.14
drivers/cxl/core/mbox.c +5 -1
index 51c64829f20a..6622eac66bf1 100644
@@ -1102,7 +1102,7 @@ int cxl_mem_sanitize(struct cxl_dev_state *cxlds, u16 cmd)
 	};
 	struct cxl_mbox_cmd mbox_cmd = { .opcode = cmd };
 
-	if (cmd != CXL_MBOX_OP_SANITIZE)
+	if (cmd != CXL_MBOX_OP_SANITIZE && cmd != CXL_MBOX_OP_SECURE_ERASE)
 		return -EINVAL;
 
 	rc = cxl_internal_send_cmd(cxlds, &sec_cmd);
@@ -1120,6 +1120,10 @@ int cxl_mem_sanitize(struct cxl_dev_state *cxlds, u16 cmd)
 	if (sec_out & CXL_PMEM_SEC_STATE_USER_PASS_SET)
 		return -EINVAL;
 
+	if (cmd == CXL_MBOX_OP_SECURE_ERASE &&
+	    sec_out & CXL_PMEM_SEC_STATE_LOCKED)
+		return -EINVAL;
+
 	rc = cxl_internal_send_cmd(cxlds, &mbox_cmd);
 	if (rc < 0) {
 		dev_err(cxlds->dev, "Failed to sanitize device : %d", rc);
drivers/cxl/core/memdev.c +28 -0
index 90f23e53d483..d06c8539e82c 100644
@@ -163,6 +163,33 @@ static ssize_t security_sanitize_store(struct device *dev,
 static struct device_attribute dev_attr_security_sanitize =
 	__ATTR(sanitize, 0200, NULL, security_sanitize_store);
 
+static ssize_t security_erase_store(struct device *dev,
+				    struct device_attribute *attr,
+				    const char *buf, size_t len)
+{
+	struct cxl_memdev *cxlmd = to_cxl_memdev(dev);
+	struct cxl_dev_state *cxlds = cxlmd->cxlds;
+	struct cxl_port *port = dev_get_drvdata(&cxlmd->dev);
+	ssize_t rc;
+	bool erase;
+
+	if (kstrtobool(buf, &erase) || !erase)
+		return -EINVAL;
+
+	if (!port || !is_cxl_endpoint(port))
+		return -EINVAL;
+
+	/* ensure no regions are mapped to this memdev */
+	if (port->commit_end != -1)
+		return -EBUSY;
+
+	rc = cxl_mem_sanitize(cxlds, CXL_MBOX_OP_SECURE_ERASE);
+
+	return rc ? rc : len;
+}
+static struct device_attribute dev_attr_security_erase =
+	__ATTR(erase, 0200, NULL, security_erase_store);
+
 static int cxl_get_poison_by_memdev(struct cxl_memdev *cxlmd)
 {
 	struct cxl_dev_state *cxlds = cxlmd->cxlds;
@@ -411,6 +438,7 @@ static struct attribute *cxl_memdev_ram_attributes[] = {
 static struct attribute *cxl_memdev_security_attributes[] = {
 	&dev_attr_security_state.attr,
 	&dev_attr_security_sanitize.attr,
+	&dev_attr_security_erase.attr,
 	NULL,
 };
 
drivers/cxl/cxlmem.h +1 -0
index 408ec33c8480..758fea7b9dbf 100644
@@ -392,6 +392,7 @@ enum cxl_opcode {
 	CXL_MBOX_OP_SCAN_MEDIA		= 0x4304,
 	CXL_MBOX_OP_GET_SCAN_MEDIA	= 0x4305,
 	CXL_MBOX_OP_SANITIZE		= 0x4400,
+	CXL_MBOX_OP_SECURE_ERASE	= 0x4401,
 	CXL_MBOX_OP_GET_SECURITY_STATE	= 0x4500,
 	CXL_MBOX_OP_SET_PASSPHRASE	= 0x4501,
 	CXL_MBOX_OP_DISABLE_PASSPHRASE	= 0x4502,
Signature · 1 line
2.40.1
maintainer Reviewed-by on v5 5/6
#15 lore raw

Re: [PATCH 5/6] cxl/mem: Support Secure Erase

Quoted 7 lines · Davidlohr Bueso

Implement support for the non-pmem exclusive secure erase, per CXL specs. Create a write-only 'security/erase' sysfs file to perform the requested operation.

As with the sanitation this requires the device being offline and thus no active HPA-DPA decoding.

Quoted 7 lines

The expectation is that userspace can use it such as:

cxl disable-memdev memX
echo 1 > /sys/bus/cxl/devices/memX/security/erase cxl enable-memdev memX

Quoted 110 lines
---
  Documentation/ABI/testing/sysfs-bus-cxl | 10 +++++++++
  drivers/cxl/core/mbox.c                 |  6 +++++-
  drivers/cxl/core/memdev.c               | 28 +++++++++++++++++++++++++
  drivers/cxl/cxlmem.h                    |  1 +
  4 files changed, 44 insertions(+), 1 deletion(-)
Documentation/ABI/testing/sysfs-bus-cxl +10 -0
index 5753cba98692..f224c1215f22 100644
@@ -85,6 +85,16 @@ Description:
  		the device to be not be actively decoding any HPA ranges.
 
 
+What            /sys/bus/cxl/devices/memX/security/erase
+Date:           June, 2023
+KernelVersion:  v6.5
+Contact:        [email protected]
+Description:
+		(WO) Write a boolean 'true' string value to this attribute to
+		secure erase user data by changing the media encryption keys for
+		all user data areas of the device.
+
+
  What:		/sys/bus/cxl/devices/*/devtype
  Date:		June, 2021
  KernelVersion:	v5.14
drivers/cxl/core/mbox.c +5 -1
index 51c64829f20a..6622eac66bf1 100644
@@ -1102,7 +1102,7 @@ int cxl_mem_sanitize(struct cxl_dev_state *cxlds, u16 cmd)
  	};
  	struct cxl_mbox_cmd mbox_cmd = { .opcode = cmd };
 
-	if (cmd != CXL_MBOX_OP_SANITIZE)
+	if (cmd != CXL_MBOX_OP_SANITIZE && cmd != CXL_MBOX_OP_SECURE_ERASE)
  		return -EINVAL;
 
  	rc = cxl_internal_send_cmd(cxlds, &sec_cmd);
@@ -1120,6 +1120,10 @@ int cxl_mem_sanitize(struct cxl_dev_state *cxlds, u16 cmd)
  	if (sec_out & CXL_PMEM_SEC_STATE_USER_PASS_SET)
  		return -EINVAL;
 
+	if (cmd == CXL_MBOX_OP_SECURE_ERASE &&
+	    sec_out & CXL_PMEM_SEC_STATE_LOCKED)
+		return -EINVAL;
+
  	rc = cxl_internal_send_cmd(cxlds, &mbox_cmd);
  	if (rc < 0) {
  		dev_err(cxlds->dev, "Failed to sanitize device : %d", rc);
drivers/cxl/core/memdev.c +28 -0
index 90f23e53d483..d06c8539e82c 100644
@@ -163,6 +163,33 @@ static ssize_t security_sanitize_store(struct device *dev,
  static struct device_attribute dev_attr_security_sanitize =
  	__ATTR(sanitize, 0200, NULL, security_sanitize_store);
 
+static ssize_t security_erase_store(struct device *dev,
+				    struct device_attribute *attr,
+				    const char *buf, size_t len)
+{
+	struct cxl_memdev *cxlmd = to_cxl_memdev(dev);
+	struct cxl_dev_state *cxlds = cxlmd->cxlds;
+	struct cxl_port *port = dev_get_drvdata(&cxlmd->dev);
+	ssize_t rc;
+	bool erase;
+
+	if (kstrtobool(buf, &erase) || !erase)
+		return -EINVAL;
+
+	if (!port || !is_cxl_endpoint(port))
+		return -EINVAL;
+
+	/* ensure no regions are mapped to this memdev */
+	if (port->commit_end != -1)
+		return -EBUSY;
+
+	rc = cxl_mem_sanitize(cxlds, CXL_MBOX_OP_SECURE_ERASE);
+
+	return rc ? rc : len;
+}
+static struct device_attribute dev_attr_security_erase =
+	__ATTR(erase, 0200, NULL, security_erase_store);
+
  static int cxl_get_poison_by_memdev(struct cxl_memdev *cxlmd)
  {
  	struct cxl_dev_state *cxlds = cxlmd->cxlds;
@@ -411,6 +438,7 @@ static struct attribute *cxl_memdev_ram_attributes[] = {
  static struct attribute *cxl_memdev_security_attributes[] = {
  	&dev_attr_security_state.attr,
  	&dev_attr_security_sanitize.attr,
+	&dev_attr_security_erase.attr,
  	NULL,
  };
 
drivers/cxl/cxlmem.h +1 -0
index 408ec33c8480..758fea7b9dbf 100644
@@ -392,6 +392,7 @@ enum cxl_opcode {
  	CXL_MBOX_OP_SCAN_MEDIA		= 0x4304,
  	CXL_MBOX_OP_GET_SCAN_MEDIA	= 0x4305,
  	CXL_MBOX_OP_SANITIZE		= 0x4400,
+	CXL_MBOX_OP_SECURE_ERASE	= 0x4401,
  	CXL_MBOX_OP_GET_SECURITY_STATE	= 0x4500,
  	CXL_MBOX_OP_SET_PASSPHRASE	= 0x4501,
  	CXL_MBOX_OP_DISABLE_PASSPHRASE	= 0x4502,
Reviewed-by on v5 5/6
#16 lore raw

Re: [PATCH 5/6] cxl/mem: Support Secure Erase

In reply to Davidlohr Bueso
Quoted 7 lines · On Thu, 25 May 2023 20:33:43 -0700 Davidlohr Bueso <[email protected]> wrote:

Implement support for the non-pmem exclusive secure erase, per CXL specs. Create a write-only 'security/erase' sysfs file to perform the requested operation.

As with the sanitation this requires the device being offline and thus no active HPA-DPA decoding.

Quoted 7 lines

The expectation is that userspace can use it such as:

cxl disable-memdev memX
echo 1 > /sys/bus/cxl/devices/memX/security/erase cxl enable-memdev memX

Trivial comment inline.

Quoted 33 lines
---
 Documentation/ABI/testing/sysfs-bus-cxl | 10 +++++++++
 drivers/cxl/core/mbox.c                 |  6 +++++-
 drivers/cxl/core/memdev.c               | 28 +++++++++++++++++++++++++
 drivers/cxl/cxlmem.h                    |  1 +
 4 files changed, 44 insertions(+), 1 deletion(-)
Documentation/ABI/testing/sysfs-bus-cxl +10 -0
index 5753cba98692..f224c1215f22 100644
@@ -85,6 +85,16 @@ Description:
 		the device to be not be actively decoding any HPA ranges.
 
 
+What            /sys/bus/cxl/devices/memX/security/erase
+Date:           June, 2023
+KernelVersion:  v6.5
+Contact:        [email protected]
+Description:
+		(WO) Write a boolean 'true' string value to this attribute to
+		secure erase user data by changing the media encryption keys for
+		all user data areas of the device.
+
+
 What:		/sys/bus/cxl/devices/*/devtype
 Date:		June, 2021
 KernelVersion:	v5.14
drivers/cxl/core/mbox.c +0 -0
index 51c64829f20a..6622eac66bf1 100644

drivers/cxl/core/mbox.c int cxl_mem_sanitize(struct cxl_dev_state *cxlds, u16 cmd) · line 1105

Quoted 5 lines
drivers/cxl/core/mbox.c +1 -1
@@ -1102,7 +1102,7 @@ int cxl_mem_sanitize(struct cxl_dev_state *cxlds, u16 cmd)
 	};
 	struct cxl_mbox_cmd mbox_cmd = { .opcode = cmd };
 
-	if (cmd != CXL_MBOX_OP_SANITIZE)
+	if (cmd != CXL_MBOX_OP_SANITIZE && cmd != CXL_MBOX_OP_SECURE_ERASE)

Maybe just use a switch + default for the inevitable growth of this list and attempting to avoid churn?

Quoted 72 lines
drivers/cxl/core/mbox.c +4 -0
 		return -EINVAL;
 
 	rc = cxl_internal_send_cmd(cxlds, &sec_cmd);
@@ -1120,6 +1120,10 @@ int cxl_mem_sanitize(struct cxl_dev_state *cxlds, u16 cmd)
 	if (sec_out & CXL_PMEM_SEC_STATE_USER_PASS_SET)
 		return -EINVAL;
 
+	if (cmd == CXL_MBOX_OP_SECURE_ERASE &&
+	    sec_out & CXL_PMEM_SEC_STATE_LOCKED)
+		return -EINVAL;
+
 	rc = cxl_internal_send_cmd(cxlds, &mbox_cmd);
 	if (rc < 0) {
 		dev_err(cxlds->dev, "Failed to sanitize device : %d", rc);
drivers/cxl/core/memdev.c +28 -0
index 90f23e53d483..d06c8539e82c 100644
@@ -163,6 +163,33 @@ static ssize_t security_sanitize_store(struct device *dev,
 static struct device_attribute dev_attr_security_sanitize =
 	__ATTR(sanitize, 0200, NULL, security_sanitize_store);
 
+static ssize_t security_erase_store(struct device *dev,
+				    struct device_attribute *attr,
+				    const char *buf, size_t len)
+{
+	struct cxl_memdev *cxlmd = to_cxl_memdev(dev);
+	struct cxl_dev_state *cxlds = cxlmd->cxlds;
+	struct cxl_port *port = dev_get_drvdata(&cxlmd->dev);
+	ssize_t rc;
+	bool erase;
+
+	if (kstrtobool(buf, &erase) || !erase)
+		return -EINVAL;
+
+	if (!port || !is_cxl_endpoint(port))
+		return -EINVAL;
+
+	/* ensure no regions are mapped to this memdev */
+	if (port->commit_end != -1)
+		return -EBUSY;
+
+	rc = cxl_mem_sanitize(cxlds, CXL_MBOX_OP_SECURE_ERASE);
+
+	return rc ? rc : len;
+}
+static struct device_attribute dev_attr_security_erase =
+	__ATTR(erase, 0200, NULL, security_erase_store);
+
 static int cxl_get_poison_by_memdev(struct cxl_memdev *cxlmd)
 {
 	struct cxl_dev_state *cxlds = cxlmd->cxlds;
@@ -411,6 +438,7 @@ static struct attribute *cxl_memdev_ram_attributes[] = {
 static struct attribute *cxl_memdev_security_attributes[] = {
 	&dev_attr_security_state.attr,
 	&dev_attr_security_sanitize.attr,
+	&dev_attr_security_erase.attr,
 	NULL,
 };
 
drivers/cxl/cxlmem.h +1 -0
index 408ec33c8480..758fea7b9dbf 100644
@@ -392,6 +392,7 @@ enum cxl_opcode {
 	CXL_MBOX_OP_SCAN_MEDIA		= 0x4304,
 	CXL_MBOX_OP_GET_SCAN_MEDIA	= 0x4305,
 	CXL_MBOX_OP_SANITIZE		= 0x4400,
+	CXL_MBOX_OP_SECURE_ERASE	= 0x4401,
 	CXL_MBOX_OP_GET_SECURITY_STATE	= 0x4500,
 	CXL_MBOX_OP_SET_PASSPHRASE	= 0x4501,
 	CXL_MBOX_OP_DISABLE_PASSPHRASE	= 0x4502,
Reviewed-by on v5 5/6
#17 lore raw

Re: [PATCH 5/6] cxl/mem: Support Secure Erase

In reply to Davidlohr Bueso
Quoted 7 lines · Davidlohr Bueso

Implement support for the non-pmem exclusive secure erase, per CXL specs. Create a write-only 'security/erase' sysfs file to perform the requested operation.

As with the sanitation this requires the device being offline and thus no active HPA-DPA decoding.

Quoted 8 lines

The expectation is that userspace can use it such as:

cxl disable-memdev memX
echo 1 > /sys/bus/cxl/devices/memX/security/erase cxl enable-memdev memX

---

Quoted 112 lines
 Documentation/ABI/testing/sysfs-bus-cxl | 10 +++++++++
 drivers/cxl/core/mbox.c                 |  6 +++++-
 drivers/cxl/core/memdev.c               | 28 +++++++++++++++++++++++++
 drivers/cxl/cxlmem.h                    |  1 +
 4 files changed, 44 insertions(+), 1 deletion(-)
Documentation/ABI/testing/sysfs-bus-cxl +10 -0
index 5753cba98692..f224c1215f22 100644
@@ -85,6 +85,16 @@ Description:
 		the device to be not be actively decoding any HPA ranges.
 
 
+What            /sys/bus/cxl/devices/memX/security/erase
+Date:           June, 2023
+KernelVersion:  v6.5
+Contact:        [email protected]
+Description:
+		(WO) Write a boolean 'true' string value to this attribute to
+		secure erase user data by changing the media encryption keys for
+		all user data areas of the device.
+
+
 What:		/sys/bus/cxl/devices/*/devtype
 Date:		June, 2021
 KernelVersion:	v5.14
drivers/cxl/core/mbox.c +5 -1
index 51c64829f20a..6622eac66bf1 100644
@@ -1102,7 +1102,7 @@ int cxl_mem_sanitize(struct cxl_dev_state *cxlds, u16 cmd)
 	};
 	struct cxl_mbox_cmd mbox_cmd = { .opcode = cmd };
 
-	if (cmd != CXL_MBOX_OP_SANITIZE)
+	if (cmd != CXL_MBOX_OP_SANITIZE && cmd != CXL_MBOX_OP_SECURE_ERASE)
 		return -EINVAL;
 
 	rc = cxl_internal_send_cmd(cxlds, &sec_cmd);
@@ -1120,6 +1120,10 @@ int cxl_mem_sanitize(struct cxl_dev_state *cxlds, u16 cmd)
 	if (sec_out & CXL_PMEM_SEC_STATE_USER_PASS_SET)
 		return -EINVAL;
 
+	if (cmd == CXL_MBOX_OP_SECURE_ERASE &&
+	    sec_out & CXL_PMEM_SEC_STATE_LOCKED)
+		return -EINVAL;
+
 	rc = cxl_internal_send_cmd(cxlds, &mbox_cmd);
 	if (rc < 0) {
 		dev_err(cxlds->dev, "Failed to sanitize device : %d", rc);
drivers/cxl/core/memdev.c +28 -0
index 90f23e53d483..d06c8539e82c 100644
@@ -163,6 +163,33 @@ static ssize_t security_sanitize_store(struct device *dev,
 static struct device_attribute dev_attr_security_sanitize =
 	__ATTR(sanitize, 0200, NULL, security_sanitize_store);
 
+static ssize_t security_erase_store(struct device *dev,
+				    struct device_attribute *attr,
+				    const char *buf, size_t len)
+{
+	struct cxl_memdev *cxlmd = to_cxl_memdev(dev);
+	struct cxl_dev_state *cxlds = cxlmd->cxlds;
+	struct cxl_port *port = dev_get_drvdata(&cxlmd->dev);
+	ssize_t rc;
+	bool erase;
+
+	if (kstrtobool(buf, &erase) || !erase)
+		return -EINVAL;
+
+	if (!port || !is_cxl_endpoint(port))
+		return -EINVAL;
+
+	/* ensure no regions are mapped to this memdev */
+	if (port->commit_end != -1)
+		return -EBUSY;
+
+	rc = cxl_mem_sanitize(cxlds, CXL_MBOX_OP_SECURE_ERASE);
+
+	return rc ? rc : len;
+}
+static struct device_attribute dev_attr_security_erase =
+	__ATTR(erase, 0200, NULL, security_erase_store);
+
 static int cxl_get_poison_by_memdev(struct cxl_memdev *cxlmd)
 {
 	struct cxl_dev_state *cxlds = cxlmd->cxlds;
@@ -411,6 +438,7 @@ static struct attribute *cxl_memdev_ram_attributes[] = {
 static struct attribute *cxl_memdev_security_attributes[] = {
 	&dev_attr_security_state.attr,
 	&dev_attr_security_sanitize.attr,
+	&dev_attr_security_erase.attr,
 	NULL,
 };
 
drivers/cxl/cxlmem.h +1 -0
index 408ec33c8480..758fea7b9dbf 100644
@@ -392,6 +392,7 @@ enum cxl_opcode {
 	CXL_MBOX_OP_SCAN_MEDIA		= 0x4304,
 	CXL_MBOX_OP_GET_SCAN_MEDIA	= 0x4305,
 	CXL_MBOX_OP_SANITIZE		= 0x4400,
+	CXL_MBOX_OP_SECURE_ERASE	= 0x4401,
 	CXL_MBOX_OP_GET_SECURITY_STATE	= 0x4500,
 	CXL_MBOX_OP_SET_PASSPHRASE	= 0x4501,
 	CXL_MBOX_OP_DISABLE_PASSPHRASE	= 0x4502,
Signature · 1 line
2.40.1
Signature · 1 line
Fan Ni <[email protected]>
author maintainer
#18 lore raw

PATCH v5 6/6 cxl/test: Add Secure Erase opcode support

In reply to Davidlohr Bueso

Add support to emulate the CXL the "Secure Erase" operation.

---
 tools/testing/cxl/test/mem.c | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
tools/testing/cxl/test/mem.c +27 -0
index faa484ea5b0b..97de0d3b2fd0 100644
@@ -557,6 +557,30 @@ static int mock_sanitize(struct cxl_dev_state *cxlds, struct cxl_mbox_cmd *cmd)
 	return 0; /* assume less than 2 secs, no bg */
 }
 
+static int mock_secure_erase(struct cxl_dev_state *cxlds,
+			     struct cxl_mbox_cmd *cmd)
+{
+	struct cxl_mockmem_data *mdata = dev_get_drvdata(cxlds->dev);
+
+	if (cmd->size_in != 0)
+		return -EINVAL;
+
+	if (cmd->size_out != 0)
+		return -EINVAL;
+
+	if (mdata->security_state & CXL_PMEM_SEC_STATE_USER_PASS_SET) {
+		cmd->return_code = CXL_MBOX_CMD_RC_SECURITY;
+		return -ENXIO;
+	}
+
+	if (mdata->security_state & CXL_PMEM_SEC_STATE_LOCKED) {
+		cmd->return_code = CXL_MBOX_CMD_RC_SECURITY;
+		return -ENXIO;
+	}
+
+	return 0;
+}
+
 static int mock_get_security_state(struct cxl_dev_state *cxlds,
 				   struct cxl_mbox_cmd *cmd)
 {
@@ -1178,6 +1202,9 @@ static int cxl_mock_mbox_send(struct cxl_dev_state *cxlds, struct cxl_mbox_cmd *
 	case CXL_MBOX_OP_SANITIZE:
 		rc = mock_sanitize(cxlds, cmd);
 		break;
+	case CXL_MBOX_OP_SECURE_ERASE:
+		rc = mock_secure_erase(cxlds, cmd);
+		break;
 	case CXL_MBOX_OP_GET_SECURITY_STATE:
 		rc = mock_get_security_state(cxlds, cmd);
 		break;
Signature · 1 line
2.40.1