Skip to contentSkip to conversation

cxl: Activate FW and userspace bgcmds

PATCH In review Davidlohr Bueso 3 participants 20 messages Jun 17, 2025 – Nov 28 CXL

lore
Share & tools
lore

Davidlohr Bueso

author maintainer
#0 lore raw

cover cxl: Activate FW and userspace bgcmds

Hello,

The following addresses two of the requests by Micron, both for general userspace bgcmd support as well as the more recent sync meeting discussion around activate FW taking too long and timing out.

This builds on the previous proposal:
https://lore.kernel.org/linux-cxl/[email protected]/

... with 2 main differences in this series (i) added the Activate FW work; and (ii) prevents kernel canceling another kernel bgcmd - this may later be extended for kernel policies, if required.

Overall this expands on the monopolizing side of things (async to the driver):
- user bgcmds are only enabled if they can be canceled
- activate fw is not timeslicable, but the expectation was that it will not run for long (online activation is currently unsupported). This is not the case apparently.

Patch 1 is generic to all further functionality. Patch 2 moves activate FW to the monopolizing (aka sanitize) category.
Patch 3+4 are prep patches for canceling bg commands. Patch 5 adds the cancelable machinery, no actual commands yet. Patch 6 adds a sensible warning instead of silently failing. Patch 7 adds the Populate Log command to the uapi.

Testing has been done via qemu, similar to the previous proposal (hacking the code further to disable the userspace only policy).

Micron folks: this does require further adhoc testing, of course, so feedback welcome.

Jonathan: does this satisfy the Maintanence requirements?

Applies against the next branch in the cxl.git tree.

Thanks!

Davidlohr Bueso (6):
cxl/mbox: Track background commands from CEL cxl/mbox: Handle Activate FW as async bg
cxl/pci: Lockless background synchronous polling cxl/mbox: Stronger cxl_mbox_background_complete() semantics cxl/mbox: Allow userspace background commands cxl/mbox: Shout upon async bgcmd race

Ravi Shankar (1):
cxl/mbox: Add Populate Log support

 drivers/cxl/core/hdm.c       |   2 +-
 drivers/cxl/core/mbox.c      |  41 +++++-
 drivers/cxl/core/memdev.c    |   4 +-
 drivers/cxl/cxlmem.h         |  44 +++++-
 drivers/cxl/pci.c            | 269 +++++++++++++++++++++++++----------
 include/cxl/mailbox.h        |   4 +
 include/uapi/linux/cxl_mem.h |   1 +
 7 files changed, 276 insertions(+), 89 deletions(-)

--
2.39.5

author maintainer
#1 lore raw

PATCH 1/7 cxl/mbox: Track background commands from CEL

Remember whether or not a command is background-capable, per the CEL. This will later be used to consult the bitmap to evaluate incoming mbox commands.

Further, "enable" any background commands that didn't fall in the uapi, poison or security categories, such as fw related. This prevents situations where the command is incorrectly listed as unsupported by the driver.

---
 drivers/cxl/core/mbox.c | 30 ++++++++++++++++++++++++++++++
 drivers/cxl/cxlmem.h    | 28 ++++++++++++++++++++++++++++
 2 files changed, 58 insertions(+)
drivers/cxl/core/mbox.c +30 -0
index 2689e6453c5a..4ea02f7a5b6b 100644
@@ -202,6 +202,31 @@ static void cxl_set_poison_cmd_enabled(struct cxl_poison_state *poison,
 	}
 }
 
+static bool cxl_set_bgcmd_enabled(struct cxl_background_state *bg, u16 opcode)
+{
+	switch (opcode) {
+	case CXL_MBOX_OP_TRANSFER_FW:
+		set_bit(CXL_BG_ENABLED_TRANSFER_FW, bg->enabled_cmds);
+		break;
+	case CXL_MBOX_OP_ACTIVATE_FW:
+		set_bit(CXL_BG_ENABLED_ACTIVATE_FW, bg->enabled_cmds);
+		break;
+	case CXL_MBOX_OP_DO_MAINTENANCE:
+		set_bit(CXL_BG_ENABLED_DO_MAINTENANCE, bg->enabled_cmds);
+		break;
+	case CXL_MBOX_OP_SCAN_MEDIA:
+		set_bit(CXL_BG_ENABLED_SCAN_MEDIA, bg->enabled_cmds);
+		break;
+	case CXL_MBOX_OP_SANITIZE:
+		set_bit(CXL_BG_ENABLED_SANITIZE, bg->enabled_cmds);
+		break;
+	default:
+		return false;
+	}
+
+	return true;
+}
+
 static struct cxl_mem_command *cxl_mem_find_command(u16 opcode)
 {
 	struct cxl_mem_command *c;
@@ -757,6 +782,7 @@ static void cxl_walk_cel(struct cxl_memdev_state *mds, size_t size, u8 *cel)
 
 	for (i = 0; i < cel_entries; i++) {
 		u16 opcode = le16_to_cpu(cel_entry[i].opcode);
+		u16 effect = le16_to_cpu(cel_entry[i].effect);
 		struct cxl_mem_command *cmd = cxl_mem_find_command(opcode);
 		int enabled = 0;
 
@@ -778,6 +804,10 @@ static void cxl_walk_cel(struct cxl_memdev_state *mds, size_t size, u8 *cel)
 			enabled++;
 		}
 
+		if (effect & CXL_CEL_BACKGROUND_OPERATION)
+			if (cxl_set_bgcmd_enabled(&mds->bg, opcode))
+				enabled++; /* might be redundant */
+
 		dev_dbg(dev, "Opcode 0x%04x %s\n", opcode,
 			enabled ? "enabled" : "unsupported by driver");
 	}
drivers/cxl/cxlmem.h +28 -0
index 551b0ba2caa1..5c7fd4a6704c 100644
@@ -248,6 +248,16 @@ enum security_cmd_enabled_bits {
 	CXL_SEC_ENABLED_MAX
 };
 
+/* Device enabled background commands */
+enum background_cmd_enabled_bits {
+	CXL_BG_ENABLED_TRANSFER_FW,
+	CXL_BG_ENABLED_ACTIVATE_FW,
+	CXL_BG_ENABLED_DO_MAINTENANCE,
+	CXL_BG_ENABLED_SCAN_MEDIA,
+	CXL_BG_ENABLED_SANITIZE,
+	CXL_BG_ENABLED_MAX
+};
+
 /**
  * struct cxl_poison_state - Driver poison state info
  *
@@ -365,6 +375,16 @@ struct cxl_security_state {
 	struct kernfs_node *sanitize_node;
 };
 
+/**
+ * struct cxl_background_state - Driver background operation state info
+ *
+ * @enabled_cmds: All background commands enabled in the CEL
+ */
+struct cxl_background_state {
+	DECLARE_BITMAP(enabled_cmds, CXL_BG_ENABLED_MAX);
+};
+
+
 /*
  * enum cxl_devtype - delineate type-2 from a generic type-3 device
  * @CXL_DEVTYPE_DEVMEM - Vendor specific CXL Type-2 device implementing HDM-D or
@@ -484,6 +504,7 @@ static inline struct cxl_dev_state *mbox_to_cxlds(struct cxl_mailbox *cxl_mbox)
  * @poison: poison driver state info
  * @security: security driver state info
  * @fw: firmware upload / activation state
+ * @bg: background operation state
  * @mce_notifier: MCE notifier
  *
  * See CXL 3.0 8.2.9.8.2 Capacity Configuration and Label Storage for
@@ -504,6 +525,7 @@ struct cxl_memdev_state {
 	struct cxl_poison_state poison;
 	struct cxl_security_state security;
 	struct cxl_fw_state fw;
+	struct cxl_background_state bg;
 	struct notifier_block mce_notifier;
 };
 
@@ -580,6 +602,12 @@ struct cxl_mbox_get_supported_logs {
 	} __packed entry[];
 }  __packed;
 
+/*
+ * Command Effects Log (CEL)
+ * CXL 3.2 Section 8.2.10.5.2.1; Table 8-87
+ */
+#define CXL_CEL_BACKGROUND_OPERATION BIT(6)
+
 struct cxl_cel_entry {
 	__le16 opcode;
 	__le16 effect;
Signature · 1 line
2.39.5
#2 lore raw

Re: [PATCH 1/7] cxl/mbox: Track background commands from CEL

Quoted 60 lines · On Tue, 17 Jun 2025 12:36:05 -0700 Davidlohr Bueso <[email protected]> wrote:

Remember whether or not a command is background-capable, per the CEL. This will later be used to consult the bitmap to evaluate incoming mbox commands.

Further, "enable" any background commands that didn't fall in the uapi, poison or security categories, such as fw related. This prevents situations where the command is incorrectly listed as unsupported by the driver.

---
 drivers/cxl/core/mbox.c | 30 ++++++++++++++++++++++++++++++
 drivers/cxl/cxlmem.h    | 28 ++++++++++++++++++++++++++++
 2 files changed, 58 insertions(+)
drivers/cxl/core/mbox.c +26 -0
index 2689e6453c5a..4ea02f7a5b6b 100644
@@ -202,6 +202,31 @@ static void cxl_set_poison_cmd_enabled(struct cxl_poison_state *poison,
 	}
 }
 
+static bool cxl_set_bgcmd_enabled(struct cxl_background_state *bg, u16 opcode)
+{
+	switch (opcode) {
+	case CXL_MBOX_OP_TRANSFER_FW:
+		set_bit(CXL_BG_ENABLED_TRANSFER_FW, bg->enabled_cmds);
+		break;
+	case CXL_MBOX_OP_ACTIVATE_FW:
+		set_bit(CXL_BG_ENABLED_ACTIVATE_FW, bg->enabled_cmds);
+		break;
+	case CXL_MBOX_OP_DO_MAINTENANCE:
+		set_bit(CXL_BG_ENABLED_DO_MAINTENANCE, bg->enabled_cmds);
+		break;
+	case CXL_MBOX_OP_SCAN_MEDIA:
+		set_bit(CXL_BG_ENABLED_SCAN_MEDIA, bg->enabled_cmds);
+		break;
+	case CXL_MBOX_OP_SANITIZE:
+		set_bit(CXL_BG_ENABLED_SANITIZE, bg->enabled_cmds);
+		break;
+	default:
+		return false;
+	}
+
+	return true;
+}
+
 static struct cxl_mem_command *cxl_mem_find_command(u16 opcode)
 {
 	struct cxl_mem_command *c;
@@ -757,6 +782,7 @@ static void cxl_walk_cel(struct cxl_memdev_state *mds, size_t size, u8 *cel)
 
 	for (i = 0; i < cel_entries; i++) {
 		u16 opcode = le16_to_cpu(cel_entry[i].opcode);
+		u16 effect = le16_to_cpu(cel_entry[i].effect);
 		struct cxl_mem_command *cmd = cxl_mem_find_command(opcode);
 		int enabled = 0;
 

drivers/cxl/core/mbox.c static void cxl_walk_cel(struct cxl_memdev_state *mds, size_t size, u8 *cel) · line 809

Quoted 6 lines
drivers/cxl/core/mbox.c +3 -0
@@ -778,6 +804,10 @@ static void cxl_walk_cel(struct cxl_memdev_state *mds, size_t size, u8 *cel)
 			enabled++;
 		}
 
+		if (effect & CXL_CEL_BACKGROUND_OPERATION)
+			if (cxl_set_bgcmd_enabled(&mds->bg, opcode))
+				enabled++; /* might be redundant */

I suspect we'll forget in the long run what 'redundant' means here. Perhaps make it more explicit.
/* May already have been detected as enabled */

The whole incrementing of enabled pattern hides that we don't really care about transitions other than 0->1. Maybe we should consider making it a bool and using
enabled |= check_features_opcode() a few lines above this.

drivers/cxl/cxlmem.h enum security_cmd_enabled_bits { · line 251

Quoted 13 lines
drivers/cxl/core/mbox.c +1 -0
+
 		dev_dbg(dev, "Opcode 0x%04x %s\n", opcode,
 			enabled ? "enabled" : "unsupported by driver");
 	}
drivers/cxl/cxlmem.h +1 -0
index 551b0ba2caa1..5c7fd4a6704c 100644
@@ -248,6 +248,16 @@ enum security_cmd_enabled_bits {
 	CXL_SEC_ENABLED_MAX
 };
 
+/* Device enabled background commands */

What does 'Device enabled' refer to? Perhaps reword.

Quoted 58 lines
drivers/cxl/cxlmem.h +27 -0
+enum background_cmd_enabled_bits {
+	CXL_BG_ENABLED_TRANSFER_FW,
+	CXL_BG_ENABLED_ACTIVATE_FW,
+	CXL_BG_ENABLED_DO_MAINTENANCE,
+	CXL_BG_ENABLED_SCAN_MEDIA,
+	CXL_BG_ENABLED_SANITIZE,
+	CXL_BG_ENABLED_MAX
+};
+
 /**
  * struct cxl_poison_state - Driver poison state info
  *
@@ -365,6 +375,16 @@ struct cxl_security_state {
 	struct kernfs_node *sanitize_node;
 };
 
+/**
+ * struct cxl_background_state - Driver background operation state info
+ *
+ * @enabled_cmds: All background commands enabled in the CEL
+ */
+struct cxl_background_state {
+	DECLARE_BITMAP(enabled_cmds, CXL_BG_ENABLED_MAX);
+};
+
+
 /*
  * enum cxl_devtype - delineate type-2 from a generic type-3 device
  * @CXL_DEVTYPE_DEVMEM - Vendor specific CXL Type-2 device implementing HDM-D or
@@ -484,6 +504,7 @@ static inline struct cxl_dev_state *mbox_to_cxlds(struct cxl_mailbox *cxl_mbox)
  * @poison: poison driver state info
  * @security: security driver state info
  * @fw: firmware upload / activation state
+ * @bg: background operation state
  * @mce_notifier: MCE notifier
  *
  * See CXL 3.0 8.2.9.8.2 Capacity Configuration and Label Storage for
@@ -504,6 +525,7 @@ struct cxl_memdev_state {
 	struct cxl_poison_state poison;
 	struct cxl_security_state security;
 	struct cxl_fw_state fw;
+	struct cxl_background_state bg;
 	struct notifier_block mce_notifier;
 };
 
@@ -580,6 +602,12 @@ struct cxl_mbox_get_supported_logs {
 	} __packed entry[];
 }  __packed;
 
+/*
+ * Command Effects Log (CEL)
+ * CXL 3.2 Section 8.2.10.5.2.1; Table 8-87
+ */
+#define CXL_CEL_BACKGROUND_OPERATION BIT(6)
+
 struct cxl_cel_entry {
 	__le16 opcode;
 	__le16 effect;
author maintainer
#3 lore raw

Re: [PATCH 1/7] cxl/mbox: Track background commands from CEL

Quoted 7 lines · Jonathan Cameron
Quoted 4 lines · On Tue, 17 Jun 2025 12:36:05 -0700 Davidlohr Bueso <[email protected]> wrote:
drivers/cxl/core/mbox.c +0 -0
@@ -778,6 +804,10 @@ static void cxl_walk_cel(struct cxl_memdev_state *mds, size_t size, u8 *cel)
 			enabled++;
 		}
 
Quoted 7 lines

drivers/cxl/core/mbox.c static void cxl_walk_cel(struct cxl_memdev_state *mds, size_t size, u8 *cel) · line 809

Quoted 3 lines
drivers/cxl/core/mbox.c +3 -0
+		if (effect & CXL_CEL_BACKGROUND_OPERATION)
+			if (cxl_set_bgcmd_enabled(&mds->bg, opcode))
+				enabled++; /* might be redundant */

I suspect we'll forget in the long run what 'redundant' means here. Perhaps make it more explicit.
/* May already have been detected as enabled */

Sure.

Quoted 4 lines

The whole incrementing of enabled pattern hides that we don't really care about transitions other than 0->1. Maybe we should consider making it a bool and using
enabled |= check_features_opcode() a few lines above this.

Yeah, will consider as a post series cleanup.

author maintainer
#4 lore raw

PATCH 2/7 cxl/mbox: Handle Activate FW as async bg

In reply to Davidlohr Bueso

Allow Activate FW to behave as Sanitize by running in an async context. These commands cannot be timesliced, and while it was the expectation that hardware vendors perform (offline) activating FW in a timely fashion, this might not always be the case.

This further reduces the timeout scenarios when handling the bg command synchronously.

Decouple the async background handling from security.

---
 drivers/cxl/core/hdm.c    |  2 +-
 drivers/cxl/core/memdev.c |  4 +-
 drivers/cxl/cxlmem.h      | 13 +++---
 drivers/cxl/pci.c         | 91 ++++++++++++++++++++++++++-------------
 4 files changed, 71 insertions(+), 39 deletions(-)
drivers/cxl/core/hdm.c +1 -1
index ab1007495f6b..0f6402b2faa6 100644
@@ -795,7 +795,7 @@ static int cxl_decoder_commit(struct cxl_decoder *cxld)
 		struct cxl_memdev_state *mds =
 			to_cxl_memdev_state(cxlmd->cxlds);
 
-		if (mds && mds->security.sanitize_active) {
+		if (mds && mds->bg.opcode == CXL_MBOX_OP_SANITIZE) {
 			dev_dbg(&cxlmd->dev,
 				"attempted to commit %s during sanitize\n",
 				dev_name(&cxld->dev));
drivers/cxl/core/memdev.c +2 -2
index f88a13adf7fa..d82f8ce182d8 100644
@@ -140,7 +140,7 @@ static ssize_t security_state_show(struct device *dev,
 
 	/* sync with latest submission state */
 	mutex_lock(&cxl_mbox->mbox_mutex);
-	if (mds->security.sanitize_active)
+	if (mds->bg.opcode == CXL_MBOX_OP_SANITIZE)
 		rc = sysfs_emit(buf, "sanitize\n");
 	mutex_unlock(&cxl_mbox->mbox_mutex);
 	if (rc)
@@ -1097,7 +1097,7 @@ static void sanitize_teardown_notifier(void *data)
 	mds->security.sanitize_node = NULL;
 	mutex_unlock(&cxl_mbox->mbox_mutex);
 
-	cancel_delayed_work_sync(&mds->security.poll_dwork);
+	cancel_delayed_work_sync(&mds->bg.poll_dwork);
 	sysfs_put(state);
 }
 
drivers/cxl/cxlmem.h +6 -7
index 5c7fd4a6704c..6fe42871fbf4 100644
@@ -361,17 +361,11 @@ struct cxl_fw_state {
  *
  * @state: state of last security operation
  * @enabled_cmds: All security commands enabled in the CEL
- * @poll_tmo_secs: polling timeout
- * @sanitize_active: sanitize completion pending
- * @poll_dwork: polling work item
  * @sanitize_node: sanitation sysfs file to notify
  */
 struct cxl_security_state {
 	unsigned long state;
 	DECLARE_BITMAP(enabled_cmds, CXL_SEC_ENABLED_MAX);
-	int poll_tmo_secs;
-	bool sanitize_active;
-	struct delayed_work poll_dwork;
 	struct kernfs_node *sanitize_node;
 };
 
@@ -379,12 +373,17 @@ struct cxl_security_state {
  * struct cxl_background_state - Driver background operation state info
  *
  * @enabled_cmds: All background commands enabled in the CEL
+ * @poll_dwork: polling background work item
+ * @poll_tmo_secs: polling timeout
+ * @opcode: async running background command
  */
 struct cxl_background_state {
 	DECLARE_BITMAP(enabled_cmds, CXL_BG_ENABLED_MAX);
+	struct delayed_work poll_dwork;
+	int poll_tmo_secs;
+	int opcode;
 };
 
-
 /*
  * enum cxl_devtype - delineate type-2 from a generic type-3 device
  * @CXL_DEVTYPE_DEVMEM - Vendor specific CXL Type-2 device implementing HDM-D or
drivers/cxl/pci.c +62 -29

150 lines not on this page · raw message

Signature · 1 line
2.39.5
Reviewed-by on v1 2/7
#5 lore raw

Re: [PATCH 2/7] cxl/mbox: Handle Activate FW as async bg

Quoted 11 lines · On Tue, 17 Jun 2025 12:36:06 -0700 Davidlohr Bueso <[email protected]> wrote:

Allow Activate FW to behave as Sanitize by running in an async context. These commands cannot be timesliced, and while it was the expectation that hardware vendors perform (offline) activating FW in a timely fashion, this might not always be the case.

This further reduces the timeout scenarios when handling the bg command synchronously.

Decouple the async background handling from security.

Trivial comment inline.

Quoted 28 lines
drivers/cxl/cxlmem.h +2 -6
index 5c7fd4a6704c..6fe42871fbf4 100644
@@ -361,17 +361,11 @@ struct cxl_fw_state {
  *
  * @state: state of last security operation
  * @enabled_cmds: All security commands enabled in the CEL
- * @poll_tmo_secs: polling timeout
- * @sanitize_active: sanitize completion pending
- * @poll_dwork: polling work item
  * @sanitize_node: sanitation sysfs file to notify
  */
 struct cxl_security_state {
 	unsigned long state;
 	DECLARE_BITMAP(enabled_cmds, CXL_SEC_ENABLED_MAX);
-	int poll_tmo_secs;
-	bool sanitize_active;
-	struct delayed_work poll_dwork;
 	struct kernfs_node *sanitize_node;
 };
 
@@ -379,12 +373,17 @@ struct cxl_security_state {
  * struct cxl_background_state - Driver background operation state info
  *
  * @enabled_cmds: All background commands enabled in the CEL
+ * @poll_dwork: polling background work item
+ * @poll_tmo_secs: polling timeout

drivers/cxl/cxlmem.h struct cxl_security_state { · line 387

Quoted 10 lines
drivers/cxl/cxlmem.h +4 -1
+ * @opcode: async running background command
  */
 struct cxl_background_state {
 	DECLARE_BITMAP(enabled_cmds, CXL_BG_ENABLED_MAX);
+	struct delayed_work poll_dwork;
+	int poll_tmo_secs;
+	int opcode;
 };
 
-

Stray change?

Quoted 3 lines
drivers/cxl/cxlmem.h +0 -0
 /*
  * enum cxl_devtype - delineate type-2 from a generic type-3 device
  * @CXL_DEVTYPE_DEVMEM - Vendor specific CXL Type-2 device implementing HDM-D or
#6 lore raw

Re: [PATCH 2/7] cxl/mbox: Handle Activate FW as async bg

In reply to Davidlohr Bueso

Hi Davidlohr,

Since you mentioned you are rebasing this series, here are two problems I hit while building on top of it, in case they help before you resend. This is on 1/7 and 2/7 rebased onto v7.4-rc4, so apologies if your rebase already covers them.

1. tools/testing/cxl no longer builds

This patch moves poll_dwork and the sanitize flag from cxl_security_state to cxl_background_state, but the cxl_test mock sanitize still uses the old fields:

test/mem.c:668: error: 'struct cxl_security_state' has no member named 'poll_dwork' test/mem.c:674: error: 'struct cxl_security_state' has no member named 'sanitize_active'

I saw this on the rebase. The series does not touch tools/testing, and the mock already used these fields in June 2025, so I think the posted version is affected as well, but I have not built that one. This is what I used locally:

tools/testing/cxl/test/mem.c +5 -5
@@ -665,13 +665,13 @@ static int mock_partition_info(struct cxl_mbox_cmd *cmd)
 void cxl_mockmem_sanitize_work(struct work_struct *work)
 {
 	struct cxl_memdev_state *mds =
-		container_of(work, typeof(*mds), security.poll_dwork.work);
+		container_of(work, typeof(*mds), bg.poll_dwork.work);
 	struct cxl_mailbox *cxl_mbox = &mds->cxlds.cxl_mbox;
 
 	mutex_lock(&cxl_mbox->mbox_mutex);
 	if (mds->security.sanitize_node)
 		sysfs_notify_dirent(mds->security.sanitize_node);
-	mds->security.sanitize_active = false;
+	mds->bg.opcode = 0;
 	mutex_unlock(&cxl_mbox->mbox_mutex);
 
 	dev_dbg(mds->cxlds.dev, "sanitize complete\n");
@@ -700,9 +700,9 @@ static int mock_sanitize(struct cxl_mockmem_data *mdata,
 	}
 
 	mutex_lock(&cxl_mbox->mbox_mutex);
-	if (schedule_delayed_work(&mds->security.poll_dwork,
+	if (schedule_delayed_work(&mds->bg.poll_dwork,
 				  msecs_to_jiffies(mdata->sanitize_timeout))) {
-		mds->security.sanitize_active = true;
+		mds->bg.opcode = CXL_MBOX_OP_SANITIZE;
 		dev_dbg(mds->cxlds.dev, "sanitize issued\n");
 	} else
 		rc = -EBUSY;
@@ -1766,7 +1766,7 @@ static int cxl_mock_mem_probe(struct platform_device *pdev)
 	cxl_mbox->mbox_send = cxl_mock_mbox_send;
 	cxl_mbox->payload_size = SZ_4K;
 	mds->event.buf = (struct cxl_get_event_payload *) mdata->event_buf;
-	INIT_DELAYED_WORK(&mds->security.poll_dwork, cxl_mockmem_sanitize_work);
+	INIT_DELAYED_WORK(&mds->bg.poll_dwork, cxl_mockmem_sanitize_work);
 
 	if (is_rcd(pdev))
 		cxlds->rcd = true;

2. bg.poll_dwork can outlive cxl_pci after unbind

The only cancel_delayed_work_sync() of the poll work is in sanitize_teardown_notifier(), which devm_cxl_sanitize_setup_notifier() registers only when the device supports sanitize. With Activate FW now also going through the async path, including the reset activation at the end of a firmware upload, a device without sanitize can still have the work queued when cxl_pci is unbound, and the timer then runs on the freed cxl_memdev_state.

QEMU and cxl_test both support sanitize, so to reproduce it I used two local test-only hacks: clear CXL_SEC_ENABLED_SANITIZE before the notifier setup, and make the foreground Activate FW from QEMU look like a background command that runs for 60 seconds. Then, with KASAN:

  cxl update-firmware mem0 -F fw.bin --wait
  echo 0000:0d:00.0 > /sys/bus/pci/drivers/cxl_pci/unbind

About a second later:

  BUG: KASAN: slab-use-after-free in __run_timers+0x794/0x870
  Write of size 8 at addr ffff88810104a480 by task swapper/3/0
   __run_timers+0x794/0x870
   run_timer_softirq+0xe9/0x1c0
  Allocated by task 13:
   devm_kmalloc+0x72/0x230
   _devm_cxl_dev_state_create+0x2b/0x1f0
   cxl_memdev_state_create+0x26/0x70
   cxl_pci_probe+0x185/0x1840
  Freed by task 678:
   release_nodes+0xb9/0x110
   devres_release_all+0x104/0x170
   device_unbind_cleanup+0x71/0x250
   device_release_driver_internal+0x393/0x520
   unbind_store+0xdb/0xf0

With the sanitize notifier left in place there is no report. Cancelling the work with a devm action next to INIT_DELAYED_WORK() made the report go away in the same setup, and rebinding worked:

drivers/cxl/pci.c +12 -1
@@ -420,6 +420,13 @@ static int cxl_pci_mbox_send(struct cxl_mailbox *cxl_mbox,
 	return rc;
 }
 
+static void cxl_mbox_bgpoll_cancel(void *data)
+{
+	struct cxl_memdev_state *mds = data;
+
+	cancel_delayed_work_sync(&mds->bg.poll_dwork);
+}
+
 static int cxl_pci_setup_mailbox(struct cxl_memdev_state *mds, bool irq_avail)
 {
 	struct cxl_dev_state *cxlds = &mds->cxlds;
@@ -427,7 +434,7 @@ static int cxl_pci_setup_mailbox(struct cxl_memdev_state *mds, bool irq_avail)
 	const int cap = readl(cxlds->regs.mbox + CXLDEV_MBOX_CAPS_OFFSET);
 	struct device *dev = cxlds->dev;
 	unsigned long timeout;
-	int irq, msgnum;
+	int irq, msgnum, rc;
 	u64 md_status;
 	u32 ctrl;
 
@@ -477,6 +484,10 @@ static int cxl_pci_setup_mailbox(struct cxl_memdev_state *mds, bool irq_avail)
 	dev_dbg(dev, "Mailbox payload sized %zu", cxl_mbox->payload_size);
 
 	INIT_DELAYED_WORK(&mds->bg.poll_dwork, cxl_mbox_bgpoll_work);
+	/* Any background command, not only sanitize, may leave it queued */
+	rc = devm_add_action_or_reset(dev, cxl_mbox_bgpoll_cancel, mds);
+	if (rc)
+		return rc;
 
 	/* background command interrupts are optional */
 	if (!(cap & CXLDEV_MBOX_CAP_BG_CMD_IRQ) || !irq_avail)

The action is registered after the memdev state is allocated, so it runs before that is freed, and the mailbox irq is requested later, so it is already gone when the work is cancelled. Please feel free to fold either change into your patches or do it differently.

One smaller observation from the same testing, in case it matters for how completion is reported: without an interrupt, a 5 second background activation was seen as complete after about 12 seconds, since the poller checks after 1 s and then backs off by 10 s.

I used an AI coding assistant to help reproduce these two issues;

Thanks,

Unsung

author maintainer
#7 lore raw

PATCH 3/7 cxl/pci: Lockless background synchronous polling

In reply to Davidlohr Bueso

For timesliced background commands we rely on holding the mbox_mutex throughout the duration of the operation. This causes other incoming commands to queue up behind, and interleaving executions while the background command is timesliced by the user.

However, in order to support the mbox request cancel background operation command, the lock will need to be available to actually perform the request. Semantically this allows other commands to many times be at the mercy of hardware returning the respective error. Potentially users would be exposed to changes in the form of errors instead of commands taking longer to run - but this is not foreseen as a problem.

In order to not loose sync with the hardware, introduce a mailbox atomic that blocks any other incoming bg operations while the driver is still polling (synchronously) on the current one.

---
 drivers/cxl/core/mbox.c |   1 +
 drivers/cxl/pci.c       | 112 ++++++++++++++++++++++++----------------
 include/cxl/mailbox.h   |   2 +
 3 files changed, 70 insertions(+), 45 deletions(-)
drivers/cxl/core/mbox.c +1 -0
index 4ea02f7a5b6b..aa1b04d8a78e 100644
@@ -1515,6 +1515,7 @@ int cxl_mailbox_init(struct cxl_mailbox *cxl_mbox, struct device *host)
 
 	cxl_mbox->host = host;
 	mutex_init(&cxl_mbox->mbox_mutex);
+	atomic_set(&cxl_mbox->poll_bgop, 0);
 	rcuwait_init(&cxl_mbox->mbox_wait);
 
 	return 0;
drivers/cxl/pci.c +67 -45

148 lines not on this page · raw message

include/cxl/mailbox.h +2 -0
index c4e99e2e3a9d..f3c60158bfca 100644
@@ -51,6 +51,7 @@ struct cxl_mbox_cmd {
  *                (CXL 3.1 8.2.8.4.3 Mailbox Capabilities Register)
  * @mbox_mutex: mutex protects device mailbox and firmware
  * @mbox_wait: rcuwait for mailbox
+ * @poll_bgop: current background operation being polled on
  * @mbox_send: @dev specific transport for transmitting mailbox commands
  * @feat_cap: Features capability
  */
@@ -61,6 +62,7 @@ struct cxl_mailbox {
 	size_t payload_size;
 	struct mutex mbox_mutex; /* lock to protect mailbox context */
 	struct rcuwait mbox_wait;
+	atomic_t poll_bgop;
 	int (*mbox_send)(struct cxl_mailbox *cxl_mbox, struct cxl_mbox_cmd *cmd);
 	enum cxl_features_capability feat_cap;
 };
Signature · 1 line
2.39.5
#8 lore raw

Re: [PATCH 3/7] cxl/pci: Lockless background synchronous polling

Quoted 13 lines · On Tue, 17 Jun 2025 12:36:07 -0700 Davidlohr Bueso <[email protected]> wrote:

For timesliced background commands we rely on holding the mbox_mutex throughout the duration of the operation. This causes other incoming commands to queue up behind, and interleaving executions while the background command is timesliced by the user.

However, in order to support the mbox request cancel background operation command, the lock will need to be available to actually perform the request. Semantically this allows other commands to many times be at the mercy of hardware returning the respective error. Potentially users would be exposed to changes in the form of errors instead of commands taking longer to run - but this is not foreseen as a problem.

Quoted 5 lines

In order to not loose sync with the hardware, introduce a mailbox atomic that blocks any other incoming bg operations while the driver is still polling (synchronously) on the current one.

Minor comments inline.

Jonathan

Quoted 85 lines
drivers/cxl/pci.c +7 -45
index 0d8f3605dc3f..3e6c231e9a8b 100644
@@ -312,23 +312,7 @@ static int __cxl_pci_mbox_send_cmd(struct cxl_mailbox *cxl_mbox,
 	mbox_cmd->return_code =
 		FIELD_GET(CXLDEV_MBOX_STATUS_RET_CODE_MASK, status_reg);
 
-	/*
-	 * Handle the background command in a synchronous manner.
-	 *
-	 * All other mailbox commands will serialize/queue on the mbox_mutex,
-	 * which we currently hold. Furthermore this also guarantees that
-	 * cxl_mbox_background_complete() checks are safe amongst each other,
-	 * in that no new bg operation can occur in between.
-	 *
-	 * Background operations are timesliced in accordance with the nature
-	 * of the command. In the event of timeout, the mailbox state is
-	 * indeterminate until the next successful command submission and the
-	 * driver can get back in sync with the hardware state.
-	 */
 	if (mbox_cmd->return_code == CXL_MBOX_CMD_RC_BACKGROUND) {
-		u64 bg_status_reg;
-		int i, timeout;
-
 		dev_dbg(dev, "Mailbox background operation (0x%04x) started\n",
 			mbox_cmd->opcode);
 
@@ -338,6 +322,8 @@ static int __cxl_pci_mbox_send_cmd(struct cxl_mailbox *cxl_mbox,
 		 */
 		if (mbox_cmd->opcode == CXL_MBOX_OP_SANITIZE ||
 		    mbox_cmd->opcode == CXL_MBOX_OP_ACTIVATE_FW) {
+			int timeout;
+
 			if (mds->bg.opcode)
 				return -EBUSY;
 
@@ -347,41 +333,17 @@ static int __cxl_pci_mbox_send_cmd(struct cxl_mailbox *cxl_mbox,
 			mds->bg.opcode = mbox_cmd->opcode;
 			schedule_delayed_work(&mds->bg.poll_dwork,
 					      timeout * HZ);
-			goto success;
+		} else {
+			/* pairs with release/acquire semantics */
+			WARN_ON_ONCE(atomic_xchg(&cxl_mbox->poll_bgop,
+						 mbox_cmd->opcode));
 		}
-
-		timeout = mbox_cmd->poll_interval_ms;
-		for (i = 0; i < mbox_cmd->poll_count; i++) {
-			if (rcuwait_wait_event_timeout(&cxl_mbox->mbox_wait,
-						       cxl_mbox_background_complete(cxlds),
-						       TASK_UNINTERRUPTIBLE,
-						       msecs_to_jiffies(timeout)) > 0)
-				break;
-		}
-
-		if (!cxl_mbox_background_complete(cxlds)) {
-			dev_err(dev, "timeout waiting for background (%d ms)\n",
-				timeout * mbox_cmd->poll_count);
-			return -ETIMEDOUT;
-		}
-
-		bg_status_reg = readq(cxlds->regs.mbox +
-				      CXLDEV_MBOX_BG_CMD_STATUS_OFFSET);
-		mbox_cmd->return_code =
-			FIELD_GET(CXLDEV_MBOX_BG_CMD_COMMAND_RC_MASK,
-				  bg_status_reg);
-		dev_dbg(dev,
-			"Mailbox background operation (0x%04x) completed\n",
-			mbox_cmd->opcode);
-	}
-
-	if (mbox_cmd->return_code != CXL_MBOX_CMD_RC_SUCCESS) {
+	} else if (mbox_cmd->return_code != CXL_MBOX_CMD_RC_SUCCESS) {
 		dev_dbg(dev, "Mailbox operation had an error: %s\n",
 			cxl_mbox_cmd_rc2str(mbox_cmd));
 		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_mbox_send(struct cxl_mailbox *cxl_mbox, · line 380

Quoted 8 lines
drivers/cxl/pci.c +3 -0
@@ -411,11 +373,71 @@ static int cxl_pci_mbox_send(struct cxl_mailbox *cxl_mbox,
 			     struct cxl_mbox_cmd *cmd)
 {
 	int rc;
+	struct cxl_dev_state *cxlds = mbox_to_cxlds(cxl_mbox);
+	struct device *dev = cxlds->dev;
+	struct cxl_memdev_state *mds = to_cxl_memdev_state(cxlds);
 
 	mutex_lock(&cxl_mbox->mbox_mutex);

Perhaps scoped_guard() appropriate here to avoid need to unlock explicitly in the error path.

drivers/cxl/pci.c static int cxl_pci_mbox_send(struct cxl_mailbox *cxl_mbox, · line 386

Quoted 6 lines
drivers/cxl/pci.c +6 -0
+	/*
+	 * Ensure cxl_mbox_background_complete() checks are safe amongst
+	 * each other: no new bg operation can occur in between while polling.
+	 */
+	if (cxl_is_background_cmd(mds, cmd->opcode)) {
+		if (atomic_read_acquire(&cxl_mbox->poll_bgop)) {

If you use scoped_guard() the indent will be getting a bit high, so maybe combine the previous two conditions.

Quoted 56 lines
drivers/cxl/pci.c +51 -0
+			mutex_unlock(&cxl_mbox->mbox_mutex);
+			return -EBUSY;
+		}
+	}
+
 	rc = __cxl_pci_mbox_send_cmd(cxl_mbox, cmd);
 	mutex_unlock(&cxl_mbox->mbox_mutex);
 
+	if (cmd->return_code != CXL_MBOX_CMD_RC_BACKGROUND)
+		return rc;
+
+	/*
+	 * Handle the background command in a synchronous manner. Background
+	 * operations are timesliced in accordance with the nature of the
+	 * command.
+	 */
+	if (cmd->opcode != CXL_MBOX_OP_SANITIZE &&
+	    cmd->opcode != CXL_MBOX_OP_ACTIVATE_FW) {
+		int i, timeout;
+		u64 bg_status_reg;
+
+		timeout = cmd->poll_interval_ms;
+		for (i = 0; i < cmd->poll_count; i++) {
+			if (rcuwait_wait_event_timeout(&cxl_mbox->mbox_wait,
+				       cxl_mbox_background_complete(cxlds),
+				       TASK_UNINTERRUPTIBLE,
+				       msecs_to_jiffies(timeout)) > 0)
+				break;
+		}
+
+		/*
+		 * In the event of timeout, the mailbox state is indeterminate
+		 * until the next successful command submission and the driver
+		 * can get back in sync with the hardware state.
+		 */
+		if (!cxl_mbox_background_complete(cxlds)) {
+			dev_err(dev, "timeout waiting for background (%d ms)\n",
+				timeout * cmd->poll_count);
+			rc = -ETIMEDOUT;
+			goto done;
+		}
+
+		bg_status_reg = readq(cxlds->regs.mbox +
+				      CXLDEV_MBOX_BG_CMD_STATUS_OFFSET);
+		cmd->return_code = FIELD_GET(CXLDEV_MBOX_BG_CMD_COMMAND_RC_MASK,
+					     bg_status_reg);
+		dev_dbg(dev,
+			"Mailbox background operation (0x%04x) completed\n",
+			cmd->opcode);
+done:
+		/* ensure clearing poll_bop is the last operation */
+		atomic_set_release(&cxl_mbox->poll_bgop, 0);
+	}
+
 	return rc;
 }
author maintainer
#9 lore raw

Re: [PATCH 3/7] cxl/pci: Lockless background synchronous polling

Quoted 11 lines · Jonathan Cameron
Quoted 8 lines · On Tue, 17 Jun 2025 12:36:07 -0700 Davidlohr Bueso <[email protected]> wrote:
drivers/cxl/pci.c +3 -0
@@ -411,11 +373,71 @@ static int cxl_pci_mbox_send(struct cxl_mailbox *cxl_mbox,
 			     struct cxl_mbox_cmd *cmd)
 {
 	int rc;
+	struct cxl_dev_state *cxlds = mbox_to_cxlds(cxl_mbox);
+	struct device *dev = cxlds->dev;
+	struct cxl_memdev_state *mds = to_cxl_memdev_state(cxlds);
 
Quoted 3 lines

drivers/cxl/pci.c static int cxl_pci_mbox_send(struct cxl_mailbox *cxl_mbox, · line 380

Quoted 1 line
drivers/cxl/pci.c +0 -0
 	mutex_lock(&cxl_mbox->mbox_mutex);

Perhaps scoped_guard() appropriate here to avoid need to unlock explicitly in the error path.

I'm not too fond of these, but yeah, I will convert to the new wrappers as most of the other lock calls in the cxl code base.

author maintainer
#10 lore raw

PATCH 4/7 cxl/mbox: Stronger cxl_mbox_background_complete() semantics

In reply to Davidlohr Bueso

Account for all situations where hw has no on-going background command. Current semantics do no change with this patch, but will serve for later supporting cancel requests. As such rename the helper accordingly.

---
 drivers/cxl/pci.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
drivers/cxl/pci.c +8 -8
index 3e6c231e9a8b..3648efcc7c89 100644
@@ -111,12 +111,12 @@ static int cxl_request_irq(struct cxl_dev_state *cxlds, int irq,
 					 dev_id);
 }
 
-static bool cxl_mbox_background_complete(struct cxl_dev_state *cxlds)
+static bool cxl_mbox_background_done(struct cxl_dev_state *cxlds)
 {
 	u64 reg;
 
-	reg = readq(cxlds->regs.mbox + CXLDEV_MBOX_BG_CMD_STATUS_OFFSET);
-	return FIELD_GET(CXLDEV_MBOX_BG_CMD_COMMAND_PCT_MASK, reg) == 100;
+	reg = readq(cxlds->regs.mbox + CXLDEV_MBOX_STATUS_OFFSET);
+	return FIELD_GET(CXLDEV_MBOX_STATUS_BG_CMD, reg) == 0;
 }
 
 static irqreturn_t cxl_pci_mbox_irq(int irq, void *id)
@@ -128,7 +128,7 @@ static irqreturn_t cxl_pci_mbox_irq(int irq, void *id)
 	struct cxl_mailbox *cxl_mbox = &cxlds->cxl_mbox;
 	struct cxl_memdev_state *mds = to_cxl_memdev_state(cxlds);
 
-	if (!cxl_mbox_background_complete(cxlds))
+	if (!cxl_mbox_background_done(cxlds))
 		return IRQ_NONE;
 
 	reg = readq(cxlds->regs.mbox + CXLDEV_MBOX_BG_CMD_STATUS_OFFSET);
@@ -158,7 +158,7 @@ static void cxl_mbox_bgpoll_work(struct work_struct *work)
 	struct cxl_mailbox *cxl_mbox = &cxlds->cxl_mbox;
 
 	mutex_lock(&cxl_mbox->mbox_mutex);
-	if (cxl_mbox_background_complete(cxlds)) {
+	if (cxl_mbox_background_done(cxlds)) {
 		mds->bg.poll_tmo_secs = 0;
 		if (mds->bg.opcode == CXL_MBOX_OP_SANITIZE)
 			sysfs_notify_dirent(mds->security.sanitize_node);
@@ -379,7 +379,7 @@ static int cxl_pci_mbox_send(struct cxl_mailbox *cxl_mbox,
 
 	mutex_lock(&cxl_mbox->mbox_mutex);
 	/*
-	 * Ensure cxl_mbox_background_complete() checks are safe amongst
+	 * Ensure cxl_mbox_background_done() checks are safe amongst
 	 * each other: no new bg operation can occur in between while polling.
 	 */
 	if (cxl_is_background_cmd(mds, cmd->opcode)) {
@@ -408,7 +408,7 @@ static int cxl_pci_mbox_send(struct cxl_mailbox *cxl_mbox,
 		timeout = cmd->poll_interval_ms;
 		for (i = 0; i < cmd->poll_count; i++) {
 			if (rcuwait_wait_event_timeout(&cxl_mbox->mbox_wait,
-				       cxl_mbox_background_complete(cxlds),
+				       cxl_mbox_background_done(cxlds),
 				       TASK_UNINTERRUPTIBLE,
 				       msecs_to_jiffies(timeout)) > 0)
 				break;
@@ -419,7 +419,7 @@ static int cxl_pci_mbox_send(struct cxl_mailbox *cxl_mbox,
 		 * until the next successful command submission and the driver
 		 * can get back in sync with the hardware state.
 		 */
-		if (!cxl_mbox_background_complete(cxlds)) {
+		if (!cxl_mbox_background_done(cxlds)) {
 			dev_err(dev, "timeout waiting for background (%d ms)\n",
 				timeout * cmd->poll_count);
 			rc = -ETIMEDOUT;
Signature · 1 line
2.39.5
Reviewed-by on v1 4/7
#11 lore raw

Re: [PATCH 4/7] cxl/mbox: Stronger cxl_mbox_background_complete() semantics

Quoted 2 lines · On Tue, 17 Jun 2025 12:36:08 -0700 Davidlohr Bueso <[email protected]> wrote:

Account for all situations where hw has no on-going background command. Current semantics do no change with this patch, but will

do not

Quoted 4 lines

serve for later supporting cancel requests. As such rename the helper accordingly.

author maintainer
#12 lore raw

PATCH 5/7 cxl/mbox: Allow userspace background commands

In reply to Davidlohr Bueso

CXL 3.1 introduced the ability to request that the current on-going background command be aborted. As such userspace may be allowed to monopolize the background operation capability if there are no other bg commands that want to run.

The policy here are as follows:

1. Give bg-capable commands from the kernel higher priority than those from userspace (albeit currently no command). Therefore a userspace on-going command can be canceled so a kernel one may run instead.

2. Userspace bg commands may cancel each other on demand. It is not up to the kernel to put any policies in place here.

3. Any previously synchronous polling timedout command that is still running may also be aborted by any command. This window is expected to be small.

The context of doing the cancellation request is the same as the new incoming command, and will always hold the mbox_mutex, guaranteeing that any successful cancel does not race with a third thread coming in and stealing the effort.

---
 drivers/cxl/core/mbox.c |  9 ++++--
 drivers/cxl/cxlmem.h    |  4 +++
 drivers/cxl/pci.c       | 72 +++++++++++++++++++++++++++++++++++++----
 include/cxl/mailbox.h   |  2 ++
 4 files changed, 78 insertions(+), 9 deletions(-)
drivers/cxl/core/mbox.c +7 -2
index aa1b04d8a78e..abb6bb77a43d 100644
@@ -382,6 +382,7 @@ static int cxl_mbox_cmd_ctor(struct cxl_mbox_cmd *mbox_cmd,
 	*mbox_cmd = (struct cxl_mbox_cmd) {
 		.opcode = opcode,
 		.size_in = in_size,
+		.user = true,
 	};
 
 	if (in_size) {
@@ -787,8 +788,12 @@ static void cxl_walk_cel(struct cxl_memdev_state *mds, size_t size, u8 *cel)
 		int enabled = 0;
 
 		if (cmd) {
-			set_bit(cmd->info.id, cxl_mbox->enabled_cmds);
-			enabled++;
+			/* only enable uapi bgcmds that may be canceled */
+			if (!(effect & CXL_CEL_BACKGROUND_OPERATION) ||
+			    (effect & CXL_CEL_REQ_ABORT_BACKGROUND_SUPPORTED)) {
+				set_bit(cmd->info.id, cxl_mbox->enabled_cmds);
+				enabled++;
+			}
 		}
 
 		enabled += check_features_opcodes(opcode, &ro_cmds,
drivers/cxl/cxlmem.h +4 -0
index 6fe42871fbf4..ebd7679143dd 100644
@@ -376,12 +376,14 @@ struct cxl_security_state {
  * @poll_dwork: polling background work item
  * @poll_tmo_secs: polling timeout
  * @opcode: async running background command
+ * @user: whether or not operation was initialized by userspace (uapi)
  */
 struct cxl_background_state {
 	DECLARE_BITMAP(enabled_cmds, CXL_BG_ENABLED_MAX);
 	struct delayed_work poll_dwork;
 	int poll_tmo_secs;
 	int opcode;
+	bool user;
 };
 
 /*
@@ -539,6 +541,7 @@ to_cxl_memdev_state(struct cxl_dev_state *cxlds)
 enum cxl_opcode {
 	CXL_MBOX_OP_INVALID		= 0x0000,
 	CXL_MBOX_OP_RAW			= CXL_MBOX_OP_INVALID,
+	CXL_MBOX_OP_REQUEST_ABORT_BG_OP = 0x0005,
 	CXL_MBOX_OP_GET_EVENT_RECORD	= 0x0100,
 	CXL_MBOX_OP_CLEAR_EVENT_RECORD	= 0x0101,
 	CXL_MBOX_OP_GET_EVT_INT_POLICY	= 0x0102,
@@ -606,6 +609,7 @@ struct cxl_mbox_get_supported_logs {
  * CXL 3.2 Section 8.2.10.5.2.1; Table 8-87
  */
 #define CXL_CEL_BACKGROUND_OPERATION BIT(6)
+#define CXL_CEL_REQ_ABORT_BACKGROUND_SUPPORTED BIT(8)
 
 struct cxl_cel_entry {
 	__le16 opcode;
drivers/cxl/pci.c +65 -7

131 lines not on this page · raw message

include/cxl/mailbox.h +2 -0
index f3c60158bfca..4a4119f49d3b 100644
@@ -19,6 +19,7 @@
  *            variable sized output commands, it tells the exact number of bytes
  *            written.
  * @min_out: (input) internal command output payload size validation
+ * @user: (input) Whether or not command was submitted from userspace (uapi).
  * @poll_count: (input) Number of timeouts to attempt.
  * @poll_interval_ms: (input) Time between mailbox background command polling
  *                    interval timeouts.
@@ -37,6 +38,7 @@ struct cxl_mbox_cmd {
 	size_t size_in;
 	size_t size_out;
 	size_t min_out;
+	bool user;
 	int poll_count;
 	int poll_interval_ms;
 	u16 return_code;
Signature · 1 line
2.39.5
#13 lore raw

Re: [PATCH 5/7] cxl/mbox: Allow userspace background commands

Quoted 37 lines · On Tue, 17 Jun 2025 12:36:09 -0700 Davidlohr Bueso <[email protected]> wrote:

CXL 3.1 introduced the ability to request that the current on-going background command be aborted. As such userspace may be allowed to monopolize the background operation capability if there are no other bg commands that want to run.

The policy here are as follows:

1. Give bg-capable commands from the kernel higher priority than those from userspace (albeit currently no command). Therefore a userspace on-going command can be canceled so a kernel one may run instead.

2. Userspace bg commands may cancel each other on demand. It is not up to the kernel to put any policies in place here.

3. Any previously synchronous polling timedout command that is still running may also be aborted by any command. This window is expected to be small.

The context of doing the cancellation request is the same as the new incoming command, and will always hold the mbox_mutex, guaranteeing that any successful cancel does not race with a third thread coming in and stealing the effort.

---
 drivers/cxl/core/mbox.c |  9 ++++--
 drivers/cxl/cxlmem.h    |  4 +++
 drivers/cxl/pci.c       | 72 +++++++++++++++++++++++++++++++++++++----
 include/cxl/mailbox.h   |  2 ++
 4 files changed, 78 insertions(+), 9 deletions(-)
drivers/cxl/pci.c +0 -0
index 3648efcc7c89..6ada0f502914 100644

drivers/cxl/pci.c static int __cxl_pci_mbox_send_cmd(struct cxl_mailbox *cxl_mbox, · line 267

Quoted 4 lines
drivers/cxl/pci.c +1 -0
@@ -263,6 +264,8 @@ static int __cxl_pci_mbox_send_cmd(struct cxl_mailbox *cxl_mbox,
 	 * commands during this window.
 	 */
 	switch (mds->bg.opcode) {
+	case 0:

I assume this is the race? Add a comment so we can figure this out in future.

Quoted 10 lines
drivers/cxl/pci.c +1 -0
+		break;
 	case CXL_MBOX_OP_SANITIZE:
 		/*
 		 * Ensure no new command comes in until so. Keep the

static int cxl_pci_mbox_send(struct cxl_mailbox *cxl_mbox,

drivers/cxl/pci.c +0 -0
 			     struct cxl_mbox_cmd *cmd)
 {

drivers/cxl/pci.c static int cxl_pci_mbox_send(struct cxl_mailbox *cxl_mbox, · line 440

Quoted 4 lines
drivers/cxl/pci.c +1 -0
@@ -381,9 +437,11 @@ static int cxl_pci_mbox_send(struct cxl_mailbox *cxl_mbox,
 	/*
 	 * Ensure cxl_mbox_background_done() checks are safe amongst
 	 * each other: no new bg operation can occur in between while polling.
+	 *

Stray change.

Quoted 1 line
drivers/cxl/pci.c +0 -0
 	 */
author maintainer
#14 lore raw

PATCH 6/7 cxl/mbox: Shout upon async bgcmd race

In reply to Davidlohr Bueso

Make the condition a warning, as the mailbox will be corrupted on such cases, which may trigger harder to debug issues down the line when simply returning a common error.

---
 drivers/cxl/pci.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/cxl/pci.c +1 -1
index 6ada0f502914..6404b01b64c9 100644
@@ -326,7 +326,7 @@ static int __cxl_pci_mbox_send_cmd(struct cxl_mailbox *cxl_mbox,
 		    mbox_cmd->user) {
 			int timeout;
 
-			if (mds->bg.opcode)
+			if (WARN_ON_ONCE(mds->bg.opcode))
 				return -EBUSY;
 
 			/* give first timeout a second */
Signature · 1 line
2.39.5
#15 lore raw

Re: [PATCH 6/7] cxl/mbox: Shout upon async bgcmd race

Quoted 3 lines · On Tue, 17 Jun 2025 12:36:10 -0700 Davidlohr Bueso <[email protected]> wrote:

Make the condition a warning, as the mailbox will be corrupted on such cases, which may trigger harder to debug issues down the line when simply returning a common error.

This is a little confusing. I assume it is meant to be impossible to hit this race?

Quoted 19 lines
---
 drivers/cxl/pci.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/cxl/pci.c +1 -1
index 6ada0f502914..6404b01b64c9 100644
@@ -326,7 +326,7 @@ static int __cxl_pci_mbox_send_cmd(struct cxl_mailbox *cxl_mbox,
 		    mbox_cmd->user) {
 			int timeout;
 
-			if (mds->bg.opcode)
+			if (WARN_ON_ONCE(mds->bg.opcode))
 				return -EBUSY;
 
 			/* give first timeout a second */
author maintainer
#16 lore raw

Re: [PATCH 6/7] cxl/mbox: Shout upon async bgcmd race

Quoted 9 lines · Jonathan Cameron
Quoted 3 lines · On Tue, 17 Jun 2025 12:36:10 -0700 Davidlohr Bueso <[email protected]> wrote:

Make the condition a warning, as the mailbox will be corrupted on such cases, which may trigger harder to debug issues down the line when simply returning a common error.

This is a little confusing. I assume it is meant to be impossible to hit this race?

Correct, this is just being overly cautious.

author maintainer
#18 lore raw

PATCH 7/7 cxl/mbox: Add Populate Log support

In reply to Davidlohr Bueso

From: Ravi Shankar <[email protected]>

Adding UAPI support for
CXL r3.1 8.2.9.5.5 Populate Log.

---
Apologies, this one had not gone out.

 drivers/cxl/core/mbox.c      | 1 +
 drivers/cxl/cxlmem.h         | 1 +
 include/uapi/linux/cxl_mem.h | 1 +
 3 files changed, 3 insertions(+)
drivers/cxl/core/mbox.c +1 -0
index abb6bb77a43d..ef737f9dacf2 100644
@@ -60,6 +60,7 @@ static struct cxl_mem_command cxl_mem_commands[CXL_MEM_COMMAND_ID_MAX] = {
 	CXL_CMD(GET_LOG_CAPS, 0x10, 0x4, 0),
 	CXL_CMD(CLEAR_LOG, 0x10, 0, 0),
 	CXL_CMD(GET_SUP_LOG_SUBLIST, 0x2, CXL_VARIABLE_PAYLOAD, 0),
+	CXL_CMD(POPULATE_LOG, 0x10, 0, 0),
 	CXL_CMD(SET_PARTITION_INFO, 0x0a, 0, 0),
 	CXL_CMD(SET_LSA, CXL_VARIABLE_PAYLOAD, 0, 0),
 	CXL_CMD(GET_ALERT_CONFIG, 0, 0x10, 0),
drivers/cxl/cxlmem.h +1 -0
index ebd7679143dd..56d5f9ea823a 100644
@@ -555,6 +555,7 @@ enum cxl_opcode {
 	CXL_MBOX_OP_GET_LOG		= 0x0401,
 	CXL_MBOX_OP_GET_LOG_CAPS	= 0x0402,
 	CXL_MBOX_OP_CLEAR_LOG           = 0x0403,
+	CXL_MBOX_OP_POPULATE_LOG        = 0x0404,
 	CXL_MBOX_OP_GET_SUP_LOG_SUBLIST = 0x0405,
 	CXL_MBOX_OP_GET_SUPPORTED_FEATURES	= 0x0500,
 	CXL_MBOX_OP_GET_FEATURE		= 0x0501,
include/uapi/linux/cxl_mem.h +1 -0
index c6c0fe27495d..ca7fbd1be873 100644
@@ -50,6 +50,7 @@
 	___C(GET_LOG_CAPS, "Get Log Capabilities"),			  \
 	___C(CLEAR_LOG, "Clear Log"),					  \
 	___C(GET_SUP_LOG_SUBLIST, "Get Supported Logs Sub-List"),	  \
+	___C(POPULATE_LOG, "Populate Log"),				  \
 	___C(MAX, "invalid / last command")
 
 #define ___C(a, b) CXL_MEM_COMMAND_ID_##a
Signature · 1 line
2.39.5