public inbox for linux-cxl@vger.kernel.org
 help / color / mirror / Atom feed
From: Ben Cheatham <Benjamin.Cheatham@amd.com>
To: <nvdimm@lists.linux.dev>, <alison.schofield@intel.com>,
	<dave.jiang@intel.com>, <vishal.l.verma@intel.com>
Cc: <linux-cxl@vger.kernel.org>, <benjamin.cheatham@amd.com>
Subject: [PATCH v8 5/7] cxl: Add poison injection/clear commands
Date: Fri, 6 Feb 2026 15:50:06 -0600	[thread overview]
Message-ID: <20260206215008.8810-6-Benjamin.Cheatham@amd.com> (raw)
In-Reply-To: <20260206215008.8810-1-Benjamin.Cheatham@amd.com>

Add the 'cxl-inject-media-poison' and 'cxl-clear-media-poison' commands.
These commands allow the user to inject and clear device poison from CXL
memory devices at a given device physical address.

Signed-off-by: Ben Cheatham <Benjamin.Cheatham@amd.com>
---
 cxl/builtin.h      |   2 +
 cxl/cxl.c          |   2 +
 cxl/inject-error.c | 169 +++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 173 insertions(+)

diff --git a/cxl/builtin.h b/cxl/builtin.h
index ca2e4d1..578f33b 100644
--- a/cxl/builtin.h
+++ b/cxl/builtin.h
@@ -26,6 +26,8 @@ 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_inject_protocol_error(int argc, const char **argv, struct cxl_ctx *ctx);
+int cmd_inject_media_poison(int argc, const char **argv, struct cxl_ctx *ctx);
+int cmd_clear_media_poison(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
diff --git a/cxl/cxl.c b/cxl/cxl.c
index 00ecda0..8dd86ca 100644
--- a/cxl/cxl.c
+++ b/cxl/cxl.c
@@ -81,6 +81,8 @@ static struct cmd_struct commands[] = {
 	{ "destroy-region", .c_fn = cmd_destroy_region },
 	{ "monitor", .c_fn = cmd_monitor },
 	{ "inject-protocol-error", .c_fn = cmd_inject_protocol_error },
+	{ "inject-media-poison", .c_fn = cmd_inject_media_poison },
+	{ "clear-media-poison", .c_fn = cmd_clear_media_poison },
 };
 
 int main(int argc, const char **argv)
diff --git a/cxl/inject-error.c b/cxl/inject-error.c
index ed6fb36..aa15eb6 100644
--- a/cxl/inject-error.c
+++ b/cxl/inject-error.c
@@ -28,6 +28,34 @@ static const struct option proto_inject_options[] = {
 	OPT_END(),
 };
 
+static struct inject_poison_params {
+	const char *address;
+} poison_inj_param;
+
+static struct clear_params {
+	const char *address;
+} poison_clear_param;
+
+static const struct option poison_inject_options[] = {
+	OPT_STRING('a', "address", &poison_inj_param.address,
+		   "Address for poison injection",
+		   "Device physical address for poison injection in hex or decimal"),
+#ifdef ENABLE_DEBUG
+	OPT_BOOLEAN(0, "debug", &debug, "turn on debug output"),
+#endif
+	OPT_END(),
+};
+
+static const struct option poison_clear_options[] = {
+	OPT_STRING('a', "address", &poison_clear_param.address,
+		   "Address for poison clearing",
+		   "Device physical address to clear poison from in hex or decimal"),
+#ifdef ENABLE_DEBUG
+	OPT_BOOLEAN(0, "debug", &debug, "turn on debug output"),
+#endif
+	OPT_END(),
+};
+
 static struct log_ctx iel;
 
 static struct cxl_protocol_error *find_cxl_proto_err(struct cxl_ctx *ctx,
@@ -70,6 +98,20 @@ static struct cxl_dport *find_cxl_dport(struct cxl_ctx *ctx, const char *devname
 	return NULL;
 }
 
+static struct cxl_memdev *find_cxl_memdev(struct cxl_ctx *ctx,
+					  const char *filter)
+{
+	struct cxl_memdev *memdev;
+
+	cxl_memdev_foreach(ctx, memdev) {
+		if (util_cxl_memdev_filter(memdev, filter, NULL))
+			return memdev;
+	}
+
+	log_err(&iel, "Memdev \"%s\" not found\n", filter);
+	return NULL;
+}
+
 static int inject_proto_err(struct cxl_ctx *ctx, const char *devname,
 			    struct cxl_protocol_error *perror)
 {
@@ -141,3 +183,130 @@ int cmd_inject_protocol_error(int argc, const char **argv, struct cxl_ctx *ctx)
 
 	return rc ? EXIT_FAILURE : EXIT_SUCCESS;
 }
+
+static int poison_action(struct cxl_ctx *ctx, const char *filter,
+			 const char *addr_str, bool inj)
+{
+	struct cxl_memdev *memdev;
+	unsigned long long addr;
+	int rc;
+
+	memdev = find_cxl_memdev(ctx, filter);
+	if (!memdev)
+		return -ENODEV;
+
+	if (!cxl_memdev_has_poison_support(memdev, inj)) {
+		log_err(&iel, "%s does not support %s\n",
+			cxl_memdev_get_devname(memdev),
+			inj ? "poison injection" : "clearing poison");
+		return -EINVAL;
+	}
+
+	errno = 0;
+	addr = strtoull(addr_str, NULL, 0);
+	if (addr == ULLONG_MAX && errno == ERANGE) {
+		log_err(&iel, "invalid address: %s", addr_str);
+		return -EINVAL;
+	}
+
+	if (inj)
+		rc = cxl_memdev_inject_poison(memdev, addr);
+	else
+		rc = cxl_memdev_clear_poison(memdev, addr);
+
+	if (rc)
+		log_err(&iel, "failed to %s %s:%s: %s\n",
+			inj ? "inject poison at" : "clear poison at",
+			cxl_memdev_get_devname(memdev), addr_str, strerror(-rc));
+	else
+		log_info(&iel,
+			 "poison %s at %s:%s\n", inj ? "injected" : "cleared",
+			 cxl_memdev_get_devname(memdev), addr_str);
+
+	return rc;
+}
+
+static int inject_poison_action(int argc, const char **argv,
+				struct cxl_ctx *ctx,
+				const struct option *options, const char *usage)
+{
+	const char * const u[] = {
+		usage,
+		NULL
+	};
+	int rc = -EINVAL;
+
+	log_init(&iel, "cxl inject-media-poison", "CXL_CLEAR_LOG");
+	argc = parse_options(argc, argv, options, u, 0);
+
+	if (debug) {
+		cxl_set_log_priority(ctx, LOG_DEBUG);
+		iel.log_priority = LOG_DEBUG;
+	} else {
+		iel.log_priority = LOG_INFO;
+	}
+
+	if (argc != 1 || !poison_inj_param.address) {
+		usage_with_options(u, options);
+		return rc;
+	}
+
+	rc = poison_action(ctx, argv[0], poison_inj_param.address, true);
+	if (rc) {
+		log_err(&iel, "Failed to inject poison on %s: %s\n", argv[0],
+			strerror(-rc));
+		return rc;
+	}
+
+	return rc;
+}
+
+int cmd_inject_media_poison(int argc, const char **argv, struct cxl_ctx *ctx)
+{
+	int rc = inject_poison_action(argc, argv, ctx, poison_inject_options,
+				      "inject-media-poison <memdev> -a <address> [<options>]");
+
+	return rc ? EXIT_FAILURE : EXIT_SUCCESS;
+}
+
+static int clear_poison_action(int argc, const char **argv, struct cxl_ctx *ctx,
+			       const struct option *options, const char *usage)
+{
+	const char * const u[] = {
+		usage,
+		NULL
+	};
+	int rc = -EINVAL;
+
+	log_init(&iel, "cxl clear-media-poison", "CXL_CLEAR_LOG");
+	argc = parse_options(argc, argv, options, u, 0);
+
+	if (debug) {
+		cxl_set_log_priority(ctx, LOG_DEBUG);
+		iel.log_priority = LOG_DEBUG;
+	} else {
+		iel.log_priority = LOG_INFO;
+	}
+
+	if (argc != 1 || !poison_clear_param.address) {
+		usage_with_options(u, options);
+		return rc;
+	}
+
+	rc = poison_action(ctx, argv[0], poison_clear_param.address, false);
+	if (rc) {
+		log_err(&iel, "Failed to clear poison on %s: %s\n", argv[0],
+			strerror(-rc));
+		return rc;
+	}
+
+	return rc;
+}
+
+int cmd_clear_media_poison(int argc, const char **argv, struct cxl_ctx *ctx)
+{
+	int rc = clear_poison_action(argc, argv, ctx, poison_clear_options,
+				     "clear-error <memdev> -a <address> [<options>]");
+
+	return rc ? EXIT_FAILURE : EXIT_SUCCESS;
+}
-- 
2.52.0


  parent reply	other threads:[~2026-02-06 21:50 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-06 21:50 [ndctl PATCH v8 0/7] Add error injection support Ben Cheatham
2026-02-06 21:50 ` [PATCH v8 1/7] libcxl: Add debugfs path to CXL context Ben Cheatham
2026-02-06 21:50 ` [PATCH v8 2/7] libcxl: Add CXL protocol errors Ben Cheatham
2026-02-06 21:50 ` [PATCH v8 3/7] libcxl: Add poison injection support Ben Cheatham
2026-02-06 21:50 ` [PATCH v8 4/7] cxl: Add inject-protocol-error command Ben Cheatham
2026-02-06 21:50 ` Ben Cheatham [this message]
2026-02-06 21:50 ` [PATCH v8 6/7] cxl/list: Add injectable errors in output Ben Cheatham
2026-02-06 21:50 ` [PATCH v8 7/7] Documentation: Add docs for protocol and poison injection commands Ben Cheatham
2026-02-10 17:52 ` [ndctl PATCH v8 0/7] Add error injection support Verma, Vishal L
2026-02-13  0:12 ` Alison Schofield
2026-02-16 14:28   ` Cheatham, Benjamin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260206215008.8810-6-Benjamin.Cheatham@amd.com \
    --to=benjamin.cheatham@amd.com \
    --cc=alison.schofield@intel.com \
    --cc=dave.jiang@intel.com \
    --cc=linux-cxl@vger.kernel.org \
    --cc=nvdimm@lists.linux.dev \
    --cc=vishal.l.verma@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox