Skip to contentSkip to conversation

cxl/mbox: Serialize RAW Get/Set Feature commands

PATCH New Richard Cheng 2 participants 2 messages Jul 14 CXL

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 cxl/mbox: Serialize RAW Get/Set Feature commands

Dave Jiang's patch [1] adds feat_mutex to serialize kernel-managed Get and Set Feature operations. However, the RAW command path calls mbox_send() directly and bypasses cxl_get_feature() and cxl_set_feature().

Take feat_mutex around each RAW Get and Set Feature mailbox send. This prevents individual RAW Feature commands from interleaving with kernel-managed Feature helper loops. Other RAW commands are unchanged.

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

---
 drivers/cxl/core/mbox.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/cxl/core/mbox.c +6 -0
index 0370ac39ec4a..55b0c3ac1ede 100644
@@ -599,6 +599,8 @@ static int handle_mailbox_cmd_from_user(struct cxl_mailbox *cxl_mbox,
 					u32 *retval)
 {
 	struct device *dev = cxl_mbox->host;
+	bool is_feature_cmd = mbox_cmd->opcode == CXL_MBOX_OP_GET_FEATURE ||
+			      mbox_cmd->opcode == CXL_MBOX_OP_SET_FEATURE;
 	int rc;
 
 	dev_dbg(dev,
@@ -608,7 +610,11 @@ static int handle_mailbox_cmd_from_user(struct cxl_mailbox *cxl_mbox,
 		cxl_mem_opcode_to_name(mbox_cmd->opcode),
 		mbox_cmd->opcode, mbox_cmd->size_in);
 
+	if (is_feature_cmd)
+		mutex_lock(&cxl_mbox->feat_mutex);
 	rc = cxl_mbox->mbox_send(cxl_mbox, mbox_cmd);
+	if (is_feature_cmd)
+		mutex_unlock(&cxl_mbox->feat_mutex);
 	if (rc)
 		goto out;
 
Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Missing lock acquisition in kernel-managed Feature paths defeats the intended synchronization.…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Missing lock acquisition in kernel-managed Feature paths defeats the intended synchronization.
- [Low] The patch references an undeclared struct member feat_mutex, resulting in an unconditional build failure. --

--- Patch [1]: [PATCH] cxl/mbox: Serialize RAW Get/Set Feature commands --- commit ffea860f8f4c719a69a797f95e4162563fecddae
Author: Richard Cheng <[email protected]>

cxl/mbox: Serialize RAW Get/Set Feature commands

This commit modifies handle_mailbox_cmd_from_user() to acquire feat_mutex around RAW Get and Set Feature mailbox commands. It aims to serialize these userspace commands against kernel-managed Feature operations.

Quoted 14 lines
drivers/cxl/core/mbox.c +2 -0
index 79543103ac298..b94b8514f6786 100644
@@ -594,6 +594,8 @@ static int handle_mailbox_cmd_from_user(struct cxl_mailbox *cxl_mbox,
 					u32 *retval)
 {
 	struct device *dev = cxl_mbox->host;
+	bool is_feature_cmd = mbox_cmd->opcode == CXL_MBOX_OP_GET_FEATURE ||
+			      mbox_cmd->opcode == CXL_MBOX_OP_SET_FEATURE;
 	int rc;
 
 	dev_dbg(dev,

drivers/cxl/core/mbox.c static int handle_mailbox_cmd_from_user(struct cxl_mailbox *cxl_mbox, · line 609

Quoted 5 lines
drivers/cxl/core/mbox.c +2 -0
@@ -603,7 +605,11 @@ static int handle_mailbox_cmd_from_user(struct cxl_mailbox *cxl_mbox,
 		cxl_mem_opcode_to_name(mbox_cmd->opcode),
 		mbox_cmd->opcode, mbox_cmd->size_in);
 
+	if (is_feature_cmd)
+		mutex_lock(&cxl_mbox->feat_mutex);

[Severity: Low]
Does this code compile? Looking at struct cxl_mailbox in include/cxl/mailbox.h, it doesn't appear to have a feat_mutex member declared:

include/cxl/mailbox.h:

struct cxl_mailbox {
	struct device *host;
	DECLARE_BITMAP(enabled_cmds, CXL_MEM_COMMAND_ID_MAX);
	DECLARE_BITMAP(exclusive_cmds, CXL_MEM_COMMAND_ID_MAX);
	size_t payload_size;
	struct mutex mbox_mutex;
	struct rcuwait mbox_wait;
	int (*mbox_send)(struct cxl_mailbox *cxl_mbox, struct cxl_mbox_cmd *cmd);
	enum cxl_features_capability feat_cap;
};

[Severity: High]
The commit message indicates this lock is meant to serialize RAW commands against kernel-managed Feature helper loops like cxl_get_feature().

However, looking at cxl_get_feature() in drivers/cxl/core/features.c, it loops over cxl_internal_send_cmd() without acquiring feat_mutex:

drivers/cxl/core/features.c:cxl_get_feature() {
	...
	do {
		...
		rc = cxl_internal_send_cmd(cxl_mbox, &mbox_cmd);
		...
	} while (data_rcvd_size < feat_out_size);
}

Does this missing lock acquisition in the kernel-managed paths defeat the intended synchronization, allowing interleaved execution if userspace submits RAW commands concurrently?

Quoted 5 lines
drivers/cxl/core/mbox.c +2 -0
 	rc = cxl_mbox->mbox_send(cxl_mbox, mbox_cmd);
+	if (is_feature_cmd)
+		mutex_unlock(&cxl_mbox->feat_mutex);
 	if (rc)
 		goto out;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1