* [PATCH v2 00/10] cxl: Userspace background commands
@ 2026-12-02 15:43 Davidlohr Bueso
2026-12-02 15:43 ` [PATCH v2 01/10] cxl/pci: Reschedule the poller with the capped timeout Davidlohr Bueso
` (9 more replies)
0 siblings, 10 replies; 12+ messages in thread
From: Davidlohr Bueso @ 2026-12-02 15:43 UTC (permalink / raw)
To: dave.jiang
Cc: jic23, alison.schofield, ming.li, icheng, benjamin.cheatham,
unsung0317, linux-cxl, dave
Hi,
Changes from v1:
o Dropped handling Activate FW asynchronously. Nothing bounds how long
an activation may take, so there is no good answer to how long the
firmware upload should wait for its result. Deferred until the spec
has the device report it. Patch 4 is what is left of that patch, the
decoupling from security, and Jonathan's tag is dropped as such.
o New fixes in patches 1 and 2, and new patch 7.
o Fixed the cxl_test build. (Unsung)
o Converted to scoped_guard() and clarified comments. (Jonathan)
o Rebased on v7.4-rc5.
This is v2 of the mailbox background command series[1]. It lets
userspace make use of the background operation capability, with the
kernel aborting such an operation whenever it needs the device for one
of its own, and adds Populate Log as the first command to use it.
v1 has been sitting since June of last year. Unsung's work on online
firmware activation[2] ran into the same background handling, and the
discussion there is what brought this back.
o Patches 1-2 are fixes to the existing background handling, and can
go in on their own.
o Patch 3 tracks which commands the CEL has as background capable.
o Patch 4 moves the async handling out of the security state.
o Patches 5-6 no longer hold the mbox_mutex while polling for a
background command the kernel waits for.
o Patch 7 adds the return codes an abort request can come back with.
o Patches 8-9 allow userspace background commands.
o Patch 10 adds Populate Log.
Testing was done with cxl_test and the ndctl suite, and with the qemu
based tests at [3], which also carry the qemu fixes they rely on.
[1] https://lore.kernel.org/all/20250617193611.564668-1-dave@stgolabs.net/
[2] https://lore.kernel.org/all/20261125124358.3583223-1-unsung0317@gmail.com/
[3] https://github.com/davidlohr/cxl-bg-tests
Thanks!
Davidlohr Bueso (9):
cxl/pci: Reschedule the poller with the capped timeout
cxl/pci: Preserve Mailbox Control bits when ringing the doorbell
cxl/mbox: Track background commands from CEL
cxl/mbox: Decouple async background handling from security
cxl/pci: Lockless background synchronous polling
cxl/mbox: Stronger cxl_mbox_background_complete() semantics
cxl/mbox: Add missing mailbox return codes
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 | 45 ++++-
drivers/cxl/core/memdev.c | 7 +-
drivers/cxl/cxlmem.h | 53 +++++-
drivers/cxl/pci.c | 318 +++++++++++++++++++++++++----------
include/cxl/mailbox.h | 4 +
include/uapi/linux/cxl_mem.h | 1 +
tools/testing/cxl/test/mem.c | 10 +-
8 files changed, 334 insertions(+), 106 deletions(-)
base-commit: 693c2e04e3f1d4c59af236e1211b7b8cb165a790
--
2.39.5
^ permalink raw reply [flat|nested] 12+ messages in thread
* [PATCH v2 01/10] cxl/pci: Reschedule the poller with the capped timeout
2026-12-02 15:43 [PATCH v2 00/10] cxl: Userspace background commands Davidlohr Bueso
@ 2026-12-02 15:43 ` Davidlohr Bueso
2026-12-02 15:43 ` [PATCH v2 02/10] cxl/pci: Preserve Mailbox Control bits when ringing the doorbell Davidlohr Bueso
` (8 subsequent siblings)
9 siblings, 0 replies; 12+ messages in thread
From: Davidlohr Bueso @ 2026-12-02 15:43 UTC (permalink / raw)
To: dave.jiang
Cc: jic23, alison.schofield, ming.li, icheng, benjamin.cheatham,
unsung0317, linux-cxl, dave
cxl_mbox_sanitize_work() backs off by 10 seconds every time it finds
the sanitize still running, up to 15 minutes. The cap is only applied
to the stored poll_tmo_secs however, while the work is rescheduled with
the uncapped value. Once the limit is reached the poller runs every 910
seconds instead.
Compute the capped value first, and use it for both.
Fixes: 0c36b6ad436a ("cxl/mbox: Add sanitization handling machinery")
Signed-off-by: Davidlohr Bueso <dave@stgolabs.net>
---
drivers/cxl/pci.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/drivers/cxl/pci.c b/drivers/cxl/pci.c
index 8ce58917c32d..96c1e3381961 100644
--- a/drivers/cxl/pci.c
+++ b/drivers/cxl/pci.c
@@ -166,9 +166,9 @@ static void cxl_mbox_sanitize_work(struct work_struct *work)
dev_dbg(cxlds->dev, "Sanitization operation ended\n");
} else {
- int timeout = mds->security.poll_tmo_secs + 10;
+ int timeout = min(15 * 60, mds->security.poll_tmo_secs + 10);
- mds->security.poll_tmo_secs = min(15 * 60, timeout);
+ mds->security.poll_tmo_secs = timeout;
schedule_delayed_work(&mds->security.poll_dwork, timeout * HZ);
}
mutex_unlock(&cxl_mbox->mbox_mutex);
--
2.39.5
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [PATCH v2 02/10] cxl/pci: Preserve Mailbox Control bits when ringing the doorbell
2026-12-02 15:43 [PATCH v2 00/10] cxl: Userspace background commands Davidlohr Bueso
2026-12-02 15:43 ` [PATCH v2 01/10] cxl/pci: Reschedule the poller with the capped timeout Davidlohr Bueso
@ 2026-12-02 15:43 ` Davidlohr Bueso
2026-12-02 15:43 ` [PATCH v2 03/10] cxl/mbox: Track background commands from CEL Davidlohr Bueso
` (7 subsequent siblings)
9 siblings, 0 replies; 12+ messages in thread
From: Davidlohr Bueso @ 2026-12-02 15:43 UTC (permalink / raw)
To: dave.jiang
Cc: jic23, alison.schofield, ming.li, icheng, benjamin.cheatham,
unsung0317, linux-cxl, dave
cxl_pci_setup_mailbox() sets Background Command Complete Interrupt
(bit 2) in the Mailbox Control register once, at probe. The doorbell
is bit 0 of that same register, and __cxl_pci_mbox_send_cmd() rings it
by writing CXLDEV_MBOX_CTRL_DOORBELL alone, zeroing everything else.
The interrupt enable is RW, and only RO while the doorbell is set, so
the first command sent after probe clears it again.
The driver then asks for no interrupt on background completion.
Nothing breaks outright, as completion is still found by polling: the
synchronous wait checks every poll_interval_ms, and the sanitize poller
backs off up to 15 minutes between checks.
Ring the doorbell with a read-modify-write, as the enable itself is
already set. This also stops writing zeroes to the remaining bits of
the register, which are not the driver's to change.
Fixes: ccadf1310fb0 ("cxl/mbox: Add background cmd handling machinery")
Signed-off-by: Davidlohr Bueso <dave@stgolabs.net>
---
drivers/cxl/pci.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/drivers/cxl/pci.c b/drivers/cxl/pci.c
index 96c1e3381961..fd3db405a6d5 100644
--- a/drivers/cxl/pci.c
+++ b/drivers/cxl/pci.c
@@ -205,6 +205,7 @@ static int __cxl_pci_mbox_send_cmd(struct cxl_mailbox *cxl_mbox,
struct device *dev = cxlds->dev;
u64 cmd_reg, status_reg;
size_t out_len;
+ u32 ctrl;
int rc;
lockdep_assert_held(&cxl_mbox->mbox_mutex);
@@ -262,7 +263,8 @@ static int __cxl_pci_mbox_send_cmd(struct cxl_mailbox *cxl_mbox,
/* #4 */
dev_dbg(dev, "Sending command: 0x%04x\n", mbox_cmd->opcode);
- writel(CXLDEV_MBOX_CTRL_DOORBELL,
+ ctrl = readl(cxlds->regs.mbox + CXLDEV_MBOX_CTRL_OFFSET);
+ writel(ctrl | CXLDEV_MBOX_CTRL_DOORBELL,
cxlds->regs.mbox + CXLDEV_MBOX_CTRL_OFFSET);
/* #5 */
--
2.39.5
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [PATCH v2 03/10] cxl/mbox: Track background commands from CEL
2026-12-02 15:43 [PATCH v2 00/10] cxl: Userspace background commands Davidlohr Bueso
2026-12-02 15:43 ` [PATCH v2 01/10] cxl/pci: Reschedule the poller with the capped timeout Davidlohr Bueso
2026-12-02 15:43 ` [PATCH v2 02/10] cxl/pci: Preserve Mailbox Control bits when ringing the doorbell Davidlohr Bueso
@ 2026-12-02 15:43 ` Davidlohr Bueso
2026-12-02 15:43 ` [PATCH v2 04/10] cxl/mbox: Decouple async background handling from security Davidlohr Bueso
` (6 subsequent siblings)
9 siblings, 0 replies; 12+ messages in thread
From: Davidlohr Bueso @ 2026-12-02 15:43 UTC (permalink / raw)
To: dave.jiang
Cc: jic23, alison.schofield, ming.li, icheng, benjamin.cheatham,
unsung0317, linux-cxl, dave
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.
Signed-off-by: Davidlohr Bueso <dave@stgolabs.net>
---
drivers/cxl/core/mbox.c | 31 +++++++++++++++++++++++++++++++
drivers/cxl/cxlmem.h | 27 +++++++++++++++++++++++++++
2 files changed, 58 insertions(+)
diff --git a/drivers/cxl/core/mbox.c b/drivers/cxl/core/mbox.c
index 39485b6bcb97..41d25880173c 100644
--- a/drivers/cxl/core/mbox.c
+++ b/drivers/cxl/core/mbox.c
@@ -235,6 +235,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;
@@ -794,6 +819,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;
@@ -820,6 +846,11 @@ static void cxl_walk_cel(struct cxl_memdev_state *mds, size_t size, u8 *cel)
enabled++;
}
+ /* may already have been detected as enabled above */
+ if (effect & CXL_CEL_BACKGROUND_OPERATION)
+ if (cxl_set_bgcmd_enabled(&mds->bg, opcode))
+ enabled++;
+
dev_dbg(dev, "Opcode 0x%04x %s\n", opcode,
enabled ? "enabled" : "unsupported by driver");
}
diff --git a/drivers/cxl/cxlmem.h b/drivers/cxl/cxlmem.h
index 35268d1c387c..8b0df394d231 100644
--- a/drivers/cxl/cxlmem.h
+++ b/drivers/cxl/cxlmem.h
@@ -293,6 +293,16 @@ enum security_cmd_enabled_bits {
CXL_SEC_ENABLED_MAX
};
+/* Background-capable commands the device advertises in the CEL */
+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
*
@@ -410,6 +420,15 @@ 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);
+};
+
#define CXL_MAX_DC_PARTITIONS 8
static inline resource_size_t cxl_part_size(struct cxl_dev_state *cxlds,
@@ -448,6 +467,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
*
* See CXL 3.0 8.2.9.8.2 Capacity Configuration and Label Storage for
* details on capacity parameters.
@@ -468,6 +488,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;
};
static inline struct cxl_memdev_state *
@@ -547,6 +568,12 @@ struct cxl_mbox_get_supported_logs {
} __packed entry[];
} __packed;
+/*
+ * Command Effects Log (CEL)
+ * CXL 4.0 Section 8.2.10.5.2.1; Table 8-254
+ */
+#define CXL_CEL_BACKGROUND_OPERATION BIT(6)
+
struct cxl_cel_entry {
__le16 opcode;
__le16 effect;
--
2.39.5
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [PATCH v2 04/10] cxl/mbox: Decouple async background handling from security
2026-12-02 15:43 [PATCH v2 00/10] cxl: Userspace background commands Davidlohr Bueso
` (2 preceding siblings ...)
2026-12-02 15:43 ` [PATCH v2 03/10] cxl/mbox: Track background commands from CEL Davidlohr Bueso
@ 2026-12-02 15:43 ` Davidlohr Bueso
2026-12-02 15:43 ` [PATCH v2 05/10] cxl/pci: Lockless background synchronous polling Davidlohr Bueso
` (5 subsequent siblings)
9 siblings, 0 replies; 12+ messages in thread
From: Davidlohr Bueso @ 2026-12-02 15:43 UTC (permalink / raw)
To: dave.jiang
Cc: jic23, alison.schofield, ming.li, icheng, benjamin.cheatham,
unsung0317, linux-cxl, dave
The asynchronous handling of a background command, the state that
says one is running and the work that polls for its completion,
lives in the security state and is hardwired to Sanitize. Move it
to the background state and track the running operation by its
opcode, such that other commands can make use of it.
Now that the interrupt handler kicks the poller based on the tracked
operation instead of the sanitize sysfs node, the sanitize notifier
teardown no longer keeps the poller from being rescheduled. Cancel
it with a devm action of its own that runs after the interrupt is
gone.
Sanitize remains the only command handled asynchronously.
Signed-off-by: Davidlohr Bueso <dave@stgolabs.net>
---
drivers/cxl/core/hdm.c | 2 +-
drivers/cxl/core/memdev.c | 7 ++--
drivers/cxl/cxlmem.h | 12 +++----
drivers/cxl/pci.c | 64 +++++++++++++++++++++++-------------
tools/testing/cxl/test/mem.c | 10 +++---
5 files changed, 57 insertions(+), 38 deletions(-)
diff --git a/drivers/cxl/core/hdm.c b/drivers/cxl/core/hdm.c
index 6329eb3b9214..04576c08f22f 100644
--- a/drivers/cxl/core/hdm.c
+++ b/drivers/cxl/core/hdm.c
@@ -879,7 +879,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));
diff --git a/drivers/cxl/core/memdev.c b/drivers/cxl/core/memdev.c
index 787988e92965..002f56322b47 100644
--- a/drivers/cxl/core/memdev.c
+++ b/drivers/cxl/core/memdev.c
@@ -133,7 +133,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)
@@ -1301,15 +1301,14 @@ static void sanitize_teardown_notifier(void *data)
struct kernfs_node *state;
/*
- * Prevent new irq triggered invocations of the workqueue and
- * flush inflight invocations.
+ * Stop the poller from notifying and flush inflight invocations.
*/
mutex_lock(&cxl_mbox->mbox_mutex);
state = mds->security.sanitize_node;
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);
}
diff --git a/drivers/cxl/cxlmem.h b/drivers/cxl/cxlmem.h
index 8b0df394d231..e1d028d9af1b 100644
--- a/drivers/cxl/cxlmem.h
+++ b/drivers/cxl/cxlmem.h
@@ -406,17 +406,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;
};
@@ -424,9 +418,15 @@ 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;
};
#define CXL_MAX_DC_PARTITIONS 8
diff --git a/drivers/cxl/pci.c b/drivers/cxl/pci.c
index fd3db405a6d5..1bc28ad17200 100644
--- a/drivers/cxl/pci.c
+++ b/drivers/cxl/pci.c
@@ -136,8 +136,9 @@ static irqreturn_t cxl_pci_mbox_irq(int irq, void *id)
opcode = FIELD_GET(CXLDEV_MBOX_BG_CMD_COMMAND_OPCODE_MASK, reg);
if (opcode == CXL_MBOX_OP_SANITIZE) {
mutex_lock(&cxl_mbox->mbox_mutex);
- if (mds->security.sanitize_node)
- mod_delayed_work(system_percpu_wq, &mds->security.poll_dwork, 0);
+ if (mds->bg.opcode)
+ mod_delayed_work(system_percpu_wq,
+ &mds->bg.poll_dwork, 0);
mutex_unlock(&cxl_mbox->mbox_mutex);
} else {
/* short-circuit the wait in __cxl_pci_mbox_send_cmd() */
@@ -150,26 +151,29 @@ static irqreturn_t cxl_pci_mbox_irq(int irq, void *id)
/*
* Sanitization operation polling mode.
*/
-static void cxl_mbox_sanitize_work(struct work_struct *work)
+static void cxl_mbox_bgpoll_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_dev_state *cxlds = &mds->cxlds;
struct cxl_mailbox *cxl_mbox = &cxlds->cxl_mbox;
mutex_lock(&cxl_mbox->mbox_mutex);
if (cxl_mbox_background_complete(cxlds)) {
- mds->security.poll_tmo_secs = 0;
- if (mds->security.sanitize_node)
+ mds->bg.poll_tmo_secs = 0;
+ if (mds->bg.opcode == CXL_MBOX_OP_SANITIZE &&
+ mds->security.sanitize_node)
sysfs_notify_dirent(mds->security.sanitize_node);
- mds->security.sanitize_active = false;
- dev_dbg(cxlds->dev, "Sanitization operation ended\n");
+ dev_dbg(cxlds->dev,
+ "Mailbox background operation (0x%04x) ended\n",
+ mds->bg.opcode);
+ mds->bg.opcode = 0;
} else {
- int timeout = min(15 * 60, mds->security.poll_tmo_secs + 10);
+ int timeout = min(15 * 60, mds->bg.poll_tmo_secs + 10);
- mds->security.poll_tmo_secs = timeout;
- schedule_delayed_work(&mds->security.poll_dwork, timeout * HZ);
+ mds->bg.poll_tmo_secs = timeout;
+ schedule_delayed_work(&mds->bg.poll_dwork, timeout * HZ);
}
mutex_unlock(&cxl_mbox->mbox_mutex);
}
@@ -242,7 +246,7 @@ static int __cxl_pci_mbox_send_cmd(struct cxl_mailbox *cxl_mbox,
* not be in sync. Ensure no new command comes in until so. Keep the
* hardware semantics and only allow device health status.
*/
- if (mds->security.poll_tmo_secs > 0) {
+ if (mds->bg.opcode == CXL_MBOX_OP_SANITIZE) {
if (mbox_cmd->opcode != CXL_MBOX_OP_GET_HEALTH_INFO)
return -EBUSY;
}
@@ -298,28 +302,27 @@ static int __cxl_pci_mbox_send_cmd(struct cxl_mailbox *cxl_mbox,
u64 bg_status_reg;
int i, timeout;
+ dev_dbg(dev, "Mailbox background operation (0x%04x) started\n",
+ mbox_cmd->opcode);
+
/*
* Sanitization 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 (mds->security.sanitize_active)
+ if (mds->bg.opcode)
return -EBUSY;
/* give first timeout a second */
timeout = 1;
- mds->security.poll_tmo_secs = timeout;
- mds->security.sanitize_active = true;
- schedule_delayed_work(&mds->security.poll_dwork,
+ mds->bg.poll_tmo_secs = timeout;
+ mds->bg.opcode = mbox_cmd->opcode;
+ schedule_delayed_work(&mds->bg.poll_dwork,
timeout * HZ);
- dev_dbg(dev, "Sanitization 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(&cxl_mbox->mbox_wait,
@@ -389,6 +392,13 @@ static int cxl_pci_mbox_send(struct cxl_mailbox *cxl_mbox,
return rc;
}
+static void cxl_pci_bg_teardown(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;
@@ -396,7 +406,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;
@@ -445,7 +455,17 @@ 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->security.poll_dwork, cxl_mbox_sanitize_work);
+ INIT_DELAYED_WORK(&mds->bg.poll_dwork, cxl_mbox_bgpoll_work);
+
+ /*
+ * The interrupt handler kicks the poller for as long as an
+ * operation is tracked, whatever the sanitize notifier is up to.
+ * Registered ahead of the interrupt, so that by the time the
+ * poller is cancelled nothing can kick it.
+ */
+ rc = devm_add_action_or_reset(dev, cxl_pci_bg_teardown, mds);
+ if (rc)
+ return rc;
/* background command interrupts are optional */
if (!(cap & CXLDEV_MBOX_CAP_BG_CMD_IRQ) || !irq_avail)
diff --git a/tools/testing/cxl/test/mem.c b/tools/testing/cxl/test/mem.c
index c740372bdaaa..6d01fe9c9453 100644
--- a/tools/testing/cxl/test/mem.c
+++ b/tools/testing/cxl/test/mem.c
@@ -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.39.5
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [PATCH v2 05/10] cxl/pci: Lockless background synchronous polling
2026-12-02 15:43 [PATCH v2 00/10] cxl: Userspace background commands Davidlohr Bueso
` (3 preceding siblings ...)
2026-12-02 15:43 ` [PATCH v2 04/10] cxl/mbox: Decouple async background handling from security Davidlohr Bueso
@ 2026-12-02 15:43 ` Davidlohr Bueso
2026-12-02 15:44 ` [PATCH v2 06/10] cxl/mbox: Stronger cxl_mbox_background_complete() semantics Davidlohr Bueso
` (4 subsequent siblings)
9 siblings, 0 replies; 12+ messages in thread
From: Davidlohr Bueso @ 2026-12-02 15:43 UTC (permalink / raw)
To: dave.jiang
Cc: jic23, alison.schofield, ming.li, icheng, benjamin.cheatham,
unsung0317, linux-cxl, dave
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.
Signed-off-by: Davidlohr Bueso <dave@stgolabs.net>
---
drivers/cxl/core/mbox.c | 1 +
drivers/cxl/pci.c | 136 +++++++++++++++++++++++++---------------
include/cxl/mailbox.h | 2 +
3 files changed, 90 insertions(+), 49 deletions(-)
diff --git a/drivers/cxl/core/mbox.c b/drivers/cxl/core/mbox.c
index 41d25880173c..32af282d0fb6 100644
--- a/drivers/cxl/core/mbox.c
+++ b/drivers/cxl/core/mbox.c
@@ -1792,6 +1792,7 @@ int cxl_mailbox_init(struct cxl_mailbox *cxl_mbox, struct device *host)
cxl_mbox->host = host;
mutex_init(&cxl_mbox->mbox_mutex);
mutex_init(&cxl_mbox->feat_mutex);
+ atomic_set(&cxl_mbox->poll_bgop, 0);
rcuwait_init(&cxl_mbox->mbox_wait);
return 0;
diff --git a/drivers/cxl/pci.c b/drivers/cxl/pci.c
index 1bc28ad17200..8cb9c5e48489 100644
--- a/drivers/cxl/pci.c
+++ b/drivers/cxl/pci.c
@@ -141,7 +141,7 @@ static irqreturn_t cxl_pci_mbox_irq(int irq, void *id)
&mds->bg.poll_dwork, 0);
mutex_unlock(&cxl_mbox->mbox_mutex);
} else {
- /* short-circuit the wait in __cxl_pci_mbox_send_cmd() */
+ /* short-circuit the wait in cxl_pci_mbox_send() */
rcuwait_wake_up(&cxl_mbox->mbox_wait);
}
@@ -178,6 +178,25 @@ static void cxl_mbox_bgpoll_work(struct work_struct *work)
mutex_unlock(&cxl_mbox->mbox_mutex);
}
+static inline bool cxl_is_background_cmd(struct cxl_memdev_state *mds,
+ u16 opcode)
+{
+ switch (opcode) {
+ case CXL_MBOX_OP_TRANSFER_FW:
+ return test_bit(CXL_BG_ENABLED_TRANSFER_FW, mds->bg.enabled_cmds);
+ case CXL_MBOX_OP_ACTIVATE_FW:
+ return test_bit(CXL_BG_ENABLED_ACTIVATE_FW, mds->bg.enabled_cmds);
+ case CXL_MBOX_OP_DO_MAINTENANCE:
+ return test_bit(CXL_BG_ENABLED_DO_MAINTENANCE, mds->bg.enabled_cmds);
+ case CXL_MBOX_OP_SCAN_MEDIA:
+ return test_bit(CXL_BG_ENABLED_SCAN_MEDIA, mds->bg.enabled_cmds);
+ case CXL_MBOX_OP_SANITIZE:
+ return test_bit(CXL_BG_ENABLED_SANITIZE, mds->bg.enabled_cmds);
+ default:
+ return false;
+ }
+}
+
/**
* __cxl_pci_mbox_send_cmd() - Execute a mailbox command
* @cxl_mbox: CXL mailbox context
@@ -285,23 +304,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);
@@ -311,6 +314,8 @@ static int __cxl_pci_mbox_send_cmd(struct cxl_mailbox *cxl_mbox,
* and allow userspace to poll(2) for completion.
*/
if (mbox_cmd->opcode == CXL_MBOX_OP_SANITIZE) {
+ int timeout;
+
if (mds->bg.opcode)
return -EBUSY;
@@ -320,41 +325,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;
- }
-
- 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;
+ } else {
+ /* pairs with release/acquire semantics */
+ WARN_ON_ONCE(atomic_xchg(&cxl_mbox->poll_bgop,
+ mbox_cmd->opcode));
}
-
- 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);
@@ -384,10 +365,67 @@ 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);
- rc = __cxl_pci_mbox_send_cmd(cxl_mbox, cmd);
- mutex_unlock(&cxl_mbox->mbox_mutex);
+ scoped_guard(mutex, &cxl_mbox->mbox_mutex) {
+ /*
+ * 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) &&
+ atomic_read_acquire(&cxl_mbox->poll_bgop))
+ return -EBUSY;
+
+ rc = __cxl_pci_mbox_send_cmd(cxl_mbox, cmd);
+ }
+
+ 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) {
+ 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_bgop is the last operation */
+ atomic_set_release(&cxl_mbox->poll_bgop, 0);
+ }
return rc;
}
diff --git a/include/cxl/mailbox.h b/include/cxl/mailbox.h
index d008b9db07aa..691c2ae21ef5 100644
--- a/include/cxl/mailbox.h
+++ b/include/cxl/mailbox.h
@@ -52,6 +52,7 @@ struct cxl_mbox_cmd {
* @mbox_mutex: mutex protects device mailbox and firmware
* @feat_mutex: serializes multi-part Get/Set Feature transfers
* @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
*/
@@ -63,6 +64,7 @@ struct cxl_mailbox {
struct mutex mbox_mutex; /* lock to protect mailbox context */
struct mutex feat_mutex;
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;
};
--
2.39.5
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [PATCH v2 06/10] cxl/mbox: Stronger cxl_mbox_background_complete() semantics
2026-12-02 15:43 [PATCH v2 00/10] cxl: Userspace background commands Davidlohr Bueso
` (4 preceding siblings ...)
2026-12-02 15:43 ` [PATCH v2 05/10] cxl/pci: Lockless background synchronous polling Davidlohr Bueso
@ 2026-12-02 15:44 ` Davidlohr Bueso
2026-12-02 15:44 ` [PATCH v2 07/10] cxl/mbox: Add missing mailbox return codes Davidlohr Bueso
` (3 subsequent siblings)
9 siblings, 0 replies; 12+ messages in thread
From: Davidlohr Bueso @ 2026-12-02 15:44 UTC (permalink / raw)
To: dave.jiang
Cc: jic23, alison.schofield, ming.li, icheng, benjamin.cheatham,
unsung0317, linux-cxl, dave
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.
Reviewed-by: Jonathan Cameron <jic23@kernel.org>
Signed-off-by: Davidlohr Bueso <dave@stgolabs.net>
---
drivers/cxl/pci.c | 16 ++++++++--------
1 file changed, 8 insertions(+), 8 deletions(-)
diff --git a/drivers/cxl/pci.c b/drivers/cxl/pci.c
index 8cb9c5e48489..ef4853296e86 100644
--- a/drivers/cxl/pci.c
+++ b/drivers/cxl/pci.c
@@ -112,12 +112,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)
@@ -129,7 +129,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);
@@ -159,7 +159,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 &&
mds->security.sanitize_node)
@@ -371,7 +371,7 @@ static int cxl_pci_mbox_send(struct cxl_mailbox *cxl_mbox,
scoped_guard(mutex, &cxl_mbox->mbox_mutex) {
/*
- * Ensure cxl_mbox_background_complete() checks are safe
+ * Ensure cxl_mbox_background_done() checks are safe
* amongst each other: no new bg operation can occur in
* between while polling.
*/
@@ -397,7 +397,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;
@@ -408,7 +408,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;
--
2.39.5
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [PATCH v2 07/10] cxl/mbox: Add missing mailbox return codes
2026-12-02 15:43 [PATCH v2 00/10] cxl: Userspace background commands Davidlohr Bueso
` (5 preceding siblings ...)
2026-12-02 15:44 ` [PATCH v2 06/10] cxl/mbox: Stronger cxl_mbox_background_complete() semantics Davidlohr Bueso
@ 2026-12-02 15:44 ` Davidlohr Bueso
2026-12-02 15:44 ` [PATCH v2 08/10] cxl/mbox: Allow userspace background commands Davidlohr Bueso
` (2 subsequent siblings)
9 siblings, 0 replies; 12+ messages in thread
From: Davidlohr Bueso @ 2026-12-02 15:44 UTC (permalink / raw)
To: dave.jiang
Cc: jic23, alison.schofield, ming.li, icheng, benjamin.cheatham,
unsung0317, linux-cxl, dave
The mailbox return code table ends at Invalid Extent List, yet the
spec has since defined two more: Transfer Out of Order and Request
Abort Not Supported by Background Operation. The table is looked up
with whatever the device returns, unchecked, so either of them reads
beyond it.
The second one is about to become an expected answer, as the driver
will send Request Abort Background Operation without knowing whether
the running operation supports it. Add both codes, and treat any other
unknown one as a generic failure.
Signed-off-by: Davidlohr Bueso <dave@stgolabs.net>
---
drivers/cxl/cxlmem.h | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/drivers/cxl/cxlmem.h b/drivers/cxl/cxlmem.h
index e1d028d9af1b..1638798bceac 100644
--- a/drivers/cxl/cxlmem.h
+++ b/drivers/cxl/cxlmem.h
@@ -157,7 +157,7 @@ static inline struct cxl_ep *cxl_ep_load(struct cxl_port *port,
}
/*
- * Per CXL 3.0 Section 8.2.8.4.5.1
+ * Per CXL 4.0 Section 8.2.9.4.5.1
*/
#define CMD_CMD_RC_TABLE \
C(SUCCESS, 0, NULL), \
@@ -191,6 +191,8 @@ static inline struct cxl_ep *cxl_ep_load(struct cxl_port *port,
C(FEATURETRANSFEROOO, -ENXIO, "feature transfer out of order"), \
C(RESOURCEEXHAUSTED, -ENXIO, "resources are exhausted"), \
C(EXTLIST, -ENXIO, "invalid Extent List"), \
+ C(TRANSFEROOO, -ENXIO, "transfer out of order"), \
+ C(ABORTNOTSUP, -ENXIO, "background cmd does not support abort"), \
#undef C
#define C(a, b, c) CXL_MBOX_CMD_RC_##a
@@ -208,11 +210,15 @@ struct cxl_mbox_cmd_rc cxl_mbox_cmd_rctable[] ={ CMD_CMD_RC_TABLE };
static inline const char *cxl_mbox_cmd_rc2str(struct cxl_mbox_cmd *mbox_cmd)
{
+ if (mbox_cmd->return_code >= ARRAY_SIZE(cxl_mbox_cmd_rctable))
+ return "unknown return code";
return cxl_mbox_cmd_rctable[mbox_cmd->return_code].desc;
}
static inline int cxl_mbox_cmd_rc2errno(struct cxl_mbox_cmd *mbox_cmd)
{
+ if (mbox_cmd->return_code >= ARRAY_SIZE(cxl_mbox_cmd_rctable))
+ return -ENXIO;
return cxl_mbox_cmd_rctable[mbox_cmd->return_code].err;
}
--
2.39.5
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [PATCH v2 08/10] cxl/mbox: Allow userspace background commands
2026-12-02 15:43 [PATCH v2 00/10] cxl: Userspace background commands Davidlohr Bueso
` (6 preceding siblings ...)
2026-12-02 15:44 ` [PATCH v2 07/10] cxl/mbox: Add missing mailbox return codes Davidlohr Bueso
@ 2026-12-02 15:44 ` Davidlohr Bueso
2026-12-02 16:10 ` sashiko-bot
2026-12-02 15:44 ` [PATCH v2 09/10] cxl/mbox: Shout upon async bgcmd race Davidlohr Bueso
2026-12-02 15:44 ` [PATCH v2 10/10] cxl/mbox: Add Populate Log support Davidlohr Bueso
9 siblings, 1 reply; 12+ messages in thread
From: Davidlohr Bueso @ 2026-12-02 15:44 UTC (permalink / raw)
To: dave.jiang
Cc: jic23, alison.schofield, ming.li, icheng, benjamin.cheatham,
unsung0317, linux-cxl, dave
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. A kernel background command is never aborted. This includes one
that timed out while being polled for synchronously and is still
running: it is no longer tracked, and incoming bg-capable commands
get -EBUSY until hardware is done with it.
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.
Signed-off-by: Davidlohr Bueso <dave@stgolabs.net>
---
drivers/cxl/core/mbox.c | 9 ++-
drivers/cxl/cxlmem.h | 4 ++
drivers/cxl/pci.c | 126 +++++++++++++++++++++++++++++++++-------
include/cxl/mailbox.h | 2 +
4 files changed, 117 insertions(+), 24 deletions(-)
diff --git a/drivers/cxl/core/mbox.c b/drivers/cxl/core/mbox.c
index 32af282d0fb6..017e1b9319a3 100644
--- a/drivers/cxl/core/mbox.c
+++ b/drivers/cxl/core/mbox.c
@@ -421,6 +421,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) {
@@ -824,8 +825,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,
diff --git a/drivers/cxl/cxlmem.h b/drivers/cxl/cxlmem.h
index 1638798bceac..70037e526e01 100644
--- a/drivers/cxl/cxlmem.h
+++ b/drivers/cxl/cxlmem.h
@@ -427,12 +427,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;
};
#define CXL_MAX_DC_PARTITIONS 8
@@ -508,6 +510,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,
@@ -579,6 +582,7 @@ struct cxl_mbox_get_supported_logs {
* CXL 4.0 Section 8.2.10.5.2.1; Table 8-254
*/
#define CXL_CEL_BACKGROUND_OPERATION BIT(6)
+#define CXL_CEL_REQ_ABORT_BACKGROUND_SUPPORTED BIT(8)
struct cxl_cel_entry {
__le16 opcode;
diff --git a/drivers/cxl/pci.c b/drivers/cxl/pci.c
index ef4853296e86..1cacc9e90a31 100644
--- a/drivers/cxl/pci.c
+++ b/drivers/cxl/pci.c
@@ -134,7 +134,7 @@ 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 (opcode == CXL_MBOX_OP_SANITIZE || mds->bg.user) {
mutex_lock(&cxl_mbox->mbox_mutex);
if (mds->bg.opcode)
mod_delayed_work(system_percpu_wq,
@@ -149,7 +149,28 @@ static irqreturn_t cxl_pci_mbox_irq(int irq, void *id)
}
/*
- * Sanitization operation polling mode.
+ * Hardware is done with the async background operation, one way or
+ * another: let go of it.
+ */
+static void cxl_mbox_bg_end(struct cxl_memdev_state *mds)
+{
+ struct cxl_dev_state *cxlds = &mds->cxlds;
+
+ lockdep_assert_held(&cxlds->cxl_mbox.mbox_mutex);
+
+ mds->bg.poll_tmo_secs = 0;
+ if (mds->bg.opcode == CXL_MBOX_OP_SANITIZE &&
+ mds->security.sanitize_node)
+ sysfs_notify_dirent(mds->security.sanitize_node);
+
+ dev_dbg(cxlds->dev, "Mailbox background operation (0x%04x) ended\n",
+ mds->bg.opcode);
+ mds->bg.opcode = 0;
+ mds->bg.user = false;
+}
+
+/*
+ * Async background operation polling mode.
*/
static void cxl_mbox_bgpoll_work(struct work_struct *work)
{
@@ -159,16 +180,10 @@ 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_done(cxlds)) {
- mds->bg.poll_tmo_secs = 0;
- if (mds->bg.opcode == CXL_MBOX_OP_SANITIZE &&
- mds->security.sanitize_node)
- sysfs_notify_dirent(mds->security.sanitize_node);
-
- dev_dbg(cxlds->dev,
- "Mailbox background operation (0x%04x) ended\n",
- mds->bg.opcode);
- mds->bg.opcode = 0;
+ if (!mds->bg.opcode) {
+ /* cxl_try_to_cancel_background() got here first */
+ } else if (cxl_mbox_background_done(cxlds)) {
+ cxl_mbox_bg_end(mds);
} else {
int timeout = min(15 * 60, mds->bg.poll_tmo_secs + 10);
@@ -261,13 +276,26 @@ static int __cxl_pci_mbox_send_cmd(struct cxl_mailbox *cxl_mbox,
}
/*
- * 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.
+ * With async bg polling, hardware might be done and the poller still
+ * not be in sync. At the very least, prevent any incoming bg-capable
+ * commands during this window.
*/
- if (mds->bg.opcode == CXL_MBOX_OP_SANITIZE) {
+ switch (mds->bg.opcode) {
+ case 0:
+ /* no async background operation to stay in sync with */
+ break;
+ case CXL_MBOX_OP_SANITIZE:
+ /*
+ * Ensure no new command comes in until so. Keep the
+ * hardware semantics and only allow device health status.
+ */
if (mbox_cmd->opcode != CXL_MBOX_OP_GET_HEALTH_INFO)
return -EBUSY;
+ break;
+ default:
+ if (cxl_is_background_cmd(mds, mbox_cmd->opcode))
+ return -EBUSY;
+ break;
}
cmd_reg = FIELD_PREP(CXLDEV_MBOX_CMD_COMMAND_OPCODE_MASK,
@@ -309,11 +337,11 @@ static int __cxl_pci_mbox_send_cmd(struct cxl_mailbox *cxl_mbox,
mbox_cmd->opcode);
/*
- * Sanitization is a special case which monopolizes the device
- * and cannot be timesliced. Handle asynchronously instead,
- * and allow userspace to poll(2) for completion.
+ * Special cases which monopolize the device and cannot be
+ * timesliced. Handle asynchronously instead.
*/
- if (mbox_cmd->opcode == CXL_MBOX_OP_SANITIZE) {
+ if (mbox_cmd->opcode == CXL_MBOX_OP_SANITIZE ||
+ mbox_cmd->user) {
int timeout;
if (mds->bg.opcode)
@@ -323,6 +351,7 @@ static int __cxl_pci_mbox_send_cmd(struct cxl_mailbox *cxl_mbox,
timeout = 1;
mds->bg.poll_tmo_secs = timeout;
mds->bg.opcode = mbox_cmd->opcode;
+ mds->bg.user = mbox_cmd->user;
schedule_delayed_work(&mds->bg.poll_dwork,
timeout * HZ);
} else {
@@ -361,6 +390,58 @@ static int __cxl_pci_mbox_send_cmd(struct cxl_mailbox *cxl_mbox,
return 0;
}
+/*
+ * Attempt to cancel an on-going background command. Return true guarantees
+ * that no such operations are running and any leftover context is gone.
+ */
+static bool cxl_try_to_cancel_background(struct cxl_mailbox *cxl_mbox)
+{
+ int rc;
+ struct cxl_dev_state *cxlds = mbox_to_cxlds(cxl_mbox);
+ struct cxl_memdev_state *mds = to_cxl_memdev_state(cxlds);
+ struct device *dev = cxlds->dev;
+ struct cxl_mbox_cmd cmd = {
+ .opcode = CXL_MBOX_OP_REQUEST_ABORT_BG_OP,
+ };
+
+ lockdep_assert_held(&cxl_mbox->mbox_mutex);
+
+ /* everything is finished, new bgcmd may proceed */
+ if (cxl_mbox_background_done(cxlds))
+ goto done;
+ if (!mds->bg.user) {
+ /*
+ * Policy is around userpace being abortable:
+ * incoming user bgcmd -> abort on-going user bgcmd
+ * incoming kernel bgcmd -> abort on-going user bgcmd
+ */
+ return false;
+ }
+
+ rc = __cxl_pci_mbox_send_cmd(cxl_mbox, &cmd);
+ if (rc) {
+ dev_dbg(dev, "Failed to send abort request : %d\n", rc);
+ return false;
+ }
+
+ if (!cxl_mbox_background_done(cxlds))
+ return false;
+
+ dev_dbg(cxlds->dev, "Mailbox background operation aborted\n");
+done:
+ if (mds->bg.opcode) {
+ /*
+ * Cleanup on behalf of the poller. It may well be blocked
+ * on the mbox_mutex right now and so cannot be waited for,
+ * it copes with finding nothing left to do.
+ */
+ cancel_delayed_work(&mds->bg.poll_dwork);
+ cxl_mbox_bg_end(mds);
+ }
+
+ return true;
+}
+
static int cxl_pci_mbox_send(struct cxl_mailbox *cxl_mbox,
struct cxl_mbox_cmd *cmd)
{
@@ -376,7 +457,8 @@ static int cxl_pci_mbox_send(struct cxl_mailbox *cxl_mbox,
* between while polling.
*/
if (cxl_is_background_cmd(mds, cmd->opcode) &&
- atomic_read_acquire(&cxl_mbox->poll_bgop))
+ (atomic_read_acquire(&cxl_mbox->poll_bgop) ||
+ !cxl_try_to_cancel_background(cxl_mbox)))
return -EBUSY;
rc = __cxl_pci_mbox_send_cmd(cxl_mbox, cmd);
@@ -390,7 +472,7 @@ static int cxl_pci_mbox_send(struct cxl_mailbox *cxl_mbox,
* operations are timesliced in accordance with the nature of the
* command.
*/
- if (cmd->opcode != CXL_MBOX_OP_SANITIZE) {
+ if (cmd->opcode != CXL_MBOX_OP_SANITIZE && !cmd->user) {
int i, timeout;
u64 bg_status_reg;
diff --git a/include/cxl/mailbox.h b/include/cxl/mailbox.h
index 691c2ae21ef5..6bc2465a6b76 100644
--- a/include/cxl/mailbox.h
+++ b/include/cxl/mailbox.h
@@ -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;
--
2.39.5
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [PATCH v2 09/10] cxl/mbox: Shout upon async bgcmd race
2026-12-02 15:43 [PATCH v2 00/10] cxl: Userspace background commands Davidlohr Bueso
` (7 preceding siblings ...)
2026-12-02 15:44 ` [PATCH v2 08/10] cxl/mbox: Allow userspace background commands Davidlohr Bueso
@ 2026-12-02 15:44 ` Davidlohr Bueso
2026-12-02 15:44 ` [PATCH v2 10/10] cxl/mbox: Add Populate Log support Davidlohr Bueso
9 siblings, 0 replies; 12+ messages in thread
From: Davidlohr Bueso @ 2026-12-02 15:44 UTC (permalink / raw)
To: dave.jiang
Cc: jic23, alison.schofield, ming.li, icheng, benjamin.cheatham,
unsung0317, linux-cxl, dave
By the time a command answers Background Command Started, any
operation the driver was tracking has been ended, either by the
poller or by cxl_try_to_cancel_background() on behalf of the new
command. Finding one still tracked means the driver's view of the
background operation no longer matches what hardware is running.
This is not expected to happen. Make the condition a warning
instead of quietly returning an error, as it is the kind of thing
that is harder to debug further down the line.
Signed-off-by: Davidlohr Bueso <dave@stgolabs.net>
---
drivers/cxl/pci.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/drivers/cxl/pci.c b/drivers/cxl/pci.c
index 1cacc9e90a31..a036f327ec7d 100644
--- a/drivers/cxl/pci.c
+++ b/drivers/cxl/pci.c
@@ -344,7 +344,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 */
--
2.39.5
^ permalink raw reply related [flat|nested] 12+ messages in thread
* [PATCH v2 10/10] cxl/mbox: Add Populate Log support
2026-12-02 15:43 [PATCH v2 00/10] cxl: Userspace background commands Davidlohr Bueso
` (8 preceding siblings ...)
2026-12-02 15:44 ` [PATCH v2 09/10] cxl/mbox: Shout upon async bgcmd race Davidlohr Bueso
@ 2026-12-02 15:44 ` Davidlohr Bueso
9 siblings, 0 replies; 12+ messages in thread
From: Davidlohr Bueso @ 2026-12-02 15:44 UTC (permalink / raw)
To: dave.jiang
Cc: jic23, alison.schofield, ming.li, icheng, benjamin.cheatham,
unsung0317, linux-cxl, dave, Ravi Shankar
From: Ravi Shankar <ravis.opensrc@micron.com>
Adding UAPI support for
CXL r4.0 8.2.10.5.5 Populate Log.
Populate Log may run in the background, in which case the command
returns once the device has started it and the kernel does not report
its outcome. For the Component State Dump Log this is observed through
Get Log (8.2.10.5.2.3): it returns Retry Required until the log is
fully populated, and then the log, with the length and the timestamp
of the data in its header.
Signed-off-by: Ravi Shankar <ravis.opensrc@micron.com>
Reviewed-by: Jonathan Cameron <jic23@kernel.org>
[ dave: track it as a background command, expand changelog ]
Signed-off-by: Davidlohr Bueso <dave@stgolabs.net>
---
drivers/cxl/core/mbox.c | 4 ++++
drivers/cxl/cxlmem.h | 2 ++
drivers/cxl/pci.c | 2 ++
include/uapi/linux/cxl_mem.h | 1 +
4 files changed, 9 insertions(+)
diff --git a/drivers/cxl/core/mbox.c b/drivers/cxl/core/mbox.c
index 017e1b9319a3..fcb3807d2581 100644
--- a/drivers/cxl/core/mbox.c
+++ b/drivers/cxl/core/mbox.c
@@ -59,6 +59,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),
@@ -244,6 +245,9 @@ static bool cxl_set_bgcmd_enabled(struct cxl_background_state *bg, u16 opcode)
case CXL_MBOX_OP_ACTIVATE_FW:
set_bit(CXL_BG_ENABLED_ACTIVATE_FW, bg->enabled_cmds);
break;
+ case CXL_MBOX_OP_POPULATE_LOG:
+ set_bit(CXL_BG_ENABLED_POPULATE_LOG, bg->enabled_cmds);
+ break;
case CXL_MBOX_OP_DO_MAINTENANCE:
set_bit(CXL_BG_ENABLED_DO_MAINTENANCE, bg->enabled_cmds);
break;
diff --git a/drivers/cxl/cxlmem.h b/drivers/cxl/cxlmem.h
index 70037e526e01..ba28d34fe6a4 100644
--- a/drivers/cxl/cxlmem.h
+++ b/drivers/cxl/cxlmem.h
@@ -303,6 +303,7 @@ enum security_cmd_enabled_bits {
enum background_cmd_enabled_bits {
CXL_BG_ENABLED_TRANSFER_FW,
CXL_BG_ENABLED_ACTIVATE_FW,
+ CXL_BG_ENABLED_POPULATE_LOG,
CXL_BG_ENABLED_DO_MAINTENANCE,
CXL_BG_ENABLED_SCAN_MEDIA,
CXL_BG_ENABLED_SANITIZE,
@@ -524,6 +525,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,
diff --git a/drivers/cxl/pci.c b/drivers/cxl/pci.c
index a036f327ec7d..2e38d3cf4310 100644
--- a/drivers/cxl/pci.c
+++ b/drivers/cxl/pci.c
@@ -201,6 +201,8 @@ static inline bool cxl_is_background_cmd(struct cxl_memdev_state *mds,
return test_bit(CXL_BG_ENABLED_TRANSFER_FW, mds->bg.enabled_cmds);
case CXL_MBOX_OP_ACTIVATE_FW:
return test_bit(CXL_BG_ENABLED_ACTIVATE_FW, mds->bg.enabled_cmds);
+ case CXL_MBOX_OP_POPULATE_LOG:
+ return test_bit(CXL_BG_ENABLED_POPULATE_LOG, mds->bg.enabled_cmds);
case CXL_MBOX_OP_DO_MAINTENANCE:
return test_bit(CXL_BG_ENABLED_DO_MAINTENANCE, mds->bg.enabled_cmds);
case CXL_MBOX_OP_SCAN_MEDIA:
diff --git a/include/uapi/linux/cxl_mem.h b/include/uapi/linux/cxl_mem.h
index c6c0fe27495d..ca7fbd1be873 100644
--- a/include/uapi/linux/cxl_mem.h
+++ b/include/uapi/linux/cxl_mem.h
@@ -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
--
2.39.5
^ permalink raw reply related [flat|nested] 12+ messages in thread
* Re: [PATCH v2 08/10] cxl/mbox: Allow userspace background commands
2026-12-02 15:44 ` [PATCH v2 08/10] cxl/mbox: Allow userspace background commands Davidlohr Bueso
@ 2026-12-02 16:10 ` sashiko-bot
0 siblings, 0 replies; 12+ messages in thread
From: sashiko-bot @ 2026-12-02 16:10 UTC (permalink / raw)
To: Davidlohr Bueso; +Cc: linux-cxl
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] cxl/pci: unlocked read of mds->bg.user in cxl_pci_mbox_irq()
- [Medium] cxl/pci: stale cxl_mbox_bgpoll_work() acts on newly started
bg command
- [Low] cxl/pci: unreachable default branch in __cxl_pci_mbox_send_cmd()
--
commit 8459d3a327df488babc367e3665eac99ff3c874f
Author: Davidlohr Bueso <dave@stgolabs.net>
cxl/mbox: Allow userspace background commands
This commit enables abortable userspace background mailbox commands, tracks
them asynchronously via the background polling worker, and allows incoming
background commands to abort an active userspace background command.
> diff --git a/drivers/cxl/pci.c b/drivers/cxl/pci.c
> index ef4853296e860..1cacc9e90a311 100644
> --- a/drivers/cxl/pci.c
> +++ b/drivers/cxl/pci.c
> @@ -134,7 +134,7 @@ 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 (opcode == CXL_MBOX_OP_SANITIZE || mds->bg.user) {
> mutex_lock(&cxl_mbox->mbox_mutex);
> if (mds->bg.opcode)
> mod_delayed_work(system_percpu_wq,
[Severity: Medium]
Can reading mds->bg.user outside cxl_mbox->mbox_mutex in cxl_pci_mbox_irq()
race with __cxl_pci_mbox_send_cmd() and cxl_mbox_bg_end()?
__cxl_pci_mbox_send_cmd() only sets mds->bg.user after ringing and waiting
for the command doorbell under mbox_mutex:
CPU0 CPU1 (threaded IRQ)
__cxl_pci_mbox_send_cmd()
mutex_lock(&cxl_mbox->mbox_mutex)
writel(... | DOORBELL, ...)
cxl_pci_mbox_wait_for_doorbell()
cxl_pci_mbox_irq()
if (... || mds->bg.user) <-- false
rcuwait_wake_up(&cxl_mbox->mbox_wait)
mds->bg.opcode = mbox_cmd->opcode
mds->bg.user = mbox_cmd->user
schedule_delayed_work(..., 1 * HZ)
mutex_unlock(&cxl_mbox->mbox_mutex)
When a fast userspace background command completes before mds->bg.user is
stored, the IRQ handler takes the else branch and fails to expedite
mds->bg.poll_dwork.
Conversely, if cxl_pci_mbox_irq() reads mds->bg.user == true for a finished
or aborted command and blocks on mbox_mutex while a new command is
submitted, it will either kick poll_dwork immediately against a newly
started userspace background command (bumping its poll backoff from 1s to
11s) or exit without calling rcuwait_wake_up(&cxl_mbox->mbox_wait) if a
synchronous kernel background command started and completed in the meantime.
> @@ -159,16 +180,10 @@ 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_done(cxlds)) {
> - mds->bg.poll_tmo_secs = 0;
> - if (mds->bg.opcode == CXL_MBOX_OP_SANITIZE &&
> - mds->security.sanitize_node)
> - sysfs_notify_dirent(mds->security.sanitize_node);
> -
> - dev_dbg(cxlds->dev,
> - "Mailbox background operation (0x%04x) ended\n",
> - mds->bg.opcode);
> - mds->bg.opcode = 0;
> + if (!mds->bg.opcode) {
> + /* cxl_try_to_cancel_background() got here first */
> + } else if (cxl_mbox_background_done(cxlds)) {
> + cxl_mbox_bg_end(mds);
> } else {
> int timeout = min(15 * 60, mds->bg.poll_tmo_secs + 10);
>
[Severity: Medium]
Can an in-flight cxl_mbox_bgpoll_work() blocked on mbox_mutex act on a
newly started background command instead of noticing the cancellation?
In cxl_pci_mbox_send(), cxl_try_to_cancel_background() and
__cxl_pci_mbox_send_cmd() run under the same mbox_mutex acquisition:
CPU0 CPU1 (kworker)
cxl_pci_mbox_send()
mutex_lock(&cxl_mbox->mbox_mutex)
cxl_mbox_bgpoll_work()
mutex_lock(&cxl_mbox->mbox_mutex)
cxl_try_to_cancel_background()
cancel_delayed_work(&mds->bg.poll_dwork)
cxl_mbox_bg_end()
mds->bg.opcode = 0
__cxl_pci_mbox_send_cmd()
mds->bg.poll_tmo_secs = 1
mds->bg.opcode = mbox_cmd->opcode
schedule_delayed_work(..., 1 * HZ)
mutex_unlock(&cxl_mbox->mbox_mutex)
if (!mds->bg.opcode) <-- false
Because mds->bg.opcode is overwritten before mbox_mutex is released, the
stale worker sees the new command's opcode. If the new command is still
running, the stale worker immediately increases mds->bg.poll_tmo_secs from
1s to 11s; if the new command has already finished, it calls
cxl_mbox_bg_end() and clears mds->bg.opcode to 0 while the 1s delayed work
scheduled by __cxl_pci_mbox_send_cmd() remains pending.
> @@ -261,13 +276,26 @@ static int __cxl_pci_mbox_send_cmd(struct cxl_mailbox *cxl_mbox,
> }
>
> /*
> - * 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.
> + * With async bg polling, hardware might be done and the poller still
> + * not be in sync. At the very least, prevent any incoming bg-capable
> + * commands during this window.
> */
> - if (mds->bg.opcode == CXL_MBOX_OP_SANITIZE) {
> + switch (mds->bg.opcode) {
> + case 0:
> + /* no async background operation to stay in sync with */
> + break;
> + case CXL_MBOX_OP_SANITIZE:
> + /*
> + * Ensure no new command comes in until so. Keep the
> + * hardware semantics and only allow device health status.
> + */
> if (mbox_cmd->opcode != CXL_MBOX_OP_GET_HEALTH_INFO)
> return -EBUSY;
> + break;
> + default:
> + if (cxl_is_background_cmd(mds, mbox_cmd->opcode))
> + return -EBUSY;
> + break;
> }
[Severity: Low]
Is the default branch in this switch reachable?
__cxl_pci_mbox_send_cmd() is only called from cxl_try_to_cancel_background()
(with CXL_MBOX_OP_REQUEST_ABORT_BG_OP, where cxl_is_background_cmd() is
false) and from cxl_pci_mbox_send(). In cxl_pci_mbox_send(), any command
where cxl_is_background_cmd() is true first calls
cxl_try_to_cancel_background() under mbox_mutex, which either fails and
returns -EBUSY before calling __cxl_pci_mbox_send_cmd(), or succeeds and
clears mds->bg.opcode to 0 via cxl_mbox_bg_end().
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261202154404.4016305-1-dave@stgolabs.net?part=8
^ permalink raw reply [flat|nested] 12+ messages in thread
end of thread, other threads:[~2026-12-02 16:10 UTC | newest]
Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 15:43 [PATCH v2 00/10] cxl: Userspace background commands Davidlohr Bueso
2026-12-02 15:43 ` [PATCH v2 01/10] cxl/pci: Reschedule the poller with the capped timeout Davidlohr Bueso
2026-12-02 15:43 ` [PATCH v2 02/10] cxl/pci: Preserve Mailbox Control bits when ringing the doorbell Davidlohr Bueso
2026-12-02 15:43 ` [PATCH v2 03/10] cxl/mbox: Track background commands from CEL Davidlohr Bueso
2026-12-02 15:43 ` [PATCH v2 04/10] cxl/mbox: Decouple async background handling from security Davidlohr Bueso
2026-12-02 15:43 ` [PATCH v2 05/10] cxl/pci: Lockless background synchronous polling Davidlohr Bueso
2026-12-02 15:44 ` [PATCH v2 06/10] cxl/mbox: Stronger cxl_mbox_background_complete() semantics Davidlohr Bueso
2026-12-02 15:44 ` [PATCH v2 07/10] cxl/mbox: Add missing mailbox return codes Davidlohr Bueso
2026-12-02 15:44 ` [PATCH v2 08/10] cxl/mbox: Allow userspace background commands Davidlohr Bueso
2026-12-02 16:10 ` sashiko-bot
2026-12-02 15:44 ` [PATCH v2 09/10] cxl/mbox: Shout upon async bgcmd race Davidlohr Bueso
2026-12-02 15:44 ` [PATCH v2 10/10] cxl/mbox: Add Populate Log support Davidlohr Bueso
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.