Skip to contentSkip to conversation

cxl: Support memdev sanitation

PATCH Superseded Davidlohr Bueso 2 participants 7 messages Apr 23, 2023 – Apr 26, 2023

Subsystems (2)

v1 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

cover cxl: Support memdev sanitation

Hi,

These are the cxl-tool new subcommands for device sanitation for the kernel side equivalent (currently v4):

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

Obviously the naming of the interfaces are derived from what is currently there for ndctl. However note that the new cxl wait-sanitize and sanitize-memdev commands do not rely on pmem security, such as ndctl_dimm_get_security(). However, the kernel does consult pmem security before sending the approving the operation, so no harm no foul.

This code is also available at:
https://github.com/davidlohr/ndctl/tree/cxl-memdev-sanitation-v1

Thanks!

Davidlohr Bueso (2):
cxl/memdev: Introduce wait-sanitize functionality cxl/memdev: Introduce sanitize-memdev functionality

 Documentation/cxl/cxl-sanitize-memdev.txt | 65 ++++++++++++++++
 Documentation/cxl/cxl-wait-sanitize.txt   | 39 ++++++++++
 Documentation/cxl/lib/libcxl.txt          |  2 +
 Documentation/cxl/meson.build             |  2 +
 cxl/builtin.h                             |  2 +
 cxl/cxl.c                                 |  2 +
 cxl/lib/libcxl.c                          | 94 +++++++++++++++++++++++
 cxl/lib/libcxl.sym                        |  2 +
 cxl/libcxl.h                              |  2 +
 cxl/memdev.c                              | 73 ++++++++++++++++++
 10 files changed, 283 insertions(+)
 create mode 100644 Documentation/cxl/cxl-sanitize-memdev.txt
 create mode 100644 Documentation/cxl/cxl-wait-sanitize.txt

--
2.40.0

#1 lore raw

PATCH 1/2 cxl/memdev: Introduce wait-sanitize functionality

Add a new cxl_memdev_wait_sanitize() to libcxl to support waiting for sanitation operation to be done in the background, if any.

This is analogous to 'ndctl wait-overwrite'.

---
 Documentation/cxl/cxl-wait-sanitize.txt | 35 +++++++++++
 Documentation/cxl/lib/libcxl.txt        |  1 +
 Documentation/cxl/meson.build           |  1 +
 cxl/builtin.h                           |  1 +
 cxl/cxl.c                               |  1 +
 cxl/lib/libcxl.c                        | 78 +++++++++++++++++++++++++
 cxl/lib/libcxl.sym                      |  1 +
 cxl/libcxl.h                            |  1 +
 cxl/memdev.c                            | 26 +++++++++
 9 files changed, 145 insertions(+)
 create mode 100644 Documentation/cxl/cxl-wait-sanitize.txt
Documentation/cxl/cxl-wait-sanitize.txt new file +35 -0
new file mode 100644
index 000000000000..c2ceae7ade0e
@@ -0,0 +1,35 @@
+// SPDX-License-Identifier: GPL-2.0
+
+cxl-wait-sanitize(1)
+====================
+
+NAME
+----
+cxl-wait-sanitize - wait for a sanitation operation to complete
+
+SYNOPSIS
+--------
+[verse]
+'cxl wait-sanitize' <mem0> [<mem1>..<memN>] [<options>]
+
+DESCRIPTION
+-----------
+The kernel provides a POLL(2) capable sysfs file ('security/sanitize') to
+indicate the state of device sanitation. When sanitation is in progress,
+this command waits for a change in the state of this file across all
+specified memdevs.
+
+OPTIONS
+-------
+<memdev>::
+include::memdev-option.txt[]
+
+-b::
+--bus=::
+include::bus-option.txt[]
+
+-v::
+--verbose::
+	Emit debug messages.
+
+include::../copyright.txt[]
Documentation/cxl/lib/libcxl.txt +1 -0
index f9af376137fa..9d98865e172c 100644
@@ -135,6 +135,7 @@ int cxl_memdev_write_label(struct cxl_memdev *memdev, void *buf, size_t length,
 struct cxl_cmd *cxl_cmd_new_get_partition(struct cxl_memdev *memdev);
 struct cxl_cmd *cxl_cmd_new_set_partition(struct cxl_memdev *memdev,
 					  unsigned long long volatile_size);
+int cxl_memdev_wait_sanitize(struct cxl_memdev *memdev);
 
 ----
 
Documentation/cxl/meson.build +1 -0
index a6d77ab8cbc2..ebf214ae30df 100644
@@ -45,6 +45,7 @@ cxl_manpages = [
   'cxl-disable-region.txt',
   'cxl-enable-region.txt',
   'cxl-destroy-region.txt',
+  'cxl-wait-sanitize.txt',
   'cxl-monitor.txt',
 ]
 
cxl/builtin.h +1 -0
index 9baa43b8a2ac..04f613703eac 100644
@@ -22,6 +22,7 @@ int cmd_create_region(int argc, const char **argv, struct cxl_ctx *ctx);
 int cmd_enable_region(int argc, const char **argv, struct cxl_ctx *ctx);
 int cmd_disable_region(int argc, const char **argv, struct cxl_ctx *ctx);
 int cmd_destroy_region(int argc, const char **argv, struct cxl_ctx *ctx);
+int cmd_wait_sanitize(int argc, const char **argv, struct cxl_ctx *ctx);
 #ifdef ENABLE_LIBTRACEFS
 int cmd_monitor(int argc, const char **argv, struct cxl_ctx *ctx);
 #else
cxl/cxl.c +1 -0
index 3be7026f43d3..bf55e8bcb2f7 100644
@@ -76,6 +76,7 @@ static struct cmd_struct commands[] = {
 	{ "enable-region", .c_fn = cmd_enable_region },
 	{ "disable-region", .c_fn = cmd_disable_region },
 	{ "destroy-region", .c_fn = cmd_destroy_region },
+	{ "wait-sanitize", .c_fn = cmd_wait_sanitize },
 	{ "monitor", .c_fn = cmd_monitor },
 };
 
cxl/lib/libcxl.c +78 -0

90 lines not on this page · raw message

cxl/lib/libcxl.sym +1 -0
index 6bc0810152e0..bf7d38f7e6fe 100644
@@ -70,6 +70,7 @@ global:
 	cxl_memdev_zero_label;
 	cxl_memdev_write_label;
 	cxl_memdev_read_label;
+	cxl_memdev_wait_sanitize;
 local:
         *;
 };
cxl/libcxl.h +1 -0
index d699af859a86..c9c935126d4a 100644
@@ -412,6 +412,7 @@ unsigned long long cxl_cmd_partition_get_next_volatile_size(struct cxl_cmd *cmd)
 unsigned long long cxl_cmd_partition_get_next_persistent_size(struct cxl_cmd *cmd);
 struct cxl_cmd *cxl_cmd_new_set_partition(struct cxl_memdev *memdev,
 		unsigned long long volatile_size);
+int cxl_memdev_wait_sanitize(struct cxl_memdev *memdev);
 
 enum cxl_setpartition_mode {
 	CXL_SETPART_NEXTBOOT,
cxl/memdev.c +26 -0
index 0b3ad0223bec..4fc787ffe1e6 100644
@@ -135,6 +135,11 @@ static const struct option free_dpa_options[] = {
 	OPT_END(),
 };
 
+static const struct option wait_sanitize_options[] = {
+	BASE_OPTIONS(),
+	OPT_END(),
+};
+
 enum reserve_dpa_mode {
 	DPA_ALLOC,
 	DPA_FREE,
@@ -653,6 +658,16 @@ out_err:
 	return rc;
 }
 
+static int action_wait_sanitize(struct cxl_memdev *memdev,
+				struct action_context *actx)
+{
+	/*
+	 * It's perfectly ok for the device to be active
+	 * or enabled, so no need to check anything here.
+	 */
+	return cxl_memdev_wait_sanitize(memdev);
+}
+
 static int memdev_action(int argc, const char **argv, struct cxl_ctx *ctx,
 			 int (*action)(struct cxl_memdev *memdev,
 				       struct action_context *actx),
@@ -893,3 +908,14 @@ int cmd_free_dpa(int argc, const char **argv, struct cxl_ctx *ctx)
 
 	return count >= 0 ? 0 : EXIT_FAILURE;
 }
+
+int cmd_wait_sanitize(int argc, const char **argv, struct cxl_ctx *ctx)
+{
+	int count = memdev_action(argc, argv, ctx, action_wait_sanitize,
+		wait_sanitize_options,
+		"cxl wait-sanitize <mem0> [<mem1>..<memn>] [<options>]");
+	log_info(&ml, "sanitation completed on %d mem device%s\n",
+		 count >= 0 ? count : 0, count > 1 ? "s" : "");
+
+	return count >= 0 ? 0 : EXIT_FAILURE;
+}
Signature · 1 line
2.40.0
#2 lore raw

Re: [PATCH 1/2] cxl/memdev: Introduce wait-sanitize functionality

Quoted 20 lines · Davidlohr Bueso

Add a new cxl_memdev_wait_sanitize() to libcxl to support waiting for sanitation operation to be done in the background, if any.

This is analogous to 'ndctl wait-overwrite'.

---
 Documentation/cxl/cxl-wait-sanitize.txt | 35 +++++++++++ Documentation/cxl/lib/libcxl.txt        |  1 + Documentation/cxl/meson.build           |  1 + cxl/builtin.h                           |  1 +
 cxl/cxl.c                               |  1 + cxl/lib/libcxl.c                        | 78 +++++++++++++++++++++++++ cxl/lib/libcxl.sym                      |  1 +
 cxl/libcxl.h                            |  1 +
 cxl/memdev.c                            | 26 +++++++++

 9 files changed, 145 insertions(+)

 create mode 100644 Documentation/cxl/cxl-wait-sanitize.txt

<..>

Quoted 9 lines
cxl/lib/libcxl.sym +1 -0
index 6bc0810152e0..bf7d38f7e6fe 100644
@@ -70,6 +70,7 @@ global:
         cxl_memdev_zero_label;
         cxl_memdev_write_label;
         cxl_memdev_read_label;
+       cxl_memdev_wait_sanitize;
Quoted 3 lines

 local:
         *;
 };

Hi Davidlohr,

These mostly look good - just one thing about the symbol script additions in both this patch and the next one - The different LIBCXL_N sections in this correspond to a library release, and are used for libtool versioning.

New symbols in a new release all go together in a new section in this file. e.g. the pending branch has a 'LIBCXL_5' section for symbols going in the v77 release (corresponding to kernel v6.3). Assuming these patches are targeting v78 / v6.4, its new symbols should go in a new LIBCXL_6 section.

#3 lore raw

Re: [PATCH 1/2] cxl/memdev: Introduce wait-sanitize functionality

Quoted 10 lines · Vishal L

These mostly look good - just one thing about the symbol script additions in both this patch and the next one - The different LIBCXL_N sections in this correspond to a library release, and are used for libtool versioning.

New symbols in a new release all go together in a new section in this file. e.g. the pending branch has a 'LIBCXL_5' section for symbols going in the v77 release (corresponding to kernel v6.3). Assuming these patches are targeting v78 / v6.4, its new symbols should go in a new LIBCXL_6 section.

I really have no specific version of ndctl in mind here, and for the kernel I'm "targeting" v6.5, so no hurry with this. It was mostly have the cxl tool support there for whenever the kernel bits landed (plus recycle testing to exercise the sysfs poll(2) support).

Would it make sense for you to update the libcxl.sym file for whenever you pick this up? If so I can just get rid of the entries in a v2.

Thanks,
Davidlohr

#4 lore raw

Re: [PATCH 1/2] cxl/memdev: Introduce wait-sanitize functionality

Quoted 13 lines · Davidlohr Bueso
Quoted 10 lines · Vishal L

These mostly look good - just one thing about the symbol script additions in both this patch and the next one -  The different LIBCXL_N sections in this correspond to a library release, and are used for libtool versioning.

New symbols in a new release all go together in a new section in this file. e.g. the pending branch has a 'LIBCXL_5' section for symbols going in the v77 release (corresponding to kernel v6.3). Assuming these patches are targeting v78 / v6.4, its new symbols should go in a new LIBCXL_6 section.

Quoted 7 lines

I really have no specific version of ndctl in mind here, and for the kernel I'm "targeting" v6.5, so no hurry with this. It was mostly have the cxl tool support there for whenever the kernel bits landed (plus recycle testing to exercise the sysfs poll(2) support).

Would it make sense for you to update the libcxl.sym file for whenever you pick this up? If so I can just get rid of the entries in a v2.

Ah right - agreed on 6.5 as I think that's the target for mw-update as well, 6.4 stuff is already 'frozen'.

Don't remove the .sym entries - I think the build might even complain - but it is a safe bet to base on the last released version (v76), and then just add a new section for your entries. And I can fix up the inevitable merge-conflicts arising from this (which happens pretty frequently for these anyway).

Quoted 3 lines

Thanks,
Davidlohr

#5 lore raw

PATCH 2/2 cxl/memdev: Introduce sanitize-memdev functionality

In reply to Davidlohr Bueso

Add a new cxl_memdev_sanitize() to libcxl to support triggering memory device sanitation, in either Sanitize and/or Secure Erase, per the CXL 3.0 specs.

This is analogous to 'ndctl sanitize-dimm'.

---
 Documentation/cxl/cxl-sanitize-memdev.txt | 65 +++++++++++++++++++++++
 Documentation/cxl/cxl-wait-sanitize.txt   |  4 ++
 Documentation/cxl/lib/libcxl.txt          |  1 +
 Documentation/cxl/meson.build             |  1 +
 cxl/builtin.h                             |  1 +
 cxl/cxl.c                                 |  1 +
 cxl/lib/libcxl.c                          | 16 ++++++
 cxl/lib/libcxl.sym                        |  1 +
 cxl/libcxl.h                              |  1 +
 cxl/memdev.c                              | 47 ++++++++++++++++
 10 files changed, 138 insertions(+)
 create mode 100644 Documentation/cxl/cxl-sanitize-memdev.txt
Documentation/cxl/cxl-sanitize-memdev.txt new file +65 -0
new file mode 100644
index 000000000000..194a9ad36778
@@ -0,0 +1,65 @@
+// SPDX-License-Identifier: GPL-2.0
+
+cxl-sanitize-memdev(1)
+======================
+
+NAME
+----
+cxl-sanitize-memdev - Perform a cryptographic destruction or sanitation of
+the contents of the given memdevs.
+
+SYNOPSIS
+--------
+[verse]
+'cxl sanitize-memdev' <mem0> [<mem1>..<memN>] [<options>]
+
+DESCRIPTION
+-----------
+The 'sanitize-memdev' command performs two different methods of
+sanitation, per the CXL 3.0 specification. It is required that
+the memdev be disabled before sanitizing, such that the device
+cannot be actively decoding any HPA ranges at the time.
+
+The default is 'sanitize', but additionally, a 'secure-erase'
+option is available. If both types of operations are supplied,
+then the 'secure-erase' is performed before 'sanitize'.
+
+
+OPTIONS
+-------
+<memdev>::
+include::memdev-option.txt[]
+
+-b::
+--bus=::
+include::bus-option.txt[]
+
+-e::
+--secure-erase::
+	Erase user data by changing the media encryption keys for all user
+	data areas of the device.
+
+-s::
+--sanitize::
+	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 label storage area,
+	is made permanently unavailable by whatever means is appropriate for
+	the media type.
+
+	With this option, the sanitation request is merely submitted to the
+	kernel, and the completion is asynchronous. Depending on the medium and
+	capacity, sanitize may take tens of minutes to many hours. Subsequently,
+	'cxl wait-sanitize’ can be used to wait for the memdevs that are under
+	the sanitation.
+
+-v::
+--verbose::
+	Emit debug messages.
+
+include::../copyright.txt[]
+
+SEE ALSO
+--------
+linkcxl:cxl-wait-sanitize[1]
+linkcxl:cxl-disable-memdev[1]
Documentation/cxl/cxl-wait-sanitize.txt +4 -0
index c2ceae7ade0e..7e56c5a3a0ca 100644
@@ -33,3 +33,7 @@ include::bus-option.txt[]
 	Emit debug messages.
 
 include::../copyright.txt[]
+
+SEE ALSO
+--------
+linkcxl:cxl-sanitize-memdev[1]
Documentation/cxl/lib/libcxl.txt +1 -0
index 9d98865e172c..0aae0537aec8 100644
@@ -136,6 +136,7 @@ struct cxl_cmd *cxl_cmd_new_get_partition(struct cxl_memdev *memdev);
 struct cxl_cmd *cxl_cmd_new_set_partition(struct cxl_memdev *memdev,
 					  unsigned long long volatile_size);
 int cxl_memdev_wait_sanitize(struct cxl_memdev *memdev);
+int cxl_memdev_sanitize(struct cxl_memdev *memdev, const char *op);
 
 ----
 
Documentation/cxl/meson.build +1 -0
index ebf214ae30df..ba4e4077c09c 100644
@@ -46,6 +46,7 @@ cxl_manpages = [
   'cxl-enable-region.txt',
   'cxl-destroy-region.txt',
   'cxl-wait-sanitize.txt',
+  'cxl-sanitize-memdev.txt',
   'cxl-monitor.txt',
 ]
 
cxl/builtin.h +1 -0
index 04f613703eac..956a773ffd0e 100644
@@ -23,6 +23,7 @@ int cmd_enable_region(int argc, const char **argv, struct cxl_ctx *ctx);
 int cmd_disable_region(int argc, const char **argv, struct cxl_ctx *ctx);
 int cmd_destroy_region(int argc, const char **argv, struct cxl_ctx *ctx);
 int cmd_wait_sanitize(int argc, const char **argv, struct cxl_ctx *ctx);
+int cmd_sanitize_memdev(int argc, const char **argv, struct cxl_ctx *ctx);
 #ifdef ENABLE_LIBTRACEFS
 int cmd_monitor(int argc, const char **argv, struct cxl_ctx *ctx);
 #else
cxl/cxl.c +1 -0
index bf55e8bcb2f7..4520162ae4fc 100644
@@ -77,6 +77,7 @@ static struct cmd_struct commands[] = {
 	{ "disable-region", .c_fn = cmd_disable_region },
 	{ "destroy-region", .c_fn = cmd_destroy_region },
 	{ "wait-sanitize", .c_fn = cmd_wait_sanitize },
+	{ "sanitize-memdev", .c_fn = cmd_sanitize_memdev },
 	{ "monitor", .c_fn = cmd_monitor },
 };
 
cxl/lib/libcxl.c +16 -0
index f369f45271d5..129d452ad989 100644
@@ -3929,6 +3929,22 @@ CXL_EXPORT int cxl_memdev_wait_sanitize(struct cxl_memdev *memdev)
 	return rc;
 }
 
+int cxl_memdev_sanitize(struct cxl_memdev *memdev, const char *op)
+{
+	struct cxl_ctx *ctx = cxl_memdev_get_ctx(memdev);
+	char *path = memdev->dev_buf;
+	int len = memdev->buf_len;
+
+	if (snprintf(path, len,
+		     "%s/security/%s", memdev->host_path, op)  >= len) {
+		err(ctx, "%s: buffer too small!\n",
+		    cxl_memdev_get_devname(memdev));
+		return -ERANGE;
+	}
+
+	return sysfs_write_attr(ctx, path, "1");
+}
+
 CXL_EXPORT int cxl_cmd_submit(struct cxl_cmd *cmd)
 {
 	struct cxl_memdev *memdev = cmd->memdev;
cxl/lib/libcxl.sym +1 -0
index bf7d38f7e6fe..2b8b47fb89e5 100644
@@ -71,6 +71,7 @@ global:
 	cxl_memdev_write_label;
 	cxl_memdev_read_label;
 	cxl_memdev_wait_sanitize;
+	cxl_memdev_sanitize;
 local:
         *;
 };
cxl/libcxl.h +1 -0
index c9c935126d4a..4f105a0bb3d9 100644
@@ -413,6 +413,7 @@ unsigned long long cxl_cmd_partition_get_next_persistent_size(struct cxl_cmd *cm
 struct cxl_cmd *cxl_cmd_new_set_partition(struct cxl_memdev *memdev,
 		unsigned long long volatile_size);
 int cxl_memdev_wait_sanitize(struct cxl_memdev *memdev);
+int cxl_memdev_sanitize(struct cxl_memdev *memdev, const char *op);
 
 enum cxl_setpartition_mode {
 	CXL_SETPART_NEXTBOOT,
cxl/memdev.c +47 -0

74 lines not on this page · raw message

Signature · 1 line
2.40.0
#6 lore raw

Re: [PATCH 2/2] cxl/memdev: Introduce sanitize-memdev functionality

cxl/lib/libcxl.c CXL_EXPORT int cxl_memdev_wait_sanitize(struct cxl_memdev *memdev) · line 3939

Quoted 8 lines · Davidlohr Bueso
cxl/lib/libcxl.c +8 -0
+int cxl_memdev_sanitize(struct cxl_memdev *memdev, const char *op)
+{
+	struct cxl_ctx *ctx = cxl_memdev_get_ctx(memdev);
+	char *path = memdev->dev_buf;
+	int len = memdev->buf_len;
+
+	if (snprintf(path, len,
+		     "%s/security/%s", memdev->host_path, op)  >= len) {

Bleh I forgot to update this (will do in an eventual v2), s/host_path/dev_path

Thanks,
Davidlohr