Skip to contentSkip to conversation

cxl/mbox: Use guard() for mbox_mutex locking

PATCH In review Davidlohr Bueso 3 participants 7 messages Apr 28 – Apr 29 CXL

lore
Share & tools
lore

Davidlohr Bueso

author maintainer
#0 lore raw

PATCH 1/1 cxl/mbox: Use guard() for mbox_mutex locking

Use the new helpers and simplify the code. No change in semantics.

---
 drivers/cxl/core/memdev.c | 16 ++++++++--------
 drivers/cxl/pci.c         | 21 ++++++++-------------
 2 files changed, 16 insertions(+), 21 deletions(-)
drivers/cxl/core/memdev.c +8 -8
index 3db4e91170a8..0c9067ced7b3 100644
@@ -138,10 +138,10 @@ static ssize_t security_state_show(struct device *dev,
 	int rc = 0;
 
 	/* sync with latest submission state */
-	mutex_lock(&cxl_mbox->mbox_mutex);
-	if (mds->security.sanitize_active)
-		rc = sysfs_emit(buf, "sanitize\n");
-	mutex_unlock(&cxl_mbox->mbox_mutex);
+	scoped_guard(mutex, &cxl_mbox->mbox_mutex) {
+		if (mds->security.sanitize_active)
+			rc = sysfs_emit(buf, "sanitize\n");
+	}
 	if (rc)
 		return rc;
 
@@ -1247,10 +1247,10 @@ static void sanitize_teardown_notifier(void *data)
 	 * Prevent new irq triggered invocations of the workqueue 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);
+	scoped_guard(mutex, &cxl_mbox->mbox_mutex) {
+		state = mds->security.sanitize_node;
+		mds->security.sanitize_node = NULL;
+	}
 
 	cancel_delayed_work_sync(&mds->security.poll_dwork);
 	sysfs_put(state);
drivers/cxl/pci.c +8 -13
index 95bf773aab14..3462cea6e61b 100644
@@ -134,10 +134,11 @@ 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) {
-		mutex_lock(&cxl_mbox->mbox_mutex);
-		if (mds->security.sanitize_node)
-			mod_delayed_work(system_percpu_wq, &mds->security.poll_dwork, 0);
-		mutex_unlock(&cxl_mbox->mbox_mutex);
+		scoped_guard(mutex, &cxl_mbox->mbox_mutex) {
+			if (mds->security.sanitize_node)
+				mod_delayed_work(system_percpu_wq,
+						 &mds->security.poll_dwork, 0);
+		}
 	} else {
 		/* short-circuit the wait in __cxl_pci_mbox_send_cmd() */
 		rcuwait_wake_up(&cxl_mbox->mbox_wait);
@@ -156,7 +157,7 @@ static void cxl_mbox_sanitize_work(struct work_struct *work)
 	struct cxl_dev_state *cxlds = &mds->cxlds;
 	struct cxl_mailbox *cxl_mbox = &cxlds->cxl_mbox;
 
-	mutex_lock(&cxl_mbox->mbox_mutex);
+	guard(mutex)(&cxl_mbox->mbox_mutex);
 	if (cxl_mbox_background_complete(cxlds)) {
 		mds->security.poll_tmo_secs = 0;
 		if (mds->security.sanitize_node)
@@ -170,7 +171,6 @@ static void cxl_mbox_sanitize_work(struct work_struct *work)
 		mds->security.poll_tmo_secs = min(15 * 60, timeout);
 		schedule_delayed_work(&mds->security.poll_dwork, timeout * HZ);
 	}
-	mutex_unlock(&cxl_mbox->mbox_mutex);
 }
 
 /**
@@ -377,13 +377,8 @@ static int __cxl_pci_mbox_send_cmd(struct cxl_mailbox *cxl_mbox,
 static int cxl_pci_mbox_send(struct cxl_mailbox *cxl_mbox,
 			     struct cxl_mbox_cmd *cmd)
 {
-	int rc;
-
-	mutex_lock(&cxl_mbox->mbox_mutex);
-	rc = __cxl_pci_mbox_send_cmd(cxl_mbox, cmd);
-	mutex_unlock(&cxl_mbox->mbox_mutex);
-
-	return rc;
+	guard(mutex)(&cxl_mbox->mbox_mutex);
+	return __cxl_pci_mbox_send_cmd(cxl_mbox, cmd);
 }
 
 static int cxl_pci_setup_mailbox(struct cxl_memdev_state *mds, bool irq_avail)
Signature · 1 line
2.39.5
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 4 lines · Davidlohr Bueso

Use the new helpers and simplify the code. No change in semantics.

Is this in prep of media ops?

Quoted 92 lines
---
 drivers/cxl/core/memdev.c | 16 ++++++++--------
 drivers/cxl/pci.c         | 21 ++++++++-------------
 2 files changed, 16 insertions(+), 21 deletions(-)
drivers/cxl/core/memdev.c +8 -8
index 3db4e91170a8..0c9067ced7b3 100644
@@ -138,10 +138,10 @@ static ssize_t security_state_show(struct device *dev,
 	int rc = 0;
 
 	/* sync with latest submission state */
-	mutex_lock(&cxl_mbox->mbox_mutex);
-	if (mds->security.sanitize_active)
-		rc = sysfs_emit(buf, "sanitize\n");
-	mutex_unlock(&cxl_mbox->mbox_mutex);
+	scoped_guard(mutex, &cxl_mbox->mbox_mutex) {
+		if (mds->security.sanitize_active)
+			rc = sysfs_emit(buf, "sanitize\n");
+	}
 	if (rc)
 		return rc;
 
@@ -1247,10 +1247,10 @@ static void sanitize_teardown_notifier(void *data)
 	 * Prevent new irq triggered invocations of the workqueue 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);
+	scoped_guard(mutex, &cxl_mbox->mbox_mutex) {
+		state = mds->security.sanitize_node;
+		mds->security.sanitize_node = NULL;
+	}
 
 	cancel_delayed_work_sync(&mds->security.poll_dwork);
 	sysfs_put(state);
drivers/cxl/pci.c +8 -13
index 95bf773aab14..3462cea6e61b 100644
@@ -134,10 +134,11 @@ 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) {
-		mutex_lock(&cxl_mbox->mbox_mutex);
-		if (mds->security.sanitize_node)
-			mod_delayed_work(system_percpu_wq, &mds->security.poll_dwork, 0);
-		mutex_unlock(&cxl_mbox->mbox_mutex);
+		scoped_guard(mutex, &cxl_mbox->mbox_mutex) {
+			if (mds->security.sanitize_node)
+				mod_delayed_work(system_percpu_wq,
+						 &mds->security.poll_dwork, 0);
+		}
 	} else {
 		/* short-circuit the wait in __cxl_pci_mbox_send_cmd() */
 		rcuwait_wake_up(&cxl_mbox->mbox_wait);
@@ -156,7 +157,7 @@ static void cxl_mbox_sanitize_work(struct work_struct *work)
 	struct cxl_dev_state *cxlds = &mds->cxlds;
 	struct cxl_mailbox *cxl_mbox = &cxlds->cxl_mbox;
 
-	mutex_lock(&cxl_mbox->mbox_mutex);
+	guard(mutex)(&cxl_mbox->mbox_mutex);
 	if (cxl_mbox_background_complete(cxlds)) {
 		mds->security.poll_tmo_secs = 0;
 		if (mds->security.sanitize_node)
@@ -170,7 +171,6 @@ static void cxl_mbox_sanitize_work(struct work_struct *work)
 		mds->security.poll_tmo_secs = min(15 * 60, timeout);
 		schedule_delayed_work(&mds->security.poll_dwork, timeout * HZ);
 	}
-	mutex_unlock(&cxl_mbox->mbox_mutex);
 }
 
 /**
@@ -377,13 +377,8 @@ static int __cxl_pci_mbox_send_cmd(struct cxl_mailbox *cxl_mbox,
 static int cxl_pci_mbox_send(struct cxl_mailbox *cxl_mbox,
 			     struct cxl_mbox_cmd *cmd)
 {
-	int rc;
-
-	mutex_lock(&cxl_mbox->mbox_mutex);
-	rc = __cxl_pci_mbox_send_cmd(cxl_mbox, cmd);
-	mutex_unlock(&cxl_mbox->mbox_mutex);
-
-	return rc;
+	guard(mutex)(&cxl_mbox->mbox_mutex);
+	return __cxl_pci_mbox_send_cmd(cxl_mbox, cmd);
 }
 
 static int cxl_pci_setup_mailbox(struct cxl_memdev_state *mds, bool irq_avail)
maintainer
#3 lore raw
Quoted 7 lines · Davidlohr Bueso
Quoted 5 lines · Dave Jiang
Quoted 3 lines · Davidlohr Bueso

Use the new helpers and simplify the code. No change in semantics.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Is this in prep of media ops?

I found the open coded locking by doing the media ops patch, but ultimately these changes are independent.

May not be worth the churn if it's not part of ongoing work.

Quoted 3 lines

Thanks,
Davidlohr

author maintainer
#4 lore raw
Quoted 1 line · Dave Jiang

May not be worth the churn if it's not part of ongoing work.

I don't disagree, also considering Jonathan not being fond of the change, lets just ignore the patch altogether.

Thanks,
Davidlohr

maintainer
#5 lore raw
In reply to Davidlohr Bueso
Quoted 2 lines · On Tue, 28 Apr 2026 14:22:20 -0700 Davidlohr Bueso <[email protected]> wrote:

Use the new helpers and simplify the code. No change in semantics.

Does it simplify the code? I'm not sure these particular ones are that helpful.

Don't get me wrong, I love guards() and using them in new code is fine but I'm not sure it's worth the churn if we don't see a significant advantage.

Some specific comments inline. I think with a few tweaks the advantages become greater and outweight the churn aspect
- others may disagree!

Quoted 15 lines
---
 drivers/cxl/core/memdev.c | 16 ++++++++--------
 drivers/cxl/pci.c         | 21 ++++++++-------------
 2 files changed, 16 insertions(+), 21 deletions(-)
drivers/cxl/core/memdev.c +0 -0
index 3db4e91170a8..0c9067ced7b3 100644
@@ -138,10 +138,10 @@ static ssize_t security_state_show(struct device *dev,
 	int rc = 0;
 
 	/* sync with latest submission state */

drivers/cxl/core/memdev.c static ssize_t security_state_show(struct device *dev, · line 146

Quoted 10 lines
drivers/cxl/core/memdev.c +4 -4
-	mutex_lock(&cxl_mbox->mbox_mutex);
-	if (mds->security.sanitize_active)
-		rc = sysfs_emit(buf, "sanitize\n");
-	mutex_unlock(&cxl_mbox->mbox_mutex);
+	scoped_guard(mutex, &cxl_mbox->mbox_mutex) {
+		if (mds->security.sanitize_active)
+			rc = sysfs_emit(buf, "sanitize\n");
+	}
 	if (rc)
 		return rc;

As it stands, if anything this is worse from readability point of view as the setting of rc is getting further away.
However... If we take a it a step further I think it would be worth doing.

	scoped_guard(mutex, &cxl_mbox->mbox_mutex) {
		if (mds->security.santize_active) {
			rc = sysfs_emit(buf, "sanitize\n");
			if (rc)
				return rc;
		}
	}

+ can drop the rc init at the top.
That way the scoped_guard() is letting us move the error check nearer the source of the error and is a win.

drivers/cxl/core/memdev.c static void sanitize_teardown_notifier(void *data) · line 1253

Quoted 13 lines
drivers/cxl/core/memdev.c +4 -4
@@ -1247,10 +1247,10 @@ static void sanitize_teardown_notifier(void *data)
 	 * Prevent new irq triggered invocations of the workqueue 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);
+	scoped_guard(mutex, &cxl_mbox->mbox_mutex) {
+		state = mds->security.sanitize_node;
+		mds->security.sanitize_node = NULL;
+	}

This one is churn for me. But if it's the only manual mutex handling int he file that is left, fair enough.

Quoted 9 lines
drivers/cxl/core/memdev.c +0 -0
 	cancel_delayed_work_sync(&mds->security.poll_dwork);
 	sysfs_put(state);
drivers/cxl/pci.c +0 -0
index 95bf773aab14..3462cea6e61b 100644
@@ -134,10 +134,11 @@ static irqreturn_t cxl_pci_mbox_irq(int irq, void *id)
 	reg = readq(cxlds->regs.mbox + CXLDEV_MBOX_BG_CMD_STATUS_OFFSET);

drivers/cxl/pci.c static irqreturn_t cxl_pci_mbox_irq(int irq, void *id) · line 140

Quoted 10 lines
drivers/cxl/pci.c +4 -4
 	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);
-		mutex_unlock(&cxl_mbox->mbox_mutex);
+		scoped_guard(mutex, &cxl_mbox->mbox_mutex) {
+			if (mds->security.sanitize_node)
+				mod_delayed_work(system_percpu_wq,
+						 &mds->security.poll_dwork, 0);

This one I dislike because the indent is getting larger for no major readability advantage. GIven the scope is tightly defined anyway how about instead doing:

	if (opcode == CXL_MBOX_OP_SANTIZE) {
		guard(mutex)(&cxl_mbox.santize_mode);
		if (mds->security.sanitize_node)
			mod_delayed_work(system_percpu_wq, &mds->security.poll_dwork, 0);
Quoted 23 lines
drivers/cxl/pci.c +1 -2
 	} else {
 		/* short-circuit the wait in __cxl_pci_mbox_send_cmd() */
 		rcuwait_wake_up(&cxl_mbox->mbox_wait);
@@ -156,7 +157,7 @@ static void cxl_mbox_sanitize_work(struct work_struct *work)
 	struct cxl_dev_state *cxlds = &mds->cxlds;
 	struct cxl_mailbox *cxl_mbox = &cxlds->cxl_mbox;
 
-	mutex_lock(&cxl_mbox->mbox_mutex);
+	guard(mutex)(&cxl_mbox->mbox_mutex);
 	if (cxl_mbox_background_complete(cxlds)) {
 		mds->security.poll_tmo_secs = 0;
 		if (mds->security.sanitize_node)
@@ -170,7 +171,6 @@ static void cxl_mbox_sanitize_work(struct work_struct *work)
 		mds->security.poll_tmo_secs = min(15 * 60, timeout);
 		schedule_delayed_work(&mds->security.poll_dwork, timeout * HZ);
 	}
-	mutex_unlock(&cxl_mbox->mbox_mutex);
 }
 
 /**
@@ -377,13 +377,8 @@ static int __cxl_pci_mbox_send_cmd(struct cxl_mailbox *cxl_mbox,
 static int cxl_pci_mbox_send(struct cxl_mailbox *cxl_mbox,
 			     struct cxl_mbox_cmd *cmd)

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

Quoted 10 lines
drivers/cxl/pci.c +2 -7
 {
-	int rc;
-
-	mutex_lock(&cxl_mbox->mbox_mutex);
-	rc = __cxl_pci_mbox_send_cmd(cxl_mbox, cmd);
-	mutex_unlock(&cxl_mbox->mbox_mutex);
-
-	return rc;
+	guard(mutex)(&cxl_mbox->mbox_mutex);
+	return __cxl_pci_mbox_send_cmd(cxl_mbox, cmd);

I like these really simple ones just for saving lines of code. So this is good as far as I'm concerned.

Quoted 3 lines
drivers/cxl/pci.c +0 -0
 }
 
 static int cxl_pci_setup_mailbox(struct cxl_memdev_state *mds, bool irq_avail)
author maintainer
#6 lore raw
In reply to Davidlohr Bueso
Quoted 73 lines · Davidlohr Bueso

Use the new helpers and simplify the code. No change in semantics.

---
drivers/cxl/core/memdev.c | 16 ++++++++--------
drivers/cxl/pci.c | 21 ++++++++-------------

2 files changed, 16 insertions(+), 21 deletions(-)
drivers/cxl/core/memdev.c +8 -8
index 3db4e91170a8..0c9067ced7b3 100644
@@ -138,10 +138,10 @@ static ssize_t security_state_show(struct device *dev,
 	int rc = 0;
 
 	/* sync with latest submission state */
-	mutex_lock(&cxl_mbox->mbox_mutex);
-	if (mds->security.sanitize_active)
-		rc = sysfs_emit(buf, "sanitize\n");
-	mutex_unlock(&cxl_mbox->mbox_mutex);
+	scoped_guard(mutex, &cxl_mbox->mbox_mutex) {
+		if (mds->security.sanitize_active)
+			rc = sysfs_emit(buf, "sanitize\n");
+	}
 	if (rc)
 		return rc;
 
@@ -1247,10 +1247,10 @@ static void sanitize_teardown_notifier(void *data)
 	 * Prevent new irq triggered invocations of the workqueue 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);
+	scoped_guard(mutex, &cxl_mbox->mbox_mutex) {
+		state = mds->security.sanitize_node;
+		mds->security.sanitize_node = NULL;
+	}
 
 	cancel_delayed_work_sync(&mds->security.poll_dwork);
 	sysfs_put(state);
drivers/cxl/pci.c +6 -5
index 95bf773aab14..3462cea6e61b 100644
@@ -134,10 +134,11 @@ 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) {
-		mutex_lock(&cxl_mbox->mbox_mutex);
-		if (mds->security.sanitize_node)
-			mod_delayed_work(system_percpu_wq, &mds->security.poll_dwork, 0);
-		mutex_unlock(&cxl_mbox->mbox_mutex);
+		scoped_guard(mutex, &cxl_mbox->mbox_mutex) {
+			if (mds->security.sanitize_node)
+				mod_delayed_work(system_percpu_wq,
+						 &mds->security.poll_dwork, 0);
+		}
 	} else {
 		/* short-circuit the wait in __cxl_pci_mbox_send_cmd() */
 		rcuwait_wake_up(&cxl_mbox->mbox_wait);
@@ -156,7 +157,7 @@ static void cxl_mbox_sanitize_work(struct work_struct *work)
 	struct cxl_dev_state *cxlds = &mds->cxlds;
 	struct cxl_mailbox *cxl_mbox = &cxlds->cxl_mbox;
 
-	mutex_lock(&cxl_mbox->mbox_mutex);
+	guard(mutex)(&cxl_mbox->mbox_mutex);
 	if (cxl_mbox_background_complete(cxlds)) {
 		mds->security.poll_tmo_secs = 0;
 		if (mds->security.sanitize_node)

drivers/cxl/pci.c static void cxl_mbox_sanitize_work(struct work_struct *work) · line 172

Quoted 2 lines
drivers/cxl/pci.c +0 -0
@@ -170,7 +171,6 @@ static void cxl_mbox_sanitize_work(struct work_struct *work)
 		mds->security.poll_tmo_secs = min(15 * 60, timeout);
 		schedule_delayed_work(&mds->security.poll_dwork, timeout * HZ);

Sashiko had some feedback, which was a false concern. Copy pasting here as I don't know how to reply to it yet.

Quoted 15 lines
drivers/cxl/pci.c +0 -0
 This isn't a regression introduced by this patch, but looking at this code,

can this unconditionally re-arm the delayed work during teardown and cause a use-after-free?

During device teardown, sanitize_teardown_notifier() clears the sanitize_node to NULL under the mbox_mutex and then drops the lock to call cancel_delayed_work_sync().

If cxl_mbox_sanitize_work() is executing concurrently, cancel_delayed_work_sync() will wait for it to finish. Meanwhile, the work function acquires mbox_mutex, sees the command is not complete, and calls schedule_delayed_work():

CPU1
sanitize_teardown_notifier()

Quoted 7 lines
   mds->security.sanitize_node = NULL;
   cancel_delayed_work_sync(&mds->security.poll_dwork);
CPU2
cxl_mbox_sanitize_work()
   guard(mutex)(&cxl_mbox->mbox_mutex);
   ...
   schedule_delayed_work(&mds->security.poll_dwork, timeout * HZ)

This is fine.

cancel_delayed_work_sync() uses WORK_CANCEL_DISABLE to bump the work's WORK_OFFQ_DISABLE_MASK and clears the pending bit.

queue_delayed_work_on() checks the disable mask via clear_pending_if_disabled(), between setting PENDING and adding the new work. If disabled, PENDING is cleared again and __queue_delayed_work is not called.

Quoted 3 lines

Unlike the IRQ handler which safely checks if (mds->security.sanitize_node) before re-arming, the work function lacks this teardown check.

sanitize_node does not dictate correctness for this, it is about the status file.

Quoted 4 lines

Does this result in the delayed work remaining armed after cancel_delayed_work_sync() returns, eventually executing on freed mds memory when the timer expires?

No, cancel_delayed_work_sync() is explicitly documented to handle rearming work.

Thanks,
Davidlohr